| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Mado\QueryBundle\Queries; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Doctrine\ORM\EntityManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Mado\QueryBundle\Services\StringParser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class Join | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     private const AND_OPERATOR_LOGIC = 'AND'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     private const OR_OPERATOR_LOGIC = 'OR'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     private $parser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     private $entityName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     private $entityAlias; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     private $manager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private $innerJoin; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private $leftJoin; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     private $joins; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     private $relationEntityAlias; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 25 |  |     public function __construct(string $entityName, string $entityAlias, EntityManager $manager) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 25 |  |         $this->parser  = new StringParser(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 25 |  |         $this->entityName = $entityName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 25 |  |         $this->entityAlias = $entityAlias; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 25 |  |         $this->manager = $manager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 25 |  |         $this->innerJoin = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 25 |  |         $this->leftJoin = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 25 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @param String $relation Nome della relazione semplice (groups.name) o con embedded (_embedded.groups.name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 11 |  |     public function join(String $relation, $logicOperator = self::AND_OPERATOR_LOGIC) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 11 |  |         $relation = explode('|', $relation)[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 11 |  |         $relations = [$relation]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 11 |  |         if (strstr($relation, '_embedded.')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 11 |  |             $embeddedFields = explode('.', $relation); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 11 |  |             $this->parser->camelize($embeddedFields[1]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             // elimino l'ultimo elemento che dovrebbe essere il nome del campo | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 11 |  |             unset($embeddedFields[count($embeddedFields) - 1]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             // elimino il primo elemento _embedded | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 11 |  |             unset($embeddedFields[0]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 11 |  |             $relations = $embeddedFields; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 11 |  |         $entityName = $this->entityName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 11 |  |         $entityAlias = $this->entityAlias; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 11 |  |         foreach ($relations as $relation) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 11 |  |             $relation = $this->parser->camelize($relation); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 11 |  |             $relationEntityAlias = 'table_' . $relation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 11 |  |             $metadata = $this->manager->getClassMetadata($entityName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 11 |  |             if ($metadata->hasAssociation($relation)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 11 |  |                 $association = $metadata->getAssociationMapping($relation); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 11 |  |                 $fieldName = $this->parser->camelize($association['fieldName']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 11 |  |                 if ($this->noExistsJoin($relationEntityAlias, $relation)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 11 |  |                     if ($logicOperator === self::AND_OPERATOR_LOGIC) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 6 |  |                         $param = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 6 |  |                         $param['field'] = $entityAlias . "." . $fieldName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 6 |  |                         $param['relation'] = $relationEntityAlias; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 6 |  |                         $this->innerJoin[] = $param; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 5 |  |                     } elseif ($logicOperator === self::OR_OPERATOR_LOGIC) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 5 |  |                         $param = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 5 |  |                         $param['field'] = $entityAlias . "." . $fieldName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 5 |  |                         $param['relation'] = $relationEntityAlias; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 5 |  |                         $this->leftJoin[] = $param; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                     } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                         throw new \Exception('Missing Logic operator'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 11 |  |                     $this->storeJoin($relationEntityAlias, $relation); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 11 |  |                 $entityName = $association['targetEntity']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 11 |  |                 $entityAlias = $relationEntityAlias; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 11 |  |             $this->setRelationEntityAlias($relationEntityAlias); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 11 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 11 |  |     private function storeJoin($prevEntityAlias, $currentEntityAlias) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 11 |  |         $needle = $prevEntityAlias . '_' . $currentEntityAlias; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 11 |  |         $this->joins[$needle] = $needle; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 11 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 11 |  |     private function noExistsJoin($prevEntityAlias, $currentEntityAlias) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 11 |  |         if (null === $this->joins) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 11 |  |             $this->joins = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 11 |  |         $needle = $prevEntityAlias . '_' . $currentEntityAlias; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 11 |  |         return !in_array($needle, $this->joins); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 13 |  |     public function getInnerJoin() :array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 13 |  |         return $this->innerJoin; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 12 |  |     public function getLeftJoin() :array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 12 |  |         return $this->leftJoin; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 11 |  |     private function setRelationEntityAlias(string $relationEntityAlias) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 11 |  |         $this->relationEntityAlias = $relationEntityAlias; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 11 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 136 | 11 |  |     public function getRelationEntityAlias() | 
            
                                                        
            
                                    
            
            
                | 137 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 138 | 11 |  |         return $this->relationEntityAlias; | 
            
                                                        
            
                                    
            
            
                | 139 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 140 |  |  | } |