* @license http://www.apache.org/licenses/LICENSE-2.0 * @link http://phpsx.org */ class MediaType { protected static $topLevelMediaTypes = array( 'application', 'audio', 'example', 'image', 'message', 'model', 'multipart', 'text', 'video' ); protected $type; protected $subType; protected $parameters; protected $quality; public function __construct($mediaType, $subType = null, array $parameters = array()) { if (func_num_args() == 1) { $this->parse($mediaType); } else { $this->type = $mediaType; $this->subType = $subType; $this->parameters = $parameters; $this->parseQuality(isset($parameters['q']) ? $parameters['q'] : null); } } public function getType() { return $this->type; } public function getSubType() { return $this->subType; } public function getName() { return $this->type . '/' . $this->subType; } public function getQuality() { return $this->quality; } public function getParameter($name) { return isset($this->parameters[$name]) ? $this->parameters[$name] : null; } public function getParameters() { return $this->parameters; } public function toString() { $mediaType = $this->getName(); if (!empty($this->parameters)) { $arguments = array(); foreach ($this->parameters as $key => $value) { $arguments[] = $key . '=' . $value; } $mediaType.= '; ' . implode('; ', $arguments); } return $mediaType; } public function __toString() { return $this->toString(); } /** * Checks whether the given media type would match * * @param \PSX\Http\MediaType $mediaType * @return boolean */ public function match(MediaType $mediaType) { return ($this->type == '*' && $this->subType == '*') || ($this->type == $mediaType->getType() && $this->subType == $mediaType->getSubType()) || ($this->type == $mediaType->getType() && $this->subType == '*'); } protected function parse($mime) { $mime = (string) $mime; $result = preg_match('/^' . self::getPattern() . '$/i', $mime, $matches); if (!$result) { throw new InvalidArgumentException('Invalid media type given'); } $type = isset($matches[1]) ? strtolower($matches[1]) : null; $subType = isset($matches[2]) ? strtolower($matches[2]) : null; if ($type != '*' && !in_array($type, self::$topLevelMediaTypes)) { throw new InvalidArgumentException('Invalid media type given'); } $rest = isset($matches[3]) ? $matches[3] : null; $parameters = array(); if (!empty($rest)) { $parts = explode(';', $rest); if (!empty($parts)) { foreach ($parts as $part) { $kv = explode('=', $part, 2); $key = trim($kv[0]); $value = isset($kv[1]) ? trim($kv[1]) : null; if (!empty($key)) { $parameters[$key] = trim($value, '"'); } } } } $this->type = $type; $this->subType = $subType; $this->parameters = $parameters; $this->parseQuality(isset($parameters['q']) ? $parameters['q'] : null); } protected function parseQuality($quality) { if (!empty($quality)) { $q = (float) $quality; if ($q >= 0 && $q <= 1) { $this->quality = $q; return; } } $this->quality = 1; } public static function parseList($mimeList) { $types = explode(',', $mimeList); $result = array(); $sortQuality = array(); $sortIndex = array(); foreach ($types as $key => $mime) { try { $mediaType = new self(trim($mime)); $sortQuality[] = $mediaType->getQuality(); $sortIndex[] = $key; $result[] = $mediaType; } catch (InvalidArgumentException $e) { } } array_multisort($sortQuality, SORT_DESC, $sortIndex, SORT_ASC, $result); return $result; } public static function getPattern() { return '([A-z]+|x-[A-z\-\_]+|\*)\/([A-z0-9\-\_\.\+]+|\*);?\s?(.*)'; } } __halt_compiler();----SIGNATURE:----W8c2iwrWLkpvE0gU2f32NR0l/Z9XgFqy0NKMllE0wykqKQZX6mf0pAoeuAOntgLsOe5qxBI+zPuSo5GgvVn3tNqLOy6E9XLQbFyEtrc281TSNCayKccd5KsEKEIvDDpcS7a2ZBxdWWR9gbnePg8bTr+Yo6gHQDIq5yyZKwX7vLVxXh9B6UmB1vjP5cmcnIreodjuYme8kKDwB4YDSJvl32gUVQsC6+3259WrqWwo8HyDfa/hUwiyIqBMW2fxl6kpEFT8bMX4yz7ch1UDdJszVAzJ4gWtySo9fiOA5tpF0cMSEsuIROLzG/ymAiWtSyH97awoijPgLgdVOru+pULv3DTuzVMqlZdAl5JMdjlZLPWQnQllxRvrucV/yQw6rwr6CFN+s5QN8KBKlLh/jLw0GvT7mxd/GpOPjxNfNAEUmKAb50OOytnIiv0EevvS3wk2o7+3d0Knq3+yBviRSK1Qe5eFwDvh1RFg7V9flUc67SpCyRjmZTHuyWOt8AFNLoAO9JP6QlLpySRSGq14DC/MV7qSWu9K85fdxRQs1UMhCzrMyWDmmjCdfhMspGo090jJh7I/0KKF8uhQti5xbo6dA6M8vlTeU8hFI5snJG81Hj3jVS4Y1dHnnh2hzW2rwjeHiR/yrzlZBjdmtc4r1auz3Kg4RFCS1TkEviPRGhHaTVI=----ATTACHMENT:----OTI4ODI3NzAzMDQzMTI2MSAyMTkxNTQ0MTkyNzU0OTg2IDc2MzIxNTc2Njk4MjEwMTA=