Skip to content
This repository has been archived by the owner on Apr 20, 2023. It is now read-only.

Unable to use ConstantGetter with InteractiveRule #335

Closed
m4olivei opened this issue Aug 22, 2018 · 4 comments
Closed

Unable to use ConstantGetter with InteractiveRule #335

m4olivei opened this issue Aug 22, 2018 · 4 comments

Comments

@m4olivei
Copy link
Contributor

Steps required to reproduce the problem

  1. Define a Transfomer rule to match embeds and apply InteractiveRule with constant width and height. eg.
$rules[] = [
    'class' => InteractiveRule::class,
    'selector' => '//div[@class="embed-code-other"][iframe[contains(@src, "soundcloud.com")]]',
    'properties' => [
      InteractiveRule::PROPERTY_IFRAME => [
        'type' => 'string',
        'selector' => 'iframe',
        'attribute' => 'src',
      ],
      InteractiveRule::PROPERTY_WIDTH => [
        'type' => 'constant',
        'value' => '320',
      ],
      InteractiveRule::PROPERTY_HEIGHT => [
        'type' => 'constant',
        'value' => '160',
      ],
    ],
  ];
  1. Run the transform against some HTML that includes:
<div class="embed-code-other">
  <iframe width="100%" height="300" scrolling="no" frameborder="no" allow="autoplay" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/461019930&color=%23ff5500&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false&show_teaser=true&visual=true"></iframe>
</div>

Expected Result

  • You get an op-interactive with width = 320 and height = 160

Actual Result

  • When running the transform, you get the following error:
InvalidArgumentException: Method expects this value ----[ <pre class='xdebug-var-dump' dir='ltr'> <small>/var/www/vendor/facebook/facebook-instant-articles-sdk-php/src/Facebook/InstantArticles/Validators/Type.php:174:</small><small>string</small> <font color='#cc0000'>'320'</font> <i>(length=3)</i> </pre>]---- to be one of the types ====[ <pre class='xdebug-var-dump' dir='ltr'> <small>/var/www/vendor/facebook/facebook-instant-articles-sdk-php/src/Facebook/InstantArticles/Validators/Type.php:179:</small><small>string</small> <font color='#cc0000'>'INTEGER'</font> <i>(length=7)</i> </pre>]==== in Facebook\InstantArticles\Validators\Type::throwException() (line 182 of /var/www/vendor/facebook/facebook-instant-articles-sdk-php/src/Facebook/InstantArticles/Validators/Type.php).

Additional comments

The problem here appears to be that the ConstantGetter requires a string value, whereas the Interactive element requires an integer.

I'm currently working around this by defining my own ConstantGetter extending the Facebook ConstantGetter:

<?php

namespace Drupal\ls_fb_instant_articles\Facebook\InstantArticles\Transformer\Getters;

use Facebook\InstantArticles\Transformer\Getters\ConstantGetter as FacebookConstantGetter;
use Facebook\InstantArticles\Validators\Type;

/**
 * Extend the Facebook ConstantGetter to allow string and integer constants.
 */
class ConstantGetter extends FacebookConstantGetter {

  /**
   * {@inheritdoc}
   */
  public function withValue($value) {
    Type::enforce($value, [
      Type::STRING,
      Type::INTEGER,
    ]);
    $this->value = $value;

    return $this;
  }

}
@everton-rosario
Copy link
Contributor

Thanks for that @m4olivei .
Do you think if we add that type enforcing thing into our base code would be enough?
You can send that piece of code as PR.

@m4olivei
Copy link
Contributor Author

@everton-rosario Yeah totally, just making ConstantGetter more liberal like my override class would do it. Sure, I'll file a quick PR.

m4olivei added a commit to m4olivei/facebook-instant-articles-sdk-php that referenced this issue Aug 22, 2018
@m4olivei
Copy link
Contributor Author

@everton-rosario I don't have time to dig in to a point where I could add a test, so I'll leave it there for now, feel free to add a test if you think it needs it.

everton-rosario added a commit that referenced this issue Aug 31, 2018
Issue #335 Allow constant getter to accept integers as well as strings
@everton-rosario
Copy link
Contributor

Sounds good @m4olivei

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants