| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * This file is a part of "Axessors" library. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @author <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @license GPL | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | namespace NoOne4rever\Axessors; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use NoOne4rever\Axessors\Exceptions\TypeError; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * Class ConditionsSuit. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * Processes Axessors conditions. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @package NoOne4rever\Axessors | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | class ConditionsSuit extends RunningSuit | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** @var string method name */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     private $method; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * ConditionsSuit constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @param int $mode mode of execution | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @param PropertyData $data property data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @param string $class class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @param string $method method name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param object|null $object object | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     public function __construct(int $mode, PropertyData $data, string $class, string $method, $object = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         parent::__construct($mode, $data, $class, $object); | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         $this->method = $method; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * Checks the conditions defined in the Axessors comment. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * Creates logical tree of the conditions and then checks if the general result is true. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @param $value mixed value of the property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @return bool result of checking of the conditions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     public function processConditions($value): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $conditions = $this->calculateConditions($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         if (empty($conditions)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             return array_reduce($conditions, function ($carry, $item) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 return $this->reduce($carry, $item, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * Reduces conditions tree and returns the result. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @param mixed $carry previous item | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @param mixed $item item | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @param bool $mode mode | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @return bool the result of reducing | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     private function reduce($carry, $item, bool $mode): bool  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         if ($item === $mode || $carry === $mode) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             return $mode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         } elseif (is_array($item)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             return array_reduce($item, function ($carry, $item) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                 return $this->reduce($carry, $item, false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             return !$mode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * Calculates every condition defined in the Axessors comment. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @param $value mixed value of the property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @return bool[] results of the conditions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     private function calculateConditions($value): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $calculatedConditions = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $conditions = $this->mode == RunningSuit::OUTPUT_MODE ? $this->propertyData->getOutputConditions() : $this->propertyData->getInputConditions(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         foreach ($conditions as $number => $complexCondition) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             if (is_array($complexCondition)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                 foreach ($complexCondition as $condition) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                     $calculatedConditions[$number][] = $this->executeCondition($condition, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                 $calculatedConditions[$number] = $this->executeCondition($complexCondition, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         return $calculatedConditions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * Calculates a condition defined in the Axessors comment. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @param $condition string the condition | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @param $value mixed value of the property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * @return bool result of the condition | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     private function executeCondition(string $condition, $value): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         if (strpos($condition, '`') !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             return $this->executeInjectedString($condition, $value, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             return $this->runStandardCondition($condition, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * Runs Axessors condition. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * @param string $condition condition | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * @param mixed $value value to check  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * @return bool the result of the checkout | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     private function runStandardCondition(string $condition, $value): bool  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         $value = $this->count($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         if (strpos($condition, '..') !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             $condition = explode('..', $condition); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             $condition = "<= {$condition[1]} && $value >= {$condition[0]}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         return eval("return $value $condition;"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      * Casts the property to integer. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      * If the property is string or array returns it's length. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      * If the property is integer of float returns the property itself. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      * @param $value mixed value of the property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      * @return int integer value of the property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      * @throws TypeError if the property can't be turned into integer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     private function count($value): int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         switch (gettype($value)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             case 'integer': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |             case 'float': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |             case 'string': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |                 $value = strlen($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |             case 'array': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |                 $value = count($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |             default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |                 throw new TypeError('value "' . var_export($value, true) . "\" passed to {$this->class}::{$this->method}() is not countable"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         return $value; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 162 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 163 |  |  | } |