| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Chubbyphp\Serialization\Mapping; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Chubbyphp\Serialization\Accessor\PropertyAccessor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Chubbyphp\Serialization\Normalizer\CallbackFieldNormalizer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Chubbyphp\Serialization\Normalizer\DateTimeFieldNormalizer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Chubbyphp\Serialization\Normalizer\FieldNormalizer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Chubbyphp\Serialization\Normalizer\FieldNormalizerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Chubbyphp\Serialization\Normalizer\Relation\EmbedManyFieldNormalizer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Chubbyphp\Serialization\Normalizer\Relation\EmbedOneFieldNormalizer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Chubbyphp\Serialization\Normalizer\Relation\ReferenceManyFieldNormalizer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Chubbyphp\Serialization\Normalizer\Relation\ReferenceOneFieldNormalizer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Chubbyphp\Serialization\Policy\NullPolicy; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Chubbyphp\Serialization\Policy\PolicyInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | final class NormalizationFieldMappingBuilder implements NormalizationFieldMappingBuilderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     private $groups = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @var FieldNormalizerInterface|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     private $fieldNormalizer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @var PolicyInterface|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     private $policy; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 9 |  |     private function __construct(string $name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 9 |  |         $this->name = $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 9 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @return NormalizationFieldMappingBuilderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 2 |  |     public static function create(string $name): NormalizationFieldMappingBuilderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 2 |  |         return new self($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @param string   $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @param callable $callback | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @return NormalizationFieldMappingBuilderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 1 |  |     public static function createCallback(string $name, callable $callback): NormalizationFieldMappingBuilderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 1 |  |         $self = new self($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 1 |  |         $self->fieldNormalizer = new CallbackFieldNormalizer($callback); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 1 |  |         return $self; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @param string $format | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @return NormalizationFieldMappingBuilderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 2 |  |     public static function createDateTime(string $name, string $format = 'c'): NormalizationFieldMappingBuilderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 2 |  |         $self = new self($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 2 |  |         $self->fieldNormalizer = new DateTimeFieldNormalizer(new PropertyAccessor($name), $format); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 2 |  |         return $self; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @return NormalizationFieldMappingBuilderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 1 |  |     public static function createEmbedMany(string $name): NormalizationFieldMappingBuilderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 1 |  |         $self = new self($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 1 |  |         $self->fieldNormalizer = new EmbedManyFieldNormalizer(new PropertyAccessor($name)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 1 |  |         return $self; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * @return NormalizationFieldMappingBuilderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 1 |  |     public static function createEmbedOne(string $name): NormalizationFieldMappingBuilderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 1 |  |         $self = new self($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 1 |  |         $self->fieldNormalizer = new EmbedOneFieldNormalizer(new PropertyAccessor($name)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 1 |  |         return $self; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * @param string $idName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * @return NormalizationFieldMappingBuilderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 119 | 1 | View Code Duplication |     public static function createReferenceMany( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         string $name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         string $idName = 'id' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     ): NormalizationFieldMappingBuilderInterface { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 1 |  |         $self = new self($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 1 |  |         $self->fieldNormalizer = new ReferenceManyFieldNormalizer( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 1 |  |             new PropertyAccessor($idName), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 1 |  |             new PropertyAccessor($name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 1 |  |         return $self; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      * @param string $idName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      * @return NormalizationFieldMappingBuilderInterface | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 137 |  |  |      */ | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 138 | 1 | View Code Duplication |     public static function createReferenceOne( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  |         string $name, | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |         string $idName = 'id' | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |     ): NormalizationFieldMappingBuilderInterface { | 
            
                                                                        
                            
            
                                    
            
            
                | 142 | 1 |  |         $self = new self($name); | 
            
                                                                        
                            
            
                                    
            
            
                | 143 | 1 |  |         $self->fieldNormalizer = new ReferenceOneFieldNormalizer( | 
            
                                                                        
                            
            
                                    
            
            
                | 144 | 1 |  |             new PropertyAccessor($idName), | 
            
                                                                        
                            
            
                                    
            
            
                | 145 | 1 |  |             new PropertyAccessor($name) | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 148 | 1 |  |         return $self; | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      * @deprecated | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |      * @param array $groups | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |      * @return NormalizationFieldMappingBuilderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 | 1 |  |     public function setGroups(array $groups): NormalizationFieldMappingBuilderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 | 1 |  |         $this->groups = $groups; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 | 1 |  |         return $this; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      * @param FieldNormalizerInterface $fieldNormalizer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |      * @return NormalizationFieldMappingBuilderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 | 1 |  |     public function setFieldNormalizer( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |         FieldNormalizerInterface $fieldNormalizer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |     ): NormalizationFieldMappingBuilderInterface { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 | 1 |  |         $this->fieldNormalizer = $fieldNormalizer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 | 1 |  |         return $this; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |      * @param PolicyInterface $policy | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |      * @return NormalizationFieldMappingBuilderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |     public function setPolicy(PolicyInterface $policy): NormalizationFieldMappingBuilderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |         $this->policy = $policy; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |      * @return NormalizationFieldMappingInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 | 9 |  |     public function getMapping(): NormalizationFieldMappingInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 | 9 |  |         return new NormalizationFieldMapping( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 | 9 |  |             $this->name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 | 9 |  |             $this->groups, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 | 9 |  |             $this->fieldNormalizer ?? new FieldNormalizer(new PropertyAccessor($this->name)), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 | 9 |  |             $this->policy ?? new NullPolicy() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 202 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 203 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.