| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Sanderdekroon\Parlant\Builder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use BadMethodCallException; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 7 |  |  | class NestedTaxonomy | 
            
                                                                        
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  |     protected $query = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  |     protected $taxonomy; | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |     protected $relation; | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |      * Used to determine if the developer has set a default taxonomy name. | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |      * @var bool | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     private $hasDefaultTaxonomy; | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 | 1 |  |     public function __construct($taxonomyName = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 | 1 |  |         $this->taxonomy = $taxonomyName; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 23 | 1 |  |         $this->hasDefaultTaxonomy = empty($taxonomyName) ? false : true; | 
            
                                                                        
                            
            
                                    
            
            
                | 24 | 1 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      * Base method for creating a tax_query within a nested context. | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      * @param  string  $taxonomy        Name of the taxonomy. | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * @param  string  $field           The field to query (term_id, name, etc.) | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * @param  string  $operator        The operator to compare the value to (IN, NOT In, etc.) | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * @param  mixed   $value           The value of the term to query. Can be a string or an integer. | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      * @param  bool    $includeChildren Whether or not to include children in hierarchical taxonomies. | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      * @param  string  $relation        The relation between different subqueries within the same nested query. | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      * @param  integer $level           Depth level. You should not modify this yourself. | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      * @return $this | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 37 | 1 |  |     protected function where($taxonomy, $field, $operator = null, $value = null, $includeChildren = true, $relation = null, $level = 2) | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 39 | 1 |  |         if (func_num_args() == 3 || is_null($value)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 40 | 1 |  |             $value = $operator; | 
            
                                                                        
                            
            
                                    
            
            
                | 41 | 1 |  |             $operator = 'IN'; | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 1 |  |         $this->setQuery(compact( | 
            
                                                                        
                            
            
                                    
            
            
                | 45 | 1 |  |             'taxonomy', | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 1 |  |             'field', | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 1 |  |             'operator', | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 1 |  |             'value', | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 1 |  |             'includeChildren', | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 1 |  |             'level' | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         )); | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 53 | 1 |  |         if (!empty($relation)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |             $this->setRelation($relation); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 1 |  |         return $this; | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |      * Magically call additional helpers on a nested taxonomy query. Accepted methods are named | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |      * after the field they're querying. For example: slug(), name(), etc. This method also | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |      * figures out if the developer has set a default taxonomy that we should injext. | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |      * @param  string $methodname       The methodname the developer is calling. | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |      * @param  array  $arguments        See $this->where for accepted arguments. | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |      * @return $this | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 68 | 1 |  |     public function __call($methodname, $arguments) | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |         $fieldsToMethods = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 71 | 1 |  |             'slug'              => 'slug', | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |             'name'              => 'name', | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |             'termTaxonomyId'    => 'term_taxonomy_id', | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |             'id'                => 'term_id', | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |         // If the developer is calling a method which we have not defined, | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |         // throw a BadMethodCall exception since we can't recover from this. | 
            
                                                                        
                            
            
                                    
            
            
                | 79 | 1 |  |         if (!in_array($methodname, array_keys($fieldsToMethods))) { | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |             throw new BadMethodCallException('Invalid method called on NestedTaxonomy.'); | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |              | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |         // If the developer has not set a default taxonomy we'll grab the first  argument | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |         // from the method that's being called. We will assume it's the taxonomy name | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |         // we are looking for and set this value to the class property taxonomy. | 
            
                                                                        
                            
            
                                    
            
            
                | 86 | 1 |  |         if ($this->hasDefaultTaxonomy === false) { | 
            
                                                                        
                            
            
                                    
            
            
                | 87 | 1 |  |             $this->taxonomy = reset($arguments); | 
            
                                                                        
                            
            
                                    
            
            
                | 88 | 1 |  |             unset($arguments[0]); | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |          | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |         // Construct a where call with the gathered variables. | 
            
                                                                        
                            
            
                                    
            
            
                | 92 | 1 |  |         return $this->where( | 
            
                                                                        
                            
            
                                    
            
            
                | 93 | 1 |  |             $this->taxonomy, | 
            
                                                                        
                            
            
                                    
            
            
                | 94 | 1 |  |             $fieldsToMethods[$methodname], | 
            
                                                                        
                            
            
                                    
            
            
                | 95 | 1 |  |             ...$arguments | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |     public function orWhere($taxonomy, $field, $operator = null, $value = null, $includeChildren = true) | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |         $this->setRelation('OR'); | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |         return $this->where($taxonomy, $field, $operator, $value, $includeChildren); | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 107 | 1 |  |     public function relation($relation) | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 109 | 1 |  |         $this->setRelation($relation); | 
            
                                                                        
                            
            
                                    
            
            
                | 110 | 1 |  |         return $this; | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 114 | 1 |  |     protected function setQuery($query) | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 116 | 1 |  |         $this->query[] = $query; | 
            
                                                                        
                            
            
                                    
            
            
                | 117 | 1 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 120 | 1 |  |     public function getQuery() | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 122 | 1 |  |         return $this->query; | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 126 | 1 |  |     protected function setRelation($relation) | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 128 | 1 |  |         $this->relation = $relation; | 
            
                                                                        
                            
            
                                    
            
            
                | 129 | 1 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 1 |  |     public function getRelation() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 134 | 1 |  |         return empty($this->relation) ? 'AND' : $this->relation; | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 | 1 |  |     public function replaceQuery($query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 140 | 1 |  |         $this->query = $query; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 141 | 1 |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 142 |  |  | } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 143 |  |  |  |