PHP Facebook\InstantArticles\Elements InstantArticle - 17 примеров найдено. Это лучшие примеры PHP кода для Facebook\InstantArticles\Elements\InstantArticle, полученные из open source проектов. Вы можете ставить оценку каждому примеру, чтобы помочь нам улучшить качество примеров.
/**
* 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;
}
public function testInstantArticle()
{
$article = InstantArticle::create()->withCanonicalUrl('')->withHeader(Header::create())->addChild(Paragraph::create()->appendText('Some text to be within a paragraph for testing.'))->addChild(Paragraph::create())->addChild(Paragraph::create()->appendText(" \n \t "))->addChild(Image::create())->addChild(Image::create()->withURL(''))->addChild(SlideShow::create()->addImage(Image::create()->withURL('https://jpeg.org/images/jpegls-home.jpg'))->addImage(Image::create()))->addChild(Ad::create()->withSource('http://foo.com'))->addChild(Paragraph::create()->appendText('Other text to be within a second paragraph for testing.'))->addChild(Analytics::create())->withFooter(Footer::create());
$expected = '<!doctype html>' . '<html>' . '<head>' . '<link rel="canonical" href=""/>' . '<meta charset="utf-8"/>' . '<meta property="op:generator" content="facebook-instant-articles-sdk-php"/>' . '<meta property="op:generator:version" content="' . InstantArticle::CURRENT_VERSION . '"/>' . '<meta property="op:markup_version" content="v1.0"/>' . '</head>' . '<body>' . '<article>' . '<p>Some text to be within a paragraph for testing.</p>' . '<figure class="op-slideshow">' . '<figure>' . '<img src="https://jpeg.org/images/jpegls-home.jpg"/>' . '</figure>' . '</figure>' . '<figure class="op-ad">' . '<iframe src="http://foo.com"></iframe>' . '</figure>' . '<p>Other text to be within a second paragraph for testing.</p>' . '</article>' . '</body>' . '</html>';
$result = $article->render();
$this->assertEquals($expected, $result);
$warnings = InstantArticleValidator::check($article);
$this->assertEquals(9, count($warnings));
}
/**
* @param string|DOMDocument $document The document html of an Instant Article
*
* @return InstantArticle filled element that was parsed from the DOMDocument parameter
*/
public function parse($content)
{
if (Type::is($content, Type::STRING)) {
libxml_use_internal_errors(true);
$document = new \DOMDocument();
$document->loadHTML($content);
libxml_use_internal_errors(false);
} else {
$document = $content;
}
$json_file = file_get_contents(__DIR__ . '/instant-articles-rules.json');
$instant_article = InstantArticle::create();
$transformer = new Transformer();
$transformer->loadRules($json_file);
$transformer->transform($instant_article, $document);
return $instant_article;
}
public function testTransformerCustomHTML()
{
$json_file = file_get_contents(__DIR__ . '/custom-html-rules.json');
$instant_article = InstantArticle::create();
$transformer = new Transformer();
$transformer->loadRules($json_file);
$html_file = file_get_contents(__DIR__ . '/custom.html');
libxml_use_internal_errors(true);
$document = new \DOMDocument();
$document->loadHTML($html_file);
libxml_use_internal_errors(false);
$instant_article->withCanonicalURL('http://localhost/article')->withHeader(Header::create()->withTitle('Peace on <b>earth</b>')->addAuthor(Author::create()->withName('bill'))->withPublishTime(Time::create(Time::PUBLISHED)->withDatetime(\DateTime::createFromFormat('j-M-Y G:i:s', '12-Apr-2016 19:46:51'))));
$transformer->transform($instant_article, $document);
$instant_article->addMetaProperty('op:generator:version', '1.0.0');
$instant_article->addMetaProperty('op:generator:transformer:version', '1.0.0');
$result = $instant_article->render('', true) . "\n";
$expected = file_get_contents(__DIR__ . '/custom-html-ia.xml');
$this->assertEquals($expected, $result);
// there must be 3 warnings related to <img> inside <li> that is not supported by IA
$this->assertEquals(3, count($transformer->getWarnings()));
}
public function testSelfTransformerNonUTF8Content()
{
$json_file = file_get_contents('src/Facebook/InstantArticles/Parser/instant-articles-rules.json');
$instant_article = InstantArticle::create();
$transformer = new Transformer();
$transformer->loadRules($json_file);
$html_file = file_get_contents(__DIR__ . '/instant-article-example-nonutf8.html');
$transformer->transformString($instant_article, $html_file, 'euc-jp');
$instant_article->withCanonicalURL('http://foo.com/article.html');
$instant_article->addMetaProperty('op:generator:version', '1.0.0');
$instant_article->addMetaProperty('op:generator:transformer:version', '1.0.0');
$result = $instant_article->render('', true) . "\n";
// some fragments are written as html entities even after transformed so
// noralize all strings to html entities and compare them.
$this->assertEquals(mb_convert_encoding($html_file, 'HTML-ENTITIES', 'euc-jp'), mb_convert_encoding($result, 'HTML-ENTITIES', 'utf-8'));
}
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;
}