facebookarchive / facebook-instant-articles-sdk-extensions-in-php

Facebook Instant Articles SDK Extensions in PHP.
https://www.facebook.com/facebookmedia/blog/instant-articles-sdk-extension-now-supports-google-amp-soon-apple-news
Other
45 stars 48 forks source link

Adding a filter (for example IA_IMAGE) throws an exception. #26

Open rbraun75 opened 7 years ago

rbraun75 commented 7 years ago

Problem noticed

Adding a filter as described on https://developers.facebook.com/docs/instant-articles/other-formats throws an Exception:

InvalidArgumentException: Method expects this value ----[ NULL ]---- to be one of the types ====[ string(10) "DOMElement" ]==== in Facebook\InstantArticles\Validators\Type::throwException() (line 182 of [...]/vendor/facebook/facebook-instant-articles-sdk-php/src/Facebook/InstantArticles/Validators/Type.php).
Facebook\InstantArticles\Validators\Type::is(NULL, 'DOMElement', 1) (Line: 39)
Facebook\InstantArticles\Validators\Type::enforce(NULL, 'DOMElement') (Line: 589)
Facebook\InstantArticles\AMP\AMPContext->addItem(NULL) (Line: 444)
Facebook\InstantArticles\AMP\AMPArticle->transformArticleContent(Object) (Line: 264)
Facebook\InstantArticles\AMP\AMPArticle->transformInstantArticle(Object) (Line: 189)
Facebook\InstantArticles\AMP\AMPArticle->toDOMElement(Object) (Line: 38)
Facebook\InstantArticles\Elements\Element->render('', 1) (Line: 145)
Facebook\InstantArticles\AMP\AMPArticle->render() (Line: 67)

Code:

$amp_article = AMPArticle::create($instant_article_html, $properties);
$amp_article->getObserver()->addFilter(
    'IA_IMAGE',
    function (
        /*DOMElement*/ $amp_image,
        /*Facebook\InstantArticles\Elements\Image*/ $ia_image,
        /*AMPContext*/ $context
    ) {
        // filter code
    },
    10,
    3
);
$amp_string = $amp_article->render();

Expected result

Adding a filter throws no exception and filter is executed.