| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | namespace ReadModel\Walker; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use ReadModel\InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | final class WalkerBuilder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     /** @var BinaryUuidTransformer */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     private $transformer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     /** @var ResultWalker[] */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     private $walkers = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** @var callable[] */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     private $callables = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** @var array */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     private $prefixes = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** @var array */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     private $binaryKeys = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** @var array */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     private $scalarMapping = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** @var bool */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     private $camelCase = false; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 31 | 2 |  |     public function __construct(BinaryUuidTransformer $transformer = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 33 | 2 |  |         $this->transformer = $transformer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 2 |  |     public function addWalker(ResultWalker $walker): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 2 |  |         $this->walkers[] = $walker; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 2 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * Embed walker is added as the first walker. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 2 |  |     public function withEmbedded(...$prefixes): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 2 |  |         $this->prefixes = array_merge($this->prefixes, $prefixes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 2 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * Callable walkers are added after embed walker. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 2 |  |     public function with(callable $callable): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 2 |  |         $this->callables[] = $callable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 2 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * Binary casting is added after callable walkers. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 2 |  |     public function withBinaryUuidCasting(...$keys): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 2 |  |         if ($this->transformer === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             throw InvalidArgumentException::binaryUuidTransformerMustBeProvided(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 2 |  |         $this->binaryKeys = array_merge($this->binaryKeys, $keys); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 2 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * Scalar casting is added after binary casting walker. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 2 |  |     public function withScalarCasting(array $mapping): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 2 |  |         $this->scalarMapping = array_merge($this->scalarMapping, $mapping); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 2 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * Camel cased walker is added as the last walker. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 2 |  |     public function withCamelCasedFieldNames(): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 2 |  |         $this->camelCase = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 2 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 2 |  |     public function build(): ResultWalker | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 2 |  |         if (!empty($this->prefixes)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 2 |  |             $this->addWalker(new EmbedWalker(...$this->prefixes)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 2 |  |         foreach ($this->callables as $callable) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 2 |  |             $this->addWalker(new CallableWalker($callable)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 2 |  |         if (!empty($this->binaryKeys)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 2 |  |             $this->addWalker(new BinaryUuidTransformerWalker($this->transformer, ...$this->binaryKeys)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 2 |  |         if (!empty($this->scalarMapping)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 2 |  |             $this->addWalker(new ScalarTransformerWalker($this->scalarMapping)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 2 |  |         if ($this->camelCase) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 2 |  |             $this->addWalker(new KeysToCamelCaseWalker()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 2 |  |         $walker = new ChainWalker(...$this->walkers); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 2 |  |         $this->clearBuilder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 2 |  |         return $walker; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 2 |  |     private function clearBuilder(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 2 |  |         $this->walkers = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 2 |  |         $this->callables = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 2 |  |         $this->prefixes = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 2 |  |         $this->binaryKeys = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 2 |  |         $this->scalarMapping = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 2 |  |         $this->camelCase = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 2 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 129 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 130 |  |  |  |