You are here

abstract class RulesDataProcessor in Rules 7.2

Common base class for Rules data processors.

Hierarchy

Expanded class hierarchy of RulesDataProcessor

File

includes/rules.processor.inc, line 15
Contains classes for data processing.

View source
abstract class RulesDataProcessor {

  /**
   * The processors' setting value.
   */
  protected $setting = NULL;

  /**
   * Allows chaining processors. If set, the next processor to invoke.
   */
  protected $processor = NULL;

  /**
   * Constructor.
   */
  protected function __construct($setting, $param_info, $var_info = array(), $processor = NULL) {
    $this->setting = $setting;
    $this->processor = $processor;
  }

  /**
   * Return $this or skip this processor by returning the next processor.
   */
  protected function getPreparedValue() {
    return isset($this->setting) && array_filter($this->setting) ? $this : $this->processor;
  }

  /**
   * Determines whether the current user has permission to edit this chain of
   * data processors.
   *
   * @return bool
   *   Whether the current user has permission to edit this chain of data
   *   processors.
   */
  public function editAccess() {
    return $this
      ->access() && (!isset($this->processor) || $this->processor
      ->editAccess());
  }

  /**
   * Prepares the processor for parameters.
   *
   * It turns the settings into a suitable processor object, which gets invoked
   * on evaluation time.
   *
   * @param $setting
   *   The processor settings which are to be prepared.
   * @param $param_info
   *   The info about the parameter to prepare the processor for.
   * @param array $var_info
   *   An array of info about the available variables.
   */
  public static function prepareSetting(&$setting, $param_info, $var_info = array()) {
    $processor = NULL;
    foreach (self::processors($param_info, FALSE) as $name => $info) {
      if (!empty($setting[$name])) {
        $object = new $info['class']($setting[$name], $param_info, $var_info, $processor);
        $processor = $object
          ->getPreparedValue();
      }
    }
    $setting = $processor;
  }

  /**
   * Attaches the form of applicable data processors.
   */
  public static function attachForm(&$form, $settings, $param_info, $var_info, $access_check = TRUE) {

    // If $settings is already prepared get the settings from the processors.
    if ($settings instanceof RulesDataProcessor) {
      $settings = $settings
        ->getChainSettings();
    }
    foreach (self::processors($param_info, $access_check) as $name => $info) {
      $settings += array(
        $name => array(),
      );
      $form[$name] = call_user_func(array(
        $info['class'],
        'form',
      ), $settings[$name], $var_info);
      $form[$name]['#weight'] = $info['weight'];
    }
  }

  /**
   * Returns defined data processors applicable for the given parameter.
   *
   * Optionally also checks access to the processors.
   *
   * @param $param_info
   *   If given, only processors valid for this parameter are returned.
   * @param bool $access_check
   * @param string $hook
   */
  public static function processors($param_info = NULL, $access_check = TRUE, $hook = 'data_processor_info') {
    static $items = array();
    if (!isset($items[$hook]['all'])) {
      $items[$hook]['all'] = rules_fetch_data($hook);
      if (isset($items[$hook]['all'])) {
        uasort($items[$hook]['all'], array(
          __CLASS__,
          '_item_sort',
        ));
      }
    }

    // Data processing isn't supported for multiple types.
    if (isset($param_info) && is_array($param_info['type'])) {
      return array();
    }

    // Filter the items by type.
    if (isset($param_info['type']) && !isset($items[$hook][$param_info['type']])) {
      $items[$hook][$param_info['type']] = array();
      foreach ($items[$hook]['all'] as $name => $info) {

        // Check whether the parameter type matches the supported types.
        $info += array(
          'type' => 'text',
        );
        if (RulesData::typesMatch($param_info, $info, FALSE)) {
          $items[$hook][$param_info['type']][$name] = $info;
        }
      }
    }

    // Apply the access check.
    $return = isset($param_info['type']) ? $items[$hook][$param_info['type']] : $items[$hook]['all'];
    if ($access_check) {
      foreach ($return as $base => $info) {
        if (!call_user_func(array(
          $info['class'],
          'access',
        ))) {
          unset($return[$base]);
        }
      }
    }
    return $return;
  }
  public static function _item_sort($a, $b) {
    return $a['weight'] < $b['weight'] ? -1 : ($a['weight'] > $b['weight'] ? 1 : 0);
  }

  /**
   * Gets the settings array for this and all contained chained processors.
   */
  public function getChainSettings() {
    foreach ($this
      ->unchain() as $name => $processor) {
      $settings[$name] = $processor
        ->getSetting();
    }
    return isset($settings) ? $settings : array();
  }

  /**
   * Returns an array of modules which we depend on.
   */
  public function dependencies() {
    $used_processor_info = array_intersect_key($this
      ->processors(), $this
      ->unchain());
    $modules = array();
    foreach ($used_processor_info as $name => $info) {
      $modules[] = $info['module'];
    }
    return array_filter($modules);
  }

  /**
   * @return
   *   An array of processors keyed by processor name.
   */
  protected function unchain() {
    $processor = $this;
    while ($processor instanceof RulesDataProcessor) {
      $processors[get_class($processor)] = $processor;
      $processor = $processor->processor;
    }

    // Note: Don't use the static context to call processors() here as we need a
    // late binding to invoke the input evaluators version, if needed.
    $return = array();
    foreach ($this
      ->processors() as $name => $info) {
      if (isset($processors[$info['class']])) {
        $return[$name] = $processors[$info['class']];
      }
    }
    return $return;
  }

  /**
   * Gets the settings of this processor.
   */
  public function getSetting() {
    return $this->setting;
  }

  /**
   * Processes the value.
   *
   * If $this->processor is set, invoke this processor first so chaining
   * multiple processors is working.
   *
   * @param $value
   *   The value to process.
   * @param $info
   *   Info about the parameter for which we process the value.
   * @param RulesState $state
   *   The rules evaluation state.
   * @param RulesPlugin $element
   *   The element for which we process the value.
   *
   * @return
   *   The processed value.
   */
  public abstract function process($value, $info, RulesState $state, RulesPlugin $element);

  /**
   * Return whether the current user has permission to use the processor.
   *
   * @return bool
   *   Whether the current user has permission to use the processor.
   */
  public static function access() {
    return TRUE;
  }

  /**
   * Defines the processor form element.
   *
   * @param $settings
   *   The settings of the processor.
   * @param array $var_info
   *   An array of info about the available variables.
   *
   * @return
   *   A form element structure.
   */
  protected static function form($settings, $var_info) {
    return array();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RulesDataProcessor::$processor protected property Allows chaining processors. If set, the next processor to invoke.
RulesDataProcessor::$setting protected property The processors' setting value.
RulesDataProcessor::access public static function Return whether the current user has permission to use the processor. 1
RulesDataProcessor::attachForm public static function Attaches the form of applicable data processors. 1
RulesDataProcessor::dependencies public function Returns an array of modules which we depend on.
RulesDataProcessor::editAccess public function Determines whether the current user has permission to edit this chain of data processors. 2
RulesDataProcessor::form protected static function Defines the processor form element. 3
RulesDataProcessor::getChainSettings public function Gets the settings array for this and all contained chained processors.
RulesDataProcessor::getPreparedValue protected function Return $this or skip this processor by returning the next processor. 1
RulesDataProcessor::getSetting public function Gets the settings of this processor.
RulesDataProcessor::prepareSetting public static function Prepares the processor for parameters. 1
RulesDataProcessor::process abstract public function Processes the value. 4
RulesDataProcessor::processors public static function Returns defined data processors applicable for the given parameter. 1
RulesDataProcessor::unchain protected function
RulesDataProcessor::_item_sort public static function
RulesDataProcessor::__construct protected function Constructor. 1