vendor/pimcore/pimcore/bundles/EcommerceFrameworkBundle/PricingManager/Condition/Bracket.php line 73

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore\Bundle\EcommerceFrameworkBundle\PricingManager\Condition;
  15. use Pimcore\Bundle\EcommerceFrameworkBundle\Factory;
  16. use Pimcore\Bundle\EcommerceFrameworkBundle\PricingManager\ConditionInterface;
  17. use Pimcore\Bundle\EcommerceFrameworkBundle\PricingManager\EnvironmentInterface;
  18. class Bracket implements BracketInterface
  19. {
  20.     /**
  21.      * @var ConditionInterface[]
  22.      */
  23.     protected $conditions = [];
  24.     /**
  25.      * @var string[] BracketInterface::OPERATOR_*
  26.      */
  27.     protected $operator = [];
  28.     /**
  29.      * @param ConditionInterface $condition
  30.      * @param string $operator BracketInterface::OPERATOR_*
  31.      *
  32.      * @return $this
  33.      */
  34.     public function addCondition(ConditionInterface $condition$operator)
  35.     {
  36.         $this->conditions[] = $condition;
  37.         $this->operator[] = $operator;
  38.         return $this;
  39.     }
  40.     /**
  41.      * @param EnvironmentInterface $environment
  42.      *
  43.      * @return bool
  44.      */
  45.     public function check(EnvironmentInterface $environment)
  46.     {
  47.         // A bracket without conditions is not restricted and thus doesn't fail
  48.         if (empty($this->conditions)) {
  49.             return true;
  50.         }
  51.         // default
  52.         $state null;
  53.         // check all conditions
  54.         foreach ($this->conditions as $num => $condition) {
  55.             //The first condition shouldn't have an operator.
  56.             //https://github.com/pimcore/pimcore/pull/7902
  57.             $operator $this->operator[$num];
  58.             if ($num === 0) {
  59.                 $operator null;
  60.             }
  61.             // test condition
  62.             $check $condition->check($environment);
  63.             // check
  64.             switch ($operator) {
  65.                 // first condition
  66.                 case null:
  67.                     $state $check;
  68.                     break;
  69.                     // AND
  70.                 case BracketInterface::OPERATOR_AND:
  71.                     if ($check === false) {
  72.                         return false;
  73.                     }
  74.                     //consider current state with check, if not default.
  75.                     $state $state ?? true;
  76.                     break;
  77.                     // AND FALSE
  78.                 case BracketInterface::OPERATOR_AND_NOT:
  79.                     if ($check === true) {
  80.                         return false;
  81.                     }
  82.                     //consider current state with check, if not default.
  83.                     $state $state ?? true;
  84.                     break;
  85.                     // OR
  86.                 case BracketInterface::OPERATOR_OR:
  87.                     if ($check === true) {
  88.                         $state $check;
  89.                     }
  90.                     break;
  91.             }
  92.         }
  93.         return $state ?? false;
  94.     }
  95.     /**
  96.      * @return string
  97.      */
  98.     public function toJSON()
  99.     {
  100.         $json = ['type' => 'Bracket''conditions' => []];
  101.         foreach ($this->conditions as $num => $condition) {
  102.             $cond = [
  103.                 'operator' => $this->operator[$num],
  104.                 'condition' => json_decode($condition->toJSON()),
  105.             ];
  106.             $json['conditions'][] = $cond;
  107.         }
  108.         return json_encode($json);
  109.     }
  110.     /**
  111.      * @param string $string
  112.      *
  113.      * @throws \Pimcore\Bundle\EcommerceFrameworkBundle\Exception\InvalidConfigException
  114.      *
  115.      * @return $this
  116.      */
  117.     public function fromJSON($string)
  118.     {
  119.         $json json_decode($string);
  120.         foreach ($json->conditions as $setting) {
  121.             $subcond Factory::getInstance()->getPricingManager()->getCondition($setting->type);
  122.             $subcond->fromJSON(json_encode($setting));
  123.             $this->addCondition($subcond$setting->operator);
  124.         }
  125.         return $this;
  126.     }
  127.     /**
  128.      * @param string $typeClass
  129.      *
  130.      * @return ConditionInterface[]
  131.      */
  132.     public function getConditionsByType(string $typeClass): array
  133.     {
  134.         $conditions = [];
  135.         foreach ($this->conditions as $condition) {
  136.             if ($condition instanceof BracketInterface) {
  137.                 $conditions array_merge($condition->getConditionsByType($typeClass));
  138.             } elseif ($condition instanceof $typeClass) {
  139.                 $conditions[] = $condition;
  140.             }
  141.         }
  142.         return $conditions;
  143.     }
  144. }