| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the FOSElasticaBundle project. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Infinite Networks Pty Ltd <http://www.infinite.net.au> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace FOS\ElasticaBundle\Index; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use FOS\ElasticaBundle\Configuration\IndexConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use FOS\ElasticaBundle\Configuration\TypeConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class MappingBuilder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * Skip adding default information to certain fields. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private $skipTypes = array('completion'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * Builds mappings for an entire index. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @param IndexConfig $indexConfig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 2 |  |     public function buildIndexMapping(IndexConfig $indexConfig) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 2 |  |         $typeMappings = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 2 |  |         foreach ($indexConfig->getTypes() as $typeConfig) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 2 |  |             $typeMappings[$typeConfig->getName()] = $this->buildTypeMapping($typeConfig); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 2 |  |         $mapping = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 2 |  |         if (!empty($typeMappings)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 2 |  |             $mapping['mappings'] = $typeMappings; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         // 'warmers' => $indexConfig->getWarmers(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 2 |  |         $settings = $indexConfig->getSettings(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 2 |  |         if (!empty($settings)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 1 |  |             $mapping['settings'] = $settings; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 2 |  |         return $mapping; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * Builds mappings for a single type. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @param TypeConfig $typeConfig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 3 |  |     public function buildTypeMapping(TypeConfig $typeConfig) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 3 |  |         $mapping = $typeConfig->getMapping(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 3 |  |         if (null !== $typeConfig->getDynamicDateFormats()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 3 |  |             $mapping['dynamic_date_formats'] = $typeConfig->getDynamicDateFormats(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 3 |  |         if (null !== $typeConfig->getDateDetection()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 2 |  |             $mapping['date_detection'] = $typeConfig->getDateDetection(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 3 |  |         if (null !== $typeConfig->getNumericDetection()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 2 |  |             $mapping['numeric_detection'] = $typeConfig->getNumericDetection(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 3 |  |         if ($typeConfig->getIndexAnalyzer()) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 1 |  |             $mapping['index_analyzer'] = $typeConfig->getIndexAnalyzer(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 3 |  |         if ($typeConfig->getSearchAnalyzer()) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 2 |  |             $mapping['search_analyzer'] = $typeConfig->getSearchAnalyzer(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 3 |  |         if ($typeConfig->getDynamic() !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 2 |  |             $mapping['dynamic'] = $typeConfig->getDynamic(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 3 |  |         if (isset($mapping['dynamic_templates']) and empty($mapping['dynamic_templates'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 3 |  |             unset($mapping['dynamic_templates']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 2 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $this->fixProperties($mapping['properties']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 3 |  |         if (!$mapping['properties']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 1 |  |             unset($mapping['properties']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 3 |  |         if ($typeConfig->getModel()) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             $mapping['_meta']['model'] = $typeConfig->getModel(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         if (empty($mapping)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 3 |  |             // Empty mapping, we want it encoded as a {} instead of a [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             $mapping = new \stdClass(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         return $mapping; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * Fixes any properties and applies basic defaults for any field that does not have | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 3 |  |      * required options. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 3 |  |      * @param $properties | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 115 | 3 |  |      */ | 
            
                                                        
            
                                    
            
            
                | 116 |  |  |     private function fixProperties(&$properties) | 
            
                                                        
            
                                    
            
            
                | 117 | 3 |  |     { | 
            
                                                        
            
                                    
            
            
                | 118 | 3 |  |         foreach ($properties as $name => &$property) { | 
            
                                                        
            
                                    
            
            
                | 119 |  |  |             unset($property['property_path']); | 
            
                                                        
            
                                    
            
            
                | 120 | 3 |  |  | 
            
                                                        
            
                                    
            
            
                | 121 |  |  |             if (!isset($property['type'])) { | 
            
                                                        
            
                                    
            
            
                | 122 |  |  |                 $property['type'] = 'string'; | 
            
                                                        
            
                                    
            
            
                | 123 | 3 |  |             } | 
            
                                                        
            
                                    
            
            
                | 124 | 2 |  |             if ($property['type'] == 'multi_field' && isset($property['fields'])) { | 
            
                                                        
            
                                    
            
            
                | 125 |  |  |                 $this->fixProperties($property['fields']); | 
            
                                                        
            
                                    
            
            
                | 126 | 3 |  |             } | 
            
                                                        
            
                                    
            
            
                | 127 | 2 |  |             if (isset($property['properties'])) { | 
            
                                                        
            
                                    
            
            
                | 128 |  |  |                 $this->fixProperties($property['properties']); | 
            
                                                        
            
                                    
            
            
                | 129 | 3 |  |             } | 
            
                                                        
            
                                    
            
            
                | 130 | 3 |  |             if (in_array($property['type'], $this->skipTypes)) { | 
            
                                                        
            
                                    
            
            
                | 131 |  |  |                 continue; | 
            
                                                        
            
                                    
            
            
                | 132 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 133 | 3 |  |         } | 
            
                                                        
            
                                    
            
            
                | 134 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 135 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 136 |  |  |  | 
            
                        
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: