You are here

class Transformer in Facebook Instant Articles 8.2

Same name and namespace in other branches
  1. 3.x src/Transformer.php \Drupal\fb_instant_articles\Transformer

Encapsulates Drupal-specific logic when using the Transformer class.

Hierarchy

  • class \Drupal\fb_instant_articles\Transformer extends \Facebook\InstantArticles\Transformer\Transformer

Expanded class hierarchy of Transformer

3 files declare their use of Transformer
FieldItemListNormalizer.php in src/Normalizer/FieldItemListNormalizer.php
SubtitleFormatter.php in src/Plugin/Field/FieldFormatter/SubtitleFormatter.php
TransformerFormatter.php in src/Plugin/Field/FieldFormatter/TransformerFormatter.php
1 string reference to 'Transformer'
fb_instant_articles.services.yml in ./fb_instant_articles.services.yml
fb_instant_articles.services.yml
1 service uses Transformer
fb_instant_articles.transformer in ./fb_instant_articles.services.yml
Drupal\fb_instant_articles\Transformer

File

src/Transformer.php, line 11

Namespace

Drupal\fb_instant_articles
View source
class Transformer extends FbiaTransformer {

  /**
   * Transformer rules manager service.
   *
   * @var \Drupal\fb_instant_articles\TransformerRulesManager
   */
  protected $transformerRulesManager;

  /**
   * Transformer constructor.
   *
   * Wraps the Transformer object from the SDK to introduce a default set of
   * rules any time it's instantiated, including an opportunity for other
   * modules to alter the set of rules that are used.
   *
   * Note the parent class does not have a constructor, so we do not call
   * @code parent::__construct() @endcode.
   *
   * @param \Drupal\fb_instant_articles\TransformerRulesManager $transformer_rules_manager
   *   Transformer rules manager service.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   Config factory service.
   *
   * @throws \ReflectionException
   *   In case the class specified by one of the loaded $rules cannot be found.
   *
   * @see hook_fb_instant_articles_transformer_rules_alter()
   */
  public function __construct(TransformerRulesManager $transformer_rules_manager, ConfigFactoryInterface $config_factory) {
    parent::__construct();
    $this->transformerRulesManager = $transformer_rules_manager;
    $this
      ->addRules($this->transformerRulesManager
      ->getRules());

    // Override the default timezone according to the site wide timezone.
    $config_data_default_timezone = $config_factory
      ->get('system.date')
      ->get('timezone.default');
    $default_time_zone = !empty($config_data_default_timezone) ? $config_data_default_timezone : @date_default_timezone_get();
    $this
      ->setDefaultDateTimeZone(new \DateTimeZone($default_time_zone));
  }

  /**
   * Adds rules from an array of rule information.
   *
   * @param array $rules
   *   An array of transformer rule arrays. This is a PHP array representation
   *   of the JSON list of Rules information expected by parent::loadRules().
   *
   * @throws \ReflectionException
   *   In case the class specified by one of the given $rules cannot be found.
   *
   * @see Transformer::__construct()
   * @see \Facebook\InstantArticles\Transformer\Transformer::loadRules()
   */
  public function addRules(array $rules) {
    foreach ($rules as $rule) {
      $class = $rule['class'];
      try {
        $factory_method = new \ReflectionMethod($class, 'createFrom');
      } catch (\ReflectionException $e) {
        $factory_method = new \ReflectionMethod('Facebook\\InstantArticles\\Transformer\\Rules\\' . $class, 'createFrom');
      }
      $this
        ->addRule($factory_method
        ->invoke(NULL, $rule));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Transformer::$transformerRulesManager protected property Transformer rules manager service.
Transformer::addRules public function Adds rules from an array of rule information.
Transformer::__construct public function Transformer constructor.