| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Loevgaard\DandomainFoundation\Entity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Loevgaard\DandomainDateTime\DateTimeImmutable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Money\Money; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Zend\Hydrator\ClassMethods; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Zend\Hydrator\HydratorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | abstract class AbstractEntity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      * This will hold the conversions applied when calling hydrate. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     protected $hydrateConversions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * This will hold the conversions applied when calling extract. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     protected $extractConversions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @param array $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @param bool  $useConversions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @param bool  $scalarsOnly    If true, it will only hydrate scalars, i.e. floats, integers, strings, booleans, dates, and money | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 39 |  |     public function hydrate(array $data, bool $useConversions = false, $scalarsOnly = true) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 39 |  |         $hydrator = $this->getHydrator(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 39 |  |         if ($scalarsOnly) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 39 |  |             foreach ($data as $key => $val) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 39 |  |                 if (!is_scalar($val) && !($val instanceof \DateTimeInterface) && !($val instanceof Money)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 39 |  |                     unset($data[$key]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 39 |  |         if ($useConversions && is_array($this->hydrateConversions)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 39 |  |             $data = $this->convert($data, $this->hydrateConversions); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 36 |  |         $hydrator->hydrate($data, $this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 36 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 6 |  |     public function extract(bool $useConversions = false): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 6 |  |         $hydrator = $this->getHydrator(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 6 |  |         $data = $hydrator->extract($this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 6 |  |         if ($useConversions && is_array($this->extractConversions)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 3 |  |             $data = $this->convert($data, $this->extractConversions); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 6 |  |         return $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 63 | 42 |  |     protected function convert(array $data, $conversions): array | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 65 | 42 |  |         foreach ($conversions as $from => $to) { | 
            
                                                                        
                            
            
                                    
            
            
                | 66 | 42 |  |             if (isset($data[$to])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 67 | 3 |  |                 throw new \InvalidArgumentException('The $to argument in the $data object is already set'); | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 70 | 39 |  |             $tmp = $data[$from] ?? null; | 
            
                                                                        
                            
            
                                    
            
            
                | 71 | 39 |  |             if ($tmp) { | 
            
                                                                        
                            
            
                                    
            
            
                | 72 | 36 |  |                 unset($data[$from]); | 
            
                                                                        
                            
            
                                    
            
            
                | 73 | 39 |  |                 $data[$to] = $tmp; | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 77 | 39 |  |         return $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 21 |  |     protected function getDateTimeFromJson($val = null): ?DateTimeImmutable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 21 |  |         if (!$val) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 3 |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 18 |  |         if ($val instanceof DateTimeImmutable) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 6 |  |             return $val; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 12 |  |         return DateTimeImmutable::createFromJson($val); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @return HydratorInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 45 |  |     protected function getHydrator(): HydratorInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 45 |  |         return new ClassMethods(false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 100 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |  |