Ejemplo n.º 1
0
 public function __construct()
 {
     $validContentTypes = array('text/html', 'application/xhtml+xml', 'application/xml');
     foreach ($validContentTypes as $validContentTypeString) {
         $mediaTypeParser = new InternetMediaTypeParser();
         $this->addValidContentType($mediaTypeParser->parse($validContentTypeString));
     }
 }
 public function testGetTypeSubtypeString()
 {
     $mediaType = new InternetMediaType();
     $mediaType->setType('text');
     $mediaType->setSubtype('html');
     $this->assertEquals('text/html', $mediaType->getTypeSubtypeString());
     $mediaTypeParser = new InternetMediaTypeParser();
     $mediaType = $mediaTypeParser->parse('application/json; charset=UTF-8');
     $this->assertEquals('application/json', $mediaType->getTypeSubtypeString());
 }
 public function parse($htmlValidatorHeaderContent)
 {
     $header = new Header();
     $headerLines = explode("\n", $htmlValidatorHeaderContent);
     foreach ($headerLines as $headerLine) {
         $keyValueParts = explode(':', $headerLine, 2);
         if ($this->isContentTypeHeader($keyValueParts[0])) {
             $mediaTypeParser = new MediaTypeParser();
             $header->set($keyValueParts[0], $mediaTypeParser->parse($keyValueParts[1]));
         }
         if ($this->isW3cValidatorHeaderKey($keyValueParts[0])) {
             $header->set($this->getKeyFromW3cKey($keyValueParts[0]), $this->getTypedValueFromW3cValue(trim($keyValueParts[1])));
         }
     }
     return $header;
 }
Ejemplo n.º 4
0
 /**
  * 
  * @return InternetMediaTypeParser
  */
 public function getInternetMediaTypeParser()
 {
     if (is_null($this->internetMediaTypeParser)) {
         $this->internetMediaTypeParser = new InternetMediaTypeParser();
         $this->internetMediaTypeParser->getConfiguration()->enableIgnoreInvalidAttributes();
         $this->internetMediaTypeParser->getConfiguration()->enableAttemptToRecoverFromInvalidInternalCharacter();
     }
     return $this->internetMediaTypeParser;
 }
Ejemplo n.º 5
0
 /**
  * Get the character set from the current web page
  * 
  * 
  * @return string|null
  */
 public function getCharacterSet()
 {
     $this->isContentTypeMalformed = false;
     $metaContentTypeSelectors = array('meta[http-equiv=Content-Type]' => false, 'meta[http-equiv=content-type]' => false, 'meta[name=Content-Type]' => true);
     foreach ($metaContentTypeSelectors as $metaContentTypeSelector => $isMalformed) {
         $contentTypeString = null;
         @$this->getDomQuery($metaContentTypeSelector)->each(function ($index, \DOMElement $domElement) use(&$contentTypeString) {
             $contentTypeString = $domElement->getAttribute('content');
         });
         if (is_string($contentTypeString)) {
             $mediaTypeParser = new InternetMediaTypeParser();
             $mediaTypeParser->setIgnoreInvalidAttributes(true);
             $mediaTypeParser->setAttemptToRecoverFromInvalidInternalCharacter(true);
             /* @var $mediaType InternetMediaType */
             try {
                 $mediaType = $mediaTypeParser->parse($contentTypeString);
                 if ($mediaType->hasParameter('charset')) {
                     $this->isContentTypeMalformed = $isMalformed;
                     return (string) $mediaType->getParameter('charset')->getValue();
                 }
             } catch (\webignition\InternetMediaType\Parser\TypeParserException $typeParserException) {
                 // Occurs when we can't parse the in-markup content type
                 // Ignore such exceptions to treat this as having no in-markup content type
             }
         }
     }
     $charsetString = '';
     @$this->getDomQuery('meta[charset]')->each(function ($index, \DOMElement $domElement) use(&$charsetString) {
         $charsetString = $domElement->getAttribute('charset');
     });
     if (is_string($charsetString) && $charsetString !== '') {
         return $charsetString;
     }
     $contentTypeString = null;
     @$this->getDomQuery('meta[name=Content-Type]')->each(function ($index, \DOMElement $domElement) use(&$contentTypeString) {
         $contentTypeString = $domElement->getAttribute('content');
     });
     return null;
 }
Ejemplo n.º 6
0
 /**
  * 
  * @param HttpResponse $response
  * @return \webignition\InternetMediaType\InternetMediaType
  */
 private function getContentTypeFromResponse(HttpResponse $response)
 {
     $mediaTypeParser = new InternetMediaTypeParser();
     $mediaTypeParser->setAttemptToRecoverFromInvalidInternalCharacter(true);
     $mediaTypeParser->setIgnoreInvalidAttributes(true);
     return $mediaTypeParser->parse($response->getHeader('content-type'));
 }