|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Port\Doctrine\LookupStrategy; |
| 4 | + |
| 5 | +use Doctrine\Persistence\ObjectManager; |
| 6 | +use Doctrine\Persistence\ObjectRepository; |
| 7 | +use Port\Doctrine\LookupStrategy; |
| 8 | + |
| 9 | +/** |
| 10 | + * Default lookup strategy using repository field criteria. |
| 11 | + * |
| 12 | + * Behavior matches historical DoctrineWriter lookup: |
| 13 | + * - With lookup fields: call the repository method (default findOneBy) with those criteria |
| 14 | + * - Without lookup fields: ObjectRepository::find(current($item)) |
| 15 | + */ |
| 16 | +class FieldsLookupStrategy implements LookupStrategy |
| 17 | +{ |
| 18 | + private ObjectRepository $objectRepository; |
| 19 | + |
| 20 | + /** @var list<string> */ |
| 21 | + private array $lookupFields; |
| 22 | + |
| 23 | + private string $lookupMethod; |
| 24 | + |
| 25 | + /** |
| 26 | + * @param ObjectRepository $objectRepository |
| 27 | + * @param string|array|null $index Field or fields used as lookup criteria (null = find by first item value) |
| 28 | + * @param string $lookupMethod Repository method used when lookup fields are set |
| 29 | + */ |
| 30 | + public function __construct( |
| 31 | + ObjectRepository $objectRepository, |
| 32 | + string|array|null $index = null, |
| 33 | + string $lookupMethod = 'findOneBy' |
| 34 | + ) { |
| 35 | + $this->objectRepository = $objectRepository; |
| 36 | + $this->lookupFields = $this->normalizeIndex($index); |
| 37 | + $this->assertLookupMethod($lookupMethod); |
| 38 | + $this->lookupMethod = $lookupMethod; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Convenience factory from an object manager and class name. |
| 43 | + */ |
| 44 | + public static function fromObjectManager( |
| 45 | + ObjectManager $objectManager, |
| 46 | + string $objectName, |
| 47 | + string|array|null $index = null, |
| 48 | + string $lookupMethod = 'findOneBy' |
| 49 | + ): self { |
| 50 | + return new self( |
| 51 | + $objectManager->getRepository($objectName), |
| 52 | + $index, |
| 53 | + $lookupMethod |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @param string $field Field to find current objects by |
| 59 | + */ |
| 60 | + public function withLookupField(string $field): self |
| 61 | + { |
| 62 | + return $this->withLookupFields([$field]); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @param list<string> $fields Fields to find current objects by |
| 67 | + */ |
| 68 | + public function withLookupFields(array $fields): self |
| 69 | + { |
| 70 | + $new = clone $this; |
| 71 | + $new->lookupFields = array_values($fields); |
| 72 | + |
| 73 | + return $new; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Accept string or list of fields (mirrors DoctrineWriter $index constructor arg). |
| 78 | + * |
| 79 | + * @param string|array $index |
| 80 | + */ |
| 81 | + public function withIndex(string|array $index): self |
| 82 | + { |
| 83 | + if (is_array($index)) { |
| 84 | + return $this->withLookupFields($index); |
| 85 | + } |
| 86 | + |
| 87 | + return $this->withLookupField($index); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Doctrine repository method for finding objects when lookup fields are set. |
| 92 | + */ |
| 93 | + public function withLookupMethod(string $lookupMethod): self |
| 94 | + { |
| 95 | + $this->assertLookupMethod($lookupMethod); |
| 96 | + |
| 97 | + $new = clone $this; |
| 98 | + $new->lookupMethod = $lookupMethod; |
| 99 | + |
| 100 | + return $new; |
| 101 | + } |
| 102 | + |
| 103 | + public function lookup(array $item): ?object |
| 104 | + { |
| 105 | + if (!empty($this->lookupFields)) { |
| 106 | + $lookupConditions = []; |
| 107 | + foreach ($this->lookupFields as $fieldName) { |
| 108 | + $lookupConditions[$fieldName] = $item[$fieldName] ?? null; |
| 109 | + } |
| 110 | + |
| 111 | + $result = $this->objectRepository->{$this->lookupMethod}($lookupConditions); |
| 112 | + |
| 113 | + return is_object($result) ? $result : null; |
| 114 | + } |
| 115 | + |
| 116 | + $result = $this->objectRepository->find(current($item)); |
| 117 | + |
| 118 | + return is_object($result) ? $result : null; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @param string|array|null $index |
| 123 | + * |
| 124 | + * @return list<string> |
| 125 | + */ |
| 126 | + private function normalizeIndex(string|array|null $index): array |
| 127 | + { |
| 128 | + if ($index === null) { |
| 129 | + return []; |
| 130 | + } |
| 131 | + |
| 132 | + if (is_array($index)) { |
| 133 | + return array_values($index); |
| 134 | + } |
| 135 | + |
| 136 | + return [$index]; |
| 137 | + } |
| 138 | + |
| 139 | + private function assertLookupMethod(string $lookupMethod): void |
| 140 | + { |
| 141 | + if (!method_exists($this->objectRepository, $lookupMethod)) { |
| 142 | + throw new \InvalidArgumentException( |
| 143 | + sprintf( |
| 144 | + 'Repository %s has no method %s', |
| 145 | + get_class($this->objectRepository), |
| 146 | + $lookupMethod |
| 147 | + ) |
| 148 | + ); |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments