You are here

class Group in Googalytics - Google Analytics 8

Class Drupal\ga\AnalyticsCommand\Group.

Hierarchy

Expanded class hierarchy of Group

File

src/AnalyticsCommand/Group.php, line 8

Namespace

Drupal\ga\AnalyticsCommand
View source
class Group implements DrupalSettingCommandsInterface, GroupInterface, \IteratorAggregate {
  use DrupalSettingCommandsTrait;
  const DEFAULT_PRIORITY = 0;

  /**
   * A key to identify this group.
   *
   * @var string
   */
  protected $groupKey;

  /**
   * The commands within this group.
   *
   * @var array[DrupalSettingsCommandsInterface]
   */
  protected $commands;

  /**
   * AnalyticsCommandGroup constructor.
   *
   * @param string $key
   *   The group name.
   * @param int $priority
   *   The group priority.
   */
  public function __construct($key, $priority = self::DEFAULT_PRIORITY) {
    $this->groupKey = $key;
    $this->priority = $priority;
    $this->commands = [];
  }

  /**
   * Get the key identifying this group.
   *
   * @return string
   *   The group key.
   */
  public function getGroupKey() {
    return $this->groupKey;
  }

  /**
   * Add a command to the group.
   *
   * @param \Drupal\ga\AnalyticsCommand\DrupalSettingCommandsInterface $command
   *   A command.
   */
  public function addCommand(DrupalSettingCommandsInterface $command) {
    $this->commands[] = $command;
  }

  /**
   * {@inheritdoc}
   */
  public function getSettingCommands() {
    usort($this->commands, function (DrupalSettingCommandsInterface $a, DrupalSettingCommandsInterface $b) {
      return $b
        ->getPriority() - $a
        ->getPriority();
    });
    return array_reduce($this->commands, function ($carry, DrupalSettingCommandsInterface $item) {
      return array_merge($carry, $item
        ->getSettingCommands());
    }, []);
  }

  /**
   * Retrieve an external iterator.
   *
   * @link http://php.net/manual/en/iteratoraggregate.getiterator.php
   *
   * @return \ArrayIterator
   *   An iterator for the group's commands.
   */
  public function getIterator() {
    return new \ArrayIterator($this->commands);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DrupalSettingCommandsTrait::$priority protected property Priority integer.
DrupalSettingCommandsTrait::getPriority public function An integer value for sorting by priority.
Group::$commands protected property The commands within this group.
Group::$groupKey protected property A key to identify this group.
Group::addCommand public function Add a command to the group.
Group::DEFAULT_PRIORITY constant
Group::getGroupKey public function Get the key identifying this group. Overrides GroupInterface::getGroupKey
Group::getIterator public function Retrieve an external iterator.
Group::getSettingCommands public function An array of commands to be sent to Google Analytics. Overrides DrupalSettingCommandsTrait::getSettingCommands
Group::__construct public function AnalyticsCommandGroup constructor.