| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Zenstruck\Foundry; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Faker; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @author Kevin Bond <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class Factory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     /** @var Configuration|null */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     private static $configuration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     private $class; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** @var callable|null */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     private $instantiator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** @var bool */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private $persist = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** @var array<array|callable> */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     private $attributeSet = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** @var callable[] */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     private $beforeInstantiate = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** @var callable[] */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     private $afterInstantiate = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** @var callable[] */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     private $afterPersist = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param array|callable $defaultAttributes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 597 |  |     public function __construct(string $class, $defaultAttributes = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 597 |  |         $this->class = $class; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 597 |  |         $this->attributeSet[] = $defaultAttributes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 597 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param array|callable $attributes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @return Proxy|object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 572 |  |     final public function create($attributes = []): Proxy | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         // merge the factory attribute set with the passed attributes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 572 |  |         $attributeSet = \array_merge($this->attributeSet, [$attributes]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         // normalize each attribute set and collapse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 572 |  |         $attributes = \array_merge(...\array_map([$this, 'normalizeAttributes'], $attributeSet)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 572 |  |         foreach ($this->beforeInstantiate as $callback) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 16 |  |             $attributes = $callback($attributes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 16 |  |             if (!\is_array($attributes)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 8 |  |                 throw new \LogicException('Before Instantiate event callback must return an array.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         // filter each attribute to convert proxies and factories to objects | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 564 |  |         $attributes = \array_map( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             function($value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 524 |  |                 return $this->normalizeAttribute($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 564 |  |             }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 564 |  |             $attributes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         // instantiate the object with the users instantiator or if not set, the default instantiator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 564 |  |         $object = ($this->instantiator ?? self::configuration()->instantiator())($attributes, $this->class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 564 |  |         foreach ($this->afterInstantiate as $callback) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 8 |  |             $callback($object, $attributes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 564 |  |         $proxy = new Proxy($object); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 564 |  |         if (!$this->persist) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 144 |  |             return $proxy; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         return $proxy->save()->withoutAutoRefresh(function(Proxy $proxy) use ($attributes) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 428 |  |             foreach ($this->afterPersist as $callback) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 24 |  |                 $proxy->executeCallback($callback, $attributes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 428 |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * @see FactoryCollection::__construct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 176 |  |     final public function many(int $min, ?int $max = null): FactoryCollection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 176 |  |         return new FactoryCollection($this, $min, $max); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * @param array|callable $attributes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @return Proxy[]|object[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 136 |  |     final public function createMany(int $number, $attributes = []): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 136 |  |         return $this->many($number)->create($attributes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 168 |  |     public function withoutPersisting(): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 168 |  |         $cloned = clone $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 168 |  |         $cloned->persist = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 168 |  |         return $cloned; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * @param array|callable $attributes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 441 |  |     final public function withAttributes($attributes = []): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 441 |  |         $cloned = clone $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 441 |  |         $cloned->attributeSet[] = $attributes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 441 |  |         return $cloned; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * @param callable $callback (array $attributes): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 24 |  |     final public function beforeInstantiate(callable $callback): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 24 |  |         $cloned = clone $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 24 |  |         $cloned->beforeInstantiate[] = $callback; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 24 |  |         return $cloned; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      * @param callable $callback (object $object, array $attributes): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 | 16 |  |     final public function afterInstantiate(callable $callback): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 16 |  |         $cloned = clone $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 | 16 |  |         $cloned->afterInstantiate[] = $callback; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 | 16 |  |         return $cloned; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |      * @param callable $callback (object|Proxy $object, array $attributes): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 | 16 |  |     final public function afterPersist(callable $callback): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 | 16 |  |         $cloned = clone $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 | 16 |  |         $cloned->afterPersist[] = $callback; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 | 16 |  |         return $cloned; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      * @param callable $instantiator (array $attributes, string $class): object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 | 16 |  |     final public function instantiateWith(callable $instantiator): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 | 16 |  |         $cloned = clone $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 | 16 |  |         $cloned->instantiator = $instantiator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 | 16 |  |         return $cloned; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |      * @internal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 | 692 |  |     final public static function boot(Configuration $configuration): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 | 692 |  |         self::$configuration = $configuration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 | 692 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |      * @internal | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 185 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 186 | 665 |  |     final public static function shutdown(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 187 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 188 | 665 |  |         if (!self::isBooted()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 189 |  |  |             return; | 
            
                                                                        
                            
            
                                    
            
            
                | 190 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 191 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 192 | 665 |  |         self::$configuration->faker()->unique(true); // reset unique | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 193 |  |  |         self::$configuration = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |      * @internal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 | 704 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |     final public static function configuration(): Configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 | 704 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |         if (!self::isBooted()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |             throw new \RuntimeException('Foundry is not yet booted. Using in a test: is your Test case using the Factories trait? Using in a fixture: is ZenstruckFoundryBundle enabled for this environment?'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 | 497 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 | 497 |  |         return self::$configuration; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |      * @internal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 | 572 |  |     final public static function isBooted(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 | 572 |  |         return null !== self::$configuration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |     final public static function faker(): Faker\Generator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |         return self::configuration()->faker(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 | 524 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |      * @param array|callable $attributes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 | 524 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 | 64 |  |     private static function normalizeAttributes($attributes): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |         return \is_callable($attributes) ? $attributes(self::faker()) : $attributes; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 | 524 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 | 56 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  |      * @param mixed $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 | 524 |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 | 80 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |     private function normalizeAttribute($value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 | 64 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 | 80 |  |         if ($value instanceof Proxy) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 | 80 |  |             return $value->object(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  |         if ($value instanceof FactoryCollection) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 | 524 |  |             $value = $this->normalizeCollection($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 | 524 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  |         if (\is_array($value)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 | 104 |  |             // possible OneToMany/ManyToMany relationship | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  |             return \array_map( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 | 24 |  |                 function($value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 |  |  |                     return $this->normalizeAttribute($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  |                 }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 | 104 |  |                 $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 | 56 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  |         if (!$value instanceof self) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 | 56 |  |             return $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 | 16 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 | 16 |  |         if (!$this->persist) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 | 16 |  |             // ensure attribute Factory's are also not persisted | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  |             $value = $value->withoutPersisting(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 | 16 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  |         return $value->create()->object(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 | 40 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  |     private function normalizeCollection(FactoryCollection $collection): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 | 40 |  |         if ($this->persist && $field = $this->inverseRelationshipField($collection->factory())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  |             $this->afterPersist[] = static function(Proxy $proxy) use ($collection, $field) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 | 40 |  |                 $collection->create([$field => $proxy]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 | 40 |  |                 $proxy->refresh(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  |             }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 | 40 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 |  |  |             // creation delegated to afterPersist event - return empty array here | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 | 40 |  |             return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 | 16 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 |  |  |         return $collection->all(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 | 24 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 |  |  |     private function inverseRelationshipField(self $factory): ?string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 |  |  |         $collectionClass = $factory->class; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 |  |  |         $collectionMetadata = self::configuration()->objectManagerFor($collectionClass)->getClassMetadata($collectionClass); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 |  |  |         foreach ($collectionMetadata->getAssociationNames() as $field) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 |  |  |             // ensure 1-n and associated class matches | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 |  |  |             if ($collectionMetadata->isSingleValuedAssociation($field) && $collectionMetadata->getAssociationTargetClass($field) === $this->class) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 |  |  |                 return $field; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 |  |  |         return null; // no relationship found | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 295 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 296 |  |  |  | 
            
                        
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.