/**
  * This method navigates thru the tree structure and validates the article content.
  *
  * @param InstantArticle $article The article that will be checked.
  * @return array of string with the warnings raised during the check.
  */
 public static function check($article)
 {
     Type::enforce($article, InstantArticle::getClassName());
     $warnings = array();
     self::getReport(array($article), $warnings);
     return $warnings;
 }
Exemplo n.º 2
0
 public function apply($transformer, $context_element, $node)
 {
     $h2 = H2::create();
     if (Type::is($context_element, array(Header::getClassName(), Caption::getClassName()))) {
         $context_element->withSubTitle($h2);
     } elseif (Type::is($context_element, InstantArticle::getClassName())) {
         $context_element->addChild($h2);
     }
     if ($this->getProperty(Caption::POSITION_BELOW, $node)) {
         $h2->withPosition(Caption::POSITION_BELOW);
     }
     if ($this->getProperty(Caption::POSITION_CENTER, $node)) {
         $h2->withPosition(Caption::POSITION_CENTER);
     }
     if ($this->getProperty(Caption::POSITION_ABOVE, $node)) {
         $h2->withPosition(Caption::POSITION_ABOVE);
     }
     if ($this->getProperty(Caption::ALIGN_LEFT, $node)) {
         $h2->withTextAlignment(Caption::ALIGN_LEFT);
     }
     if ($this->getProperty(Caption::ALIGN_CENTER, $node)) {
         $h2->withTextAlignment(Caption::ALIGN_CENTER);
     }
     if ($this->getProperty(Caption::ALIGN_RIGHT, $node)) {
         $h2->withTextAlignment(Caption::ALIGN_RIGHT);
     }
     $transformer->transform($h2, $node);
     return $context_element;
 }
 public function apply($transformer, $context, $node)
 {
     $interactive = Interactive::create();
     if (Type::is($context, InstantArticle::getClassName())) {
         $instant_article = $context;
     } elseif ($transformer->getInstantArticle()) {
         $instant_article = $transformer->getInstantArticle();
         $context->disableEmptyValidation();
         $context = Paragraph::create();
         $context->disableEmptyValidation();
     } else {
         $transformer->addWarning(new NoRootInstantArticleFoundWarning(null, $node));
         return $context;
     }
     // Builds the interactive
     $iframe = $this->getProperty(self::PROPERTY_IFRAME, $node);
     $url = $this->getProperty(self::PROPERTY_URL, $node);
     if ($iframe) {
         $interactive->withHTML($iframe);
     }
     if ($url) {
         $interactive->withSource($url);
     }
     if ($iframe || $url) {
         $instant_article->addChild($interactive);
         if ($instant_article !== $context) {
             $instant_article->addChild($context);
         }
     } else {
         $transformer->addWarning(new InvalidSelector(self::PROPERTY_IFRAME, $instant_article, $node, $this));
     }
     if ($this->getProperty(self::PROPERTY_WIDTH_COLUMN_WIDTH, $node)) {
         $interactive->withMargin(Interactive::COLUMN_WIDTH);
     } else {
         $interactive->withMargin(Interactive::NO_MARGIN);
     }
     $width = $this->getProperty(self::PROPERTY_WIDTH, $node);
     if ($width) {
         $interactive->withWidth($width);
     }
     $height = $this->getProperty(self::PROPERTY_HEIGHT, $node);
     if ($height) {
         $interactive->withHeight($height);
     }
     $suppress_warnings = $transformer->suppress_warnings;
     $transformer->suppress_warnings = true;
     $transformer->transform($interactive, $node);
     $transformer->suppress_warnings = $suppress_warnings;
     return $context;
 }
 public function apply($transformer, $context, $node)
 {
     $image = Image::create();
     if (Type::is($context, InstantArticle::getClassName())) {
         $instant_article = $context;
     } elseif ($transformer->getInstantArticle()) {
         $instant_article = $transformer->getInstantArticle();
         $context->disableEmptyValidation();
         $context = Paragraph::create();
         $context->disableEmptyValidation();
     } else {
         $transformer->addWarning(new NoRootInstantArticleFoundWarning(null, $node));
         return $context;
     }
     // Builds the image
     $url = $this->getProperty(self::PROPERTY_IMAGE_URL, $node);
     if ($url) {
         $image->withURL($url);
         $instant_article->addChild($image);
         if ($instant_article !== $context) {
             $instant_article->addChild($context);
         }
     } else {
         $transformer->addWarning(new InvalidSelector(self::PROPERTY_IMAGE_URL, $instant_article, $node, $this));
     }
     if ($this->getProperty(Image::ASPECT_FIT, $node)) {
         $image->withPresentation(Image::ASPECT_FIT);
     } elseif ($this->getProperty(Image::ASPECT_FIT_ONLY, $node)) {
         $image->withPresentation(Image::ASPECT_FIT_ONLY);
     } elseif ($this->getProperty(Image::FULLSCREEN, $node)) {
         $image->withPresentation(Image::FULLSCREEN);
     } elseif ($this->getProperty(Image::NON_INTERACTIVE, $node)) {
         $image->withPresentation(Image::NON_INTERACTIVE);
     }
     if ($this->getProperty(self::PROPERTY_LIKE, $node)) {
         $image->enableLike();
     }
     if ($this->getProperty(self::PROPERTY_COMMENTS, $node)) {
         $image->enableComments();
     }
     $suppress_warnings = $transformer->suppress_warnings;
     $transformer->suppress_warnings = true;
     $transformer->transform($image, $node);
     $transformer->suppress_warnings = $suppress_warnings;
     return $context;
 }
 public function getContextClass()
 {
     return InstantArticle::getClassName();
 }
Exemplo n.º 6
0
 public function testIsNotInSetException()
 {
     $this->setExpectedException('InvalidArgumentException');
     Type::enforce(Caption::create(), [InstantArticle::getClassName(), Video::getClassName(), Image::getClassName()]);
 }
 /**
  * @param InstantArticle $context
  * @param \DOMNode $node
  *
  * @return mixed
  */
 public function transform($context, $node)
 {
     if (Type::is($context, InstantArticle::getClassName())) {
         $context->addMetaProperty('op:generator:transformer', 'facebook-instant-articles-sdk-php');
         $context->addMetaProperty('op:generator:transformer:version', InstantArticle::CURRENT_VERSION);
         $this->instantArticle = $context;
     }
     $log = \Logger::getLogger('facebook-instantarticles-transformer');
     if (!$node) {
         $e = new \Exception();
         $log->error('Transformer::transform($context, $node) requires $node' . ' to be a valid one. Check on the stacktrace if this is ' . 'some nested transform operation and fix the selector.', $e->getTraceAsString());
     }
     $current_context = $context;
     if ($node->hasChildNodes()) {
         foreach ($node->childNodes as $child) {
             if (self::isProcessed($child)) {
                 continue;
             }
             $matched = false;
             $log->debug("===========================");
             $log->debug($child->ownerDocument->saveHtml($child));
             // Get all classes and interfaces this context extends/implements
             $contextClassNames = self::getAllClassTypes($context->getClassName());
             // Look for rules applying to any of them as context
             $matchingContextRules = [];
             foreach ($contextClassNames as $contextClassName) {
                 if (isset($this->rules[$contextClassName])) {
                     // Use array union (+) instead of merge to preserve
                     // indexes (as they represent the order of insertion)
                     $matchingContextRules = $matchingContextRules + $this->rules[$contextClassName];
                 }
             }
             // Sort by insertion order
             ksort($matchingContextRules);
             // Process in reverse order
             $matchingContextRules = array_reverse($matchingContextRules);
             foreach ($matchingContextRules as $rule) {
                 // We know context was matched, now check if it matches the node
                 if ($rule->matchesNode($child)) {
                     $current_context = $rule->apply($this, $current_context, $child);
                     $matched = true;
                     // Just a single rule for each node, so move on
                     break;
                 }
             }
             if (!$matched && !($child->nodeName === '#text' && trim($child->textContent) === '') && !($child->nodeName === '#comment') && !($child->nodeName === 'html' && Type::is($child, 'DOMDocumentType')) && !($child->nodeName === 'xml' && Type::is($child, 'DOMProcessingInstruction')) && !$this->suppress_warnings) {
                 $tag_content = $child->ownerDocument->saveXML($child);
                 $tag_trimmed = trim($tag_content);
                 if (!empty($tag_trimmed)) {
                     $log->debug('context class: ' . get_class($context));
                     $log->debug('node name: ' . $child->nodeName);
                     $log->debug("CONTENT NOT MATCHED: \n" . $tag_content);
                 } else {
                     $log->debug('empty content ignored');
                 }
                 $this->addWarning(new UnrecognizedElement($current_context, $child));
             }
         }
     }
     return $context;
 }