|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Orbitale\Component\ArrayFixture; |
| 4 | + |
| 5 | +use Doctrine\Common\Collections\ArrayCollection; |
| 6 | + |
| 7 | +abstract class StaticFixture extends ArrayFixture |
| 8 | +{ |
| 9 | + abstract public static function getStaticData(): iterable; |
| 10 | + |
| 11 | + public static function findByKeyAndValue(string $key, mixed $value): array |
| 12 | + { |
| 13 | + $data = \array_filter( |
| 14 | + static::getStaticData(), |
| 15 | + static fn (array $data) => $value instanceof Ref && $data[$key] instanceof Ref |
| 16 | + ? $value->name === $data[$key]->name |
| 17 | + : $data[$key] === $value |
| 18 | + ); |
| 19 | + |
| 20 | + if (!\count($data)) { |
| 21 | + throw new \RuntimeException(\sprintf('No fixture object with key "%s" and value "%s" in class "%s".', $key, $value, static::class)); |
| 22 | + } |
| 23 | + |
| 24 | + return \array_values($data)[0]; |
| 25 | + } |
| 26 | + |
| 27 | + public static function filterByKeyAndValue(string $key, mixed $value): array |
| 28 | + { |
| 29 | + return \array_filter( |
| 30 | + static::getStaticData(), |
| 31 | + static fn (array $data) => $value instanceof Ref && $data[$key] instanceof Ref |
| 32 | + ? $value->name === $data[$key]->name |
| 33 | + : $data[$key] === $value |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + public static function filterData(\Closure $callback): array |
| 38 | + { |
| 39 | + return \array_filter(static::getStaticData(), $callback); |
| 40 | + } |
| 41 | + |
| 42 | + public static function getIdFromName(string $name): string |
| 43 | + { |
| 44 | + foreach (static::getStaticData() as $id => $values) { |
| 45 | + if ($values['name'] === $name) { |
| 46 | + return $id; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + throw new \RuntimeException(\sprintf('No fixture object with name "%s" in class "%s".', $name, static::class)); |
| 51 | + } |
| 52 | + |
| 53 | + protected function getObjects(): iterable |
| 54 | + { |
| 55 | + foreach (static::getStaticData() as $id => $properties) { |
| 56 | + foreach ($properties as $property => $value) { |
| 57 | + $properties['id'] = $id; |
| 58 | + |
| 59 | + if ($value instanceof \Closure) { |
| 60 | + $value = $value(); |
| 61 | + } |
| 62 | + |
| 63 | + if ($value instanceof Ref) { |
| 64 | + $value = $this->getReference($value->name, $value->class); |
| 65 | + } elseif (\is_array($value)) { |
| 66 | + $arrayData = $value; |
| 67 | + $hasRef = false; |
| 68 | + |
| 69 | + foreach ($arrayData as $key => $arrayValue) { |
| 70 | + if ($arrayValue instanceof Ref) { |
| 71 | + $arrayData[$key] = $this->getReference($arrayValue->name, $arrayValue->class); |
| 72 | + $hasRef = true; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + $value = $hasRef ? new ArrayCollection($arrayData) : $arrayData; |
| 77 | + } |
| 78 | + |
| 79 | + $properties[$property] = $value; |
| 80 | + } |
| 81 | + |
| 82 | + $class = $this->getEntityClass(); |
| 83 | + if (\property_exists($class, 'createdAt')) { |
| 84 | + $properties['createdAt'] = new \DateTime(); |
| 85 | + } |
| 86 | + if (\property_exists($class, 'updatedAt')) { |
| 87 | + $properties['updatedAt'] = new \DateTime(); |
| 88 | + } |
| 89 | + |
| 90 | + yield $id => $properties; |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments