| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of YaEtl | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  *     (c) Fabrice de Stefanis / https://github.com/fab2s/YaEtl | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * This source file is licensed under the MIT license which you will | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * find in the LICENSE file or at https://opensource.org/licenses/MIT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | namespace fab2s\YaEtl\Transformers\Arrays; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use DateTimeImmutable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use DateTimeZone; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use fab2s\NodalFlow\NodalFlowException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use fab2s\YaEtl\Transformers\TransformerAbstract; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class DateFormatTransformer extends TransformerAbstract | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     const DEFAULT_TIMEZONE = 'UTC'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @var array<string,array<string, string|null>> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      *                                               'key_name' => 'date_format' // Y-m-d => DateTimeImmutable instance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      *                                               or: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      *                                               'key_name' => ['from' => 'date_format', 'to' => 'date_format|null'] // to defaults to from | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     protected $setUp = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @var DateTimeZone | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     protected $dateTimeZoneFrom; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @var DateTimeZone | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     protected $dateTimeZoneTo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @var array<string,string|array<string,string>> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      *                                                'key_name' => 'date_format' // Y-m-d => DateTimeImmutable instance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      *                                                or: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      *                                                'key_name' => ['from' => 'date_format', 'to' => 'date_format'] // to defaults to from | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @param DateTimeZone|null $dateTimeZoneFrom | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param DateTimeZone|null $dateTimeZoneTo | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @throws NodalFlowException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |     public function __construct(array $setup, DateTimeZone $dateTimeZoneFrom = null, DateTimeZone $dateTimeZoneTo = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         parent::__construct(); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         $this->initSetup($setup); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         $this->dateTimeZoneFrom = $dateTimeZoneFrom ?: new DateTimeZone(static::DEFAULT_TIMEZONE); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         $this->dateTimeZoneTo   = $dateTimeZoneTo ?: new DateTimeZone(static::DEFAULT_TIMEZONE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     public function exec($param = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         foreach ($this->setUp as $key => $dateFormat) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             $param[$key] = DateTimeImmutable::createFromFormat($dateFormat['from'], $param[$key], $this->dateTimeZoneFrom) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 ->setTimezone($this->dateTimeZoneTo); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             if ($dateFormat['to']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 $param[$key] = $param[$key]->format($dateFormat['to']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         return $param; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @param array<string,string|array<string,string>> $setup | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     protected function initSetup(array $setup): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         foreach ($setup as $key => $dateFormat) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             $formatTo = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             if (is_array($dateFormat)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                 $formatFrom = $dateFormat['from']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                 $formatTo   = $dateFormat['to'] ?? $formatFrom; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                 $formatFrom = $dateFormat; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             $this->setUp[$key] = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                 'from' => $formatFrom, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 'to'   => $formatTo, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 97 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |  |