|
4 | 4 |
|
5 | 5 | namespace ACSEO\TypesenseBundle\Transformer; |
6 | 6 |
|
| 7 | +use App\Doctrine\Entity\Site\Site; |
| 8 | +use Doctrine\Common\Collections\Collection; |
7 | 9 | use Doctrine\Common\Util\ClassUtils; |
| 10 | +use Doctrine\ORM\PersistentCollection; |
8 | 11 | use Symfony\Component\DependencyInjection\ContainerInterface; |
9 | 12 | use Symfony\Component\PropertyAccess\Exception\RuntimeException; |
10 | 13 | use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
@@ -61,11 +64,7 @@ public function convert($entity): array |
61 | 64 | if (str_contains($entityAttribute, '::')) { |
62 | 65 | $value = $this->getFieldValueFromService($entity, $entityAttribute); |
63 | 66 | } else { |
64 | | - try { |
65 | | - $value = $this->accessor->getValue($entity, $fieldsInfo['entity_attribute']); |
66 | | - } catch (RuntimeException $exception) { |
67 | | - $value = null; |
68 | | - } |
| 67 | + $value = $this->getFieldValueFromEntity($entity, $entityAttribute); |
69 | 68 | } |
70 | 69 |
|
71 | 70 | $name = $fieldsInfo['name']; |
@@ -141,4 +140,61 @@ private function getFieldValueFromService($entity, $entityAttribute) |
141 | 140 | return null; |
142 | 141 | } |
143 | 142 |
|
| 143 | + /* |
| 144 | + * @param object $entity The starting entity object. |
| 145 | + * @param string $entityAttribute The dot-separated attribute path. |
| 146 | + * @return mixed|null The value of the attribute, or null if not found or an exception occurs. |
| 147 | + */ |
| 148 | + private function getFieldValueFromEntity(object $entity, string $entityAttribute) |
| 149 | + { |
| 150 | + if (empty($entityAttribute)) { |
| 151 | + return null; |
| 152 | + } |
| 153 | + |
| 154 | + try { |
| 155 | + if ($this->accessor->isReadable($entity, $entityAttribute)) { |
| 156 | + return $this->accessor->getValue($entity, $entityAttribute); |
| 157 | + } |
| 158 | + |
| 159 | + // Handle cases where the path *might* involve a Collection (1-n, n-n) |
| 160 | + $atr = explode('.', $entityAttribute); |
| 161 | + $firstProperty = $atr[0]; |
| 162 | + |
| 163 | + if ($this->accessor->isReadable($entity, $firstProperty)) { |
| 164 | + $nestedValue = $this->accessor->getValue($entity, $firstProperty); |
| 165 | + |
| 166 | + if ($nestedValue instanceof Collection || is_array($nestedValue)) { |
| 167 | + $remainingPath = implode('.', array_slice($atr, 1)); |
| 168 | + |
| 169 | + if (empty($remainingPath)) { |
| 170 | + return $nestedValue; |
| 171 | + } |
| 172 | + |
| 173 | + $results = []; |
| 174 | + |
| 175 | + foreach ($nestedValue as $item) { |
| 176 | + $result = $this->getFieldValueFromEntity($item, $remainingPath); |
| 177 | + if ($result !== null) { |
| 178 | + if (is_array($result)) { |
| 179 | + $results = array_merge($results, $result); |
| 180 | + } else { |
| 181 | + $results[] = $result; |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + if (count($results) > 0) { |
| 187 | + return $results; |
| 188 | + } |
| 189 | + } else { |
| 190 | + return null; |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + return null; |
| 195 | + |
| 196 | + } catch (RuntimeException $exception) { |
| 197 | + return null; |
| 198 | + } |
| 199 | + } |
144 | 200 | } |
0 commit comments