| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Cerbero\Dto; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use ArrayAccess; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Cerbero\Dto\Traits\HasFlags; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Cerbero\Dto\Traits\HasProperties; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Cerbero\Dto\Traits\HasValues; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Cerbero\Dto\Traits\ManipulatesData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Cerbero\Dto\Traits\TurnsIntoArray; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Cerbero\Dto\Traits\TurnsIntoString; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use IteratorAggregate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use JsonSerializable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Serializable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * The data transfer object. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | abstract class Dto implements IteratorAggregate, ArrayAccess, Serializable, JsonSerializable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     use HasProperties; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     use HasValues; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     use HasFlags; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     use ManipulatesData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     use TurnsIntoArray; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     use TurnsIntoString; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * Instantiate the class. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param array $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param int $flags | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 181 |  |     public function __construct(array $data = [], int $flags = NONE) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 181 |  |         $this->flags = $this->mergeFlags(static::getDefaultFlags(), $flags); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 181 |  |         $this->propertiesMap = $this->mapData($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 181 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * Retrieve the merged flags | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param int $initialFlags | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @param int $flagsToMerge | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @throws Exceptions\IncompatibleDtoFlagsException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 181 |  |     protected function mergeFlags(int $initialFlags, int $flagsToMerge): int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 181 |  |         return (new DtoFlagsHandler())->merge($initialFlags, $flagsToMerge); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * Retrieve the DTO mapped properties for the given data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @param array $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @throws Exceptions\DtoNotFoundException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @throws Exceptions\InvalidDocCommentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @throws Exceptions\MissingValueException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @throws Exceptions\UnexpectedValueException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 181 |  |     protected function mapData(array $data): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 181 |  |         return DtoPropertiesMapper::for(static::class)->map($data, $this->getFlags()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * Retrieve an instance of DTO | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @param array $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @param int $flags | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @return self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 163 |  |     public static function make(array $data = [], int $flags = NONE): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 163 |  |         return new static($data, $flags); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * Retrieve a clone of the DTO | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @return self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 18 |  |     public function clone(): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 18 |  |         return clone $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * Retrieve the serialized DTO | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 95 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 96 | 3 |  |     public function serialize(): string | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 98 | 3 |  |         return serialize([ | 
            
                                                                        
                            
            
                                    
            
            
                | 99 | 3 |  |             $this->toArray(), | 
            
                                                                        
                            
            
                                    
            
            
                | 100 | 3 |  |             $this->getFlags(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * Retrieve the unserialized DTO | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @param mixed $serialized | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 3 |  |     public function unserialize($serialized): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 3 |  |         [$data, $flags] = unserialize($serialized); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 3 |  |         $this->__construct($data, $flags); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * Determine how to clone the DTO | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 18 |  |     public function __clone() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 18 |  |         foreach ($this->propertiesMap as &$property) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 18 |  |             $property = clone $property; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 18 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 128 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 129 |  |  |  |