er1z /
fakemock
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Er1z\FakeMock; |
||
| 4 | |||
| 5 | use Er1z\FakeMock\Annotations\FakeMock as MainAnnotation; |
||
| 6 | use Er1z\FakeMock\Decorator\DecoratorChain; |
||
| 7 | use Er1z\FakeMock\Decorator\DecoratorChainInterface; |
||
| 8 | use Er1z\FakeMock\Metadata\Factory; |
||
| 9 | use Er1z\FakeMock\Metadata\FactoryInterface; |
||
| 10 | use Er1z\FakeMock\Generator\GeneratorChain; |
||
| 11 | use Er1z\FakeMock\Generator\GeneratorChainInterface; |
||
| 12 | |||
| 13 | class FakeMock |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var GeneratorChainInterface |
||
| 17 | */ |
||
| 18 | private $generatorChain; |
||
| 19 | /** |
||
| 20 | * @var DecoratorChainInterface |
||
| 21 | */ |
||
| 22 | private $decoratorChain; |
||
| 23 | /** |
||
| 24 | * @var FactoryInterface |
||
| 25 | */ |
||
| 26 | private $metadataFactory; |
||
| 27 | |||
| 28 | 66 | public function __construct( |
|
| 29 | ?FactoryInterface $metadataFactory = null, ?GeneratorChainInterface $generatorChain = null, ?DecoratorChainInterface $decoratorChain = null |
||
| 30 | ) { |
||
| 31 | 66 | $this->generatorChain = $generatorChain ?? new GeneratorChain(); |
|
| 32 | 66 | $this->decoratorChain = $decoratorChain ?? new DecoratorChain(); |
|
| 33 | 66 | $this->metadataFactory = $metadataFactory ?? new Factory(); |
|
| 34 | 66 | } |
|
| 35 | |||
| 36 | 54 | public function fill($objectOrClassName, $group = null, $newObjectArguments = []) |
|
| 37 | { |
||
| 38 | 54 | $obj = $this->getClass($objectOrClassName, $newObjectArguments); |
|
| 39 | |||
| 40 | 54 | $reflection = new \ReflectionClass($obj); |
|
| 41 | 54 | $cfg = $this->metadataFactory->getObjectConfiguration($reflection); |
|
| 42 | |||
| 43 | 54 | if (!$cfg) { |
|
| 44 | 8 | return $obj; |
|
| 45 | } |
||
| 46 | |||
| 47 | 46 | return $this->populateObject($reflection, $obj, $cfg, $group); |
|
| 48 | } |
||
| 49 | |||
| 50 | 46 | protected function populateObject(\ReflectionClass $reflection, $object, MainAnnotation $objectConfiguration, $group = null) |
|
| 51 | { |
||
| 52 | 46 | $props = $reflection->getProperties(); |
|
| 53 | |||
| 54 | 46 | foreach ($props as $prop) { |
|
| 55 | 46 | $metadatum = $this->metadataFactory->create($object, $objectConfiguration, $prop); |
|
| 56 | 46 | $metadata = $this->metadataFactory->getConfigurationForFieldByGroup($metadatum, $group); |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 57 | |||
| 58 | 46 | if (!$metadata) { |
|
| 59 | 12 | continue; |
|
| 60 | } |
||
| 61 | |||
| 62 | 38 | $value = $this->generatorChain->getValueForField($metadata, $this, $group); |
|
| 63 | 38 | $value = $this->decoratorChain->getDecoratedValue($value, $metadata); |
|
| 64 | |||
| 65 | 38 | Accessor::setPropertyValue($object, $prop->getName(), $value); |
|
| 66 | } |
||
| 67 | |||
| 68 | 46 | return $object; |
|
| 69 | } |
||
| 70 | |||
| 71 | 54 | protected function getClass($objectOrClass, $newObjectArguments = []) |
|
| 72 | { |
||
| 73 | 54 | if (is_object($objectOrClass)) { |
|
| 74 | 50 | return $objectOrClass; |
|
| 75 | } |
||
| 76 | |||
| 77 | 4 | return new $objectOrClass(...(array) $newObjectArguments); |
|
| 78 | } |
||
| 79 | } |
||
| 80 |