| 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 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * Class ConditionsProcessor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * Processes Axessors conditions. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @package NoOne4rever\Axessors | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | class ConditionsProcessor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** @var string conditions for setter */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     private $inputConditions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** @var string conditions for getter */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     private $outputConditions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** @var string class namespace */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     private $namespace; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * ConditionsProcessor constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @param string $in input conditions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @param string $out output conditions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param string $namespace class namespace | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public function __construct(string $in, string $out, string $namespace) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $this->inputConditions = $in; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $this->outputConditions = $out; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $this->namespace = $namespace; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * Creates list of conditions for input data. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @return string[] conditions | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     public function processInputConditions(): array | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         return $this->makeConditionsTree($this->inputConditions); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * Creates list of conditions for output data. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @return string[] conditions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     public function processOutputConditions(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         return $this->makeConditionsTree($this->outputConditions); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * Creates list of conditions from a string of conditions definition. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @param string $conditions conditions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @return array conditions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     private function explodeConditions(string $conditions): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         if ($conditions === '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $result = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $injProcessor = new InjectedStringSuit($conditions); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $conditions = $injProcessor->addSlashes('|&'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $conditions = preg_split('{\s*\|\|\s*}', $conditions); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         foreach ($conditions as $condition) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             $result[] = preg_split('{\s*&&\s*}', $condition); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         return $this->rmSlashes($result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * Removes slashes from conditions. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @param array $conditions conditions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @return array processed conditions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     private function rmSlashes(array $conditions): array  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         foreach ($conditions as $number => &$complexCondition) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             if (is_array($complexCondition)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                 foreach ($complexCondition as $num => &$condition) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                     $condition = stripcslashes($condition); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 $complexCondition = stripcslashes($complexCondition); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         return $conditions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * Makes tree of conditions. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @param string $conditions string with conditions definition | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @return array tree of conditions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     private function makeConditionsTree(string $conditions): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $result = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         $conditions = $this->explodeConditions($conditions); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         foreach ($conditions as $number => $condition) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             foreach ($condition as $token) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |                 $injProcessor = new InjectedStringSuit($token); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                 if (count($condition) === 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                     $result[] = $injProcessor->resolveClassNames($this->namespace); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                     $result[$number][] = $token; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         return $result; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 123 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 124 |  |  | } |