| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Cerbero\Dto\Traits; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use ArrayIterator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Cerbero\Dto\Exceptions\ImmutableDtoException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Cerbero\Dto\Exceptions\UnknownDtoPropertyException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Cerbero\Dto\Exceptions\UnsetDtoPropertyException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Cerbero\Dto\Manipulators\ArrayConverter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Traversable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use const Cerbero\Dto\MUTABLE; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * Trait to turn a DTO into an array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | trait TurnsIntoArray | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * Retrieve the DTO as an array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 54 |  |     public function toArray(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 54 |  |         $data = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 54 |  |         foreach ($this->getPropertiesMap() as $name => $property) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 54 |  |             $data[$name] = ArrayConverter::instance()->convert($property->value()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 54 |  |         return $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * Retrieve the DTO as an array with snake-case keys | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 3 |  |     public function toSnakeCaseArray(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 3 |  |         $data = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 3 |  |         foreach ($this->getPropertiesMap() as $name => $property) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 3 |  |             $key = strtolower(preg_replace(ArrayConverter::RE_SNAKE_CASE, '_', $name)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 3 |  |             $data[$key] = ArrayConverter::instance()->convert($property->value(), true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 3 |  |         return $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * Retrieve the DTO as an iterator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @return Traversable | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 57 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 58 | 15 |  |     public function getIterator(): Traversable | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 60 | 15 |  |         return new ArrayIterator($this->toArray(), ArrayIterator::ARRAY_AS_PROPS); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * Determine whether a given property has a value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @param mixed $property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 18 |  |     public function offsetExists($property): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 18 |  |         return $this->has($property); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * Retrieve the given property value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @param mixed $property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @throws UnknownDtoPropertyException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 33 |  |     public function &offsetGet($property) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 33 |  |         $value = $this->get($property); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 27 |  |         return $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * Set the given property to the provided value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @param mixed $property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @param mixed $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @throws ImmutableDtoException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * @throws UnknownDtoPropertyException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 12 |  |     public function offsetSet($property, $value): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 12 |  |         if (!($this->getFlags() & MUTABLE)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 6 |  |             throw new ImmutableDtoException(static::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 6 |  |         $this->set($property, $value); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 6 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * Set the given property to the provided value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * @param mixed $property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * @throws ImmutableDtoException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * @throws UnsetDtoPropertyException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * @throws UnknownDtoPropertyException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 9 |  |     public function offsetUnset($property): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 9 |  |         if (!($this->getFlags() & MUTABLE)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 3 |  |             throw new ImmutableDtoException(static::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 6 |  |         $this->unset($property); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 3 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 123 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 124 |  |  |  |