You are here

OrderConditionBase.php in Ubercart 8.4

File

uc_order/src/Plugin/Condition/OrderConditionBase.php
View source
<?php

namespace Drupal\uc_order\Plugin\Condition;

use Drupal\Core\Database\Connection;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\rules\Core\RulesConditionBase;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Base class containing useful functions for some Order conditions.
 */
abstract class OrderConditionBase extends RulesConditionBase implements ContainerFactoryPluginInterface {

  /**
   * The database service.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * Constructs an OrderConditionBase object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin ID for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Database\Connection $database
   *   The database service.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $database) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->database = $database;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('database'));
  }

  /**
   * Operator options callback.
   *
   * @return string[]
   *   An array of logic operations for multiple role matching.
   */
  public function comparisonOptions() {
    return [
      'less' => $this
        ->t('Total is less than specified value.'),
      'less_equal' => $this
        ->t('Total is less than or equal to specified value.'),
      'equal' => $this
        ->t('Total is equal to specified value.'),
      'greater_equal' => $this
        ->t('Total is greater than or equal to specified value.'),
      'greater' => $this
        ->t('Total is greater than specified value.'),
    ];
  }

  /**
   * Value comparison.
   *
   * @param float $source
   *   The source value.
   * @param string $operator
   *   The comparison operator.
   * @param float $target
   *   The target value.
   *
   * @return bool
   *   Whether the comparison meets the specified conditions.
   */
  public function compareComparisonOptions($source, $operator, $target) {
    switch ($operator) {
      case 'less':
        return $source < $target;
      case 'less_equal':
        return $source <= $target;
      case 'equal':
        return $source == $target;
      case 'greater_equal':
        return $source >= $target;
      case 'greater':
        return $source > $target;
    }
  }

}

Classes

Namesort descending Description
OrderConditionBase Base class containing useful functions for some Order conditions.