| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * File containing the DoctrineDatabase base field value Handler class. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @copyright Copyright (C) eZ Systems AS. All rights reserved. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @license For full copyright and license information view LICENSE file distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | namespace eZ\Publish\Core\Search\Legacy\Content\Common\Gateway\CriterionHandler\FieldValue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use eZ\Publish\Core\Persistence\Database\DatabaseHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use eZ\Publish\API\Repository\Values\Content\Query\Criterion; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use eZ\Publish\API\Repository\Values\Content\Query\Criterion\Operator as CriterionOperator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use eZ\Publish\Core\Persistence\Database\SelectQuery; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use eZ\Publish\Core\Persistence\TransformationProcessor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use RuntimeException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * Content locator gateway implementation using the DoctrineDatabase. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | abstract class Handler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * DB handler to fetch additional field information. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @var \eZ\Publish\Core\Persistence\Database\DatabaseHandler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     protected $dbHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * Map of criterion operators to the respective function names | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * in the DoctrineDatabase DBAL. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     protected $comparatorMap = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         CriterionOperator::EQ => 'eq', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         CriterionOperator::GT => 'gt', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         CriterionOperator::GTE => 'gte', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         CriterionOperator::LT => 'lt', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         CriterionOperator::LTE => 'lte', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         CriterionOperator::LIKE => 'like', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * Transformation processor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @var \eZ\Publish\Core\Persistence\TransformationProcessor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     protected $transformationProcessor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * Creates a new criterion handler. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @param \eZ\Publish\Core\Persistence\Database\DatabaseHandler $dbHandler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @param \eZ\Publish\Core\Persistence\TransformationProcessor $transformationProcessor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     public function __construct(DatabaseHandler $dbHandler, TransformationProcessor $transformationProcessor) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $this->dbHandler = $dbHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $this->transformationProcessor = $transformationProcessor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * Generates query expression for operator and value of a Field Criterion. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @throws \RuntimeException If operator is not handled. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @param \eZ\Publish\Core\Persistence\Database\SelectQuery $query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @param string $column | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @return \eZ\Publish\Core\Persistence\Database\Expression | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     public function handle(SelectQuery $query, Criterion $criterion, $column) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $column = $this->dbHandler->quoteColumn($column); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         switch ($criterion->operator) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             case Criterion\Operator::IN: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 $filter = $query->expr->in( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                     $column, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                     array_map(array($this, 'lowercase'), $criterion->value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             case Criterion\Operator::BETWEEN: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                 $filter = $query->expr->between( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                     $column, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                     $query->bindValue($this->lowercase($criterion->value[0])), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                     $query->bindValue($this->lowercase($criterion->value[1])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             case Criterion\Operator::EQ: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             case Criterion\Operator::GT: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             case Criterion\Operator::GTE: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             case Criterion\Operator::LT: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             case Criterion\Operator::LTE: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             case Criterion\Operator::LIKE: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 $operatorFunction = $this->comparatorMap[$criterion->operator]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                 $filter = $query->expr->$operatorFunction( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                     $column, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                     $query->bindValue($this->lowercase($criterion->value)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             case Criterion\Operator::CONTAINS: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                 $filter = $query->expr->like( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                     $column, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                     $query->bindValue( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                         '%' . $this->prepareLikeString($criterion->value) . '%' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                 throw new RuntimeException("Unknown operator '{$criterion->operator}' for Field criterion handler."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         return $filter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * Returns the given $string prepared for use in SQL LIKE clause. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      * LIKE clause wildcards '%' and '_' contained in the given $string will be escaped. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * @param $string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     protected function prepareLikeString($string) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         return addcslashes($this->lowercase($string), '%_'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      * Downcases a given string using string transformation processor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      * @param string $string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 144 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 145 |  |  |     protected function lowerCase($string) | 
            
                                                        
            
                                    
            
            
                | 146 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 147 |  |  |         return $this->transformationProcessor->transformByGroup($string, 'lowercase'); | 
            
                                                        
            
                                    
            
            
                | 148 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 149 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 150 |  |  |  |