| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This software package is licensed under AGPL or Commercial license. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @package maslosoft/mangan | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @licence AGPL or Commercial | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @copyright Copyright (c) Piotr Masełkowski <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @copyright Copyright (c) Maslosoft | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @copyright Copyright (c) Others as mentioned in code | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @link https://maslosoft.com/mangan/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | namespace Maslosoft\Mangan\Criteria; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Maslosoft\Addendum\Interfaces\AnnotatedInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Maslosoft\Mangan\Criteria; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * Conditions builder helper class. This exposes fluid interface for building complex queries. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * Building complex queries can lead to (semi) manually constructing deeply nested array of conditions, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * which is both unreadable and unreliable, espacially when user with `$or`, `$and`, `$not` etc. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * This class is meant to overcome this issue, by providing fluid, cascading interface. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * Example for simple query, let's find `visits` larger than 30 and lesser than 100: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * ```php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * $conditions = new Conditions($model) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  * $conditions->visits->gt(30)->lt(100); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  * ``` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  * However real improvement comes when used with more complex query. In this example we | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  * search for `visits` greater than 10 and lesser than 200 or greater than 100 and | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  * lesser than 200, where `active` is true: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  * ```php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  * $c1 = new Conditions($model); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  * $c1->visits->gt(10)->lt(20); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  * $c2->visits->gt(100)->lt(200); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  * $condtions = new Conditions($model); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  * $conditions->active = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  * $conditions->or($c1, $c2); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  * ``` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  * In above example mongodb conditions array is quite verbose and depth, however | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  * with `Conditions` class it is fairly clean and easy to create it. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  * When conditions setup is finished, this should be passed to `Criteria` by `setCriteria`: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  * ```php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  * $criteria = new Criteria(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  * $criteria->setconditions($conditions); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  * ``` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  * Or alternatively it can be passed directly into constructor: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  * ``` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  * $criteria = new Criteria($conditions); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  * ``` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  * @author Piotr Maselkowski <pmaselkowski at gmail.com> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | class Conditions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	const FieldName = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	const Operator = 2; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 	const Value = 3; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	private $model = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	private $criteria = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	private $field = ''; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  | 	public function __construct(AnnotatedInterface $model) | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  | 	{ | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  | 		$this->criteria = new Criteria(null, $model); | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 	public function __get($name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 		$this->field = $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	public function __set($name, $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 		$this->field = $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 		$this->eq($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	public function eq($value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 		$this->criteria->addCond($this->field, 'eq', $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 	public function gt($value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 		$this->criteria->addCond($this->field, 'gt', $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 	public function gte($value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 		$this->criteria->addCond($this->field, 'gte', $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 	public function lt($value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 		$this->criteria->addCond($this->field, 'lt', $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 	public function lte($value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 		$this->criteria->addCond($this->field, 'lte', $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 	public function ne($value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 		$this->criteria->addCond($this->field, 'ne', $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 	public function in(array $values) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 	public function notIn(array $values) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 	 * @param Conditions|Conditions[] $conditions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 	 * @return Conditions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 	public function addOr($conditions) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 	public function addAnd($value) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 	public function addNot($value) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 	public function addNor($value) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  | 	public function get() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  | 		return $this->criteria->getConditions(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 172 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 173 |  |  |  | 
            
                        
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.