AbstractHydrator.php
9 months ago
ArrayHydrator.php
9 months ago
HydrationException.php
9 months ago
IterableResult.php
9 months ago
ObjectHydrator.php
9 months ago
ScalarColumnHydrator.php
9 months ago
ScalarHydrator.php
9 months ago
SimpleObjectHydrator.php
9 months ago
SingleScalarHydrator.php
9 months ago
index.php
9 months ago
SimpleObjectHydrator.php
129 lines
| 1 | <?php |
| 2 | declare (strict_types=1); |
| 3 | namespace MailPoetVendor\Doctrine\ORM\Internal\Hydration; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | use MailPoetVendor\Doctrine\ORM\Internal\SQLResultCasing; |
| 6 | use MailPoetVendor\Doctrine\ORM\Mapping\ClassMetadata; |
| 7 | use MailPoetVendor\Doctrine\ORM\Mapping\MappingException; |
| 8 | use MailPoetVendor\Doctrine\ORM\Query; |
| 9 | use Exception; |
| 10 | use RuntimeException; |
| 11 | use ValueError; |
| 12 | use function array_keys; |
| 13 | use function array_search; |
| 14 | use function count; |
| 15 | use function in_array; |
| 16 | use function is_array; |
| 17 | use function key; |
| 18 | use function reset; |
| 19 | use function sprintf; |
| 20 | class SimpleObjectHydrator extends AbstractHydrator |
| 21 | { |
| 22 | use SQLResultCasing; |
| 23 | private $class; |
| 24 | protected function prepare() |
| 25 | { |
| 26 | if (count($this->resultSetMapping()->aliasMap) !== 1) { |
| 27 | throw new RuntimeException('Cannot use SimpleObjectHydrator with a ResultSetMapping that contains more than one object result.'); |
| 28 | } |
| 29 | if ($this->resultSetMapping()->scalarMappings) { |
| 30 | throw new RuntimeException('Cannot use SimpleObjectHydrator with a ResultSetMapping that contains scalar mappings.'); |
| 31 | } |
| 32 | $this->class = $this->getClassMetadata(reset($this->resultSetMapping()->aliasMap)); |
| 33 | } |
| 34 | protected function cleanup() |
| 35 | { |
| 36 | parent::cleanup(); |
| 37 | $this->_uow->triggerEagerLoads(); |
| 38 | $this->_uow->hydrationComplete(); |
| 39 | } |
| 40 | protected function hydrateAllData() |
| 41 | { |
| 42 | $result = []; |
| 43 | while ($row = $this->statement()->fetchAssociative()) { |
| 44 | $this->hydrateRowData($row, $result); |
| 45 | } |
| 46 | $this->_em->getUnitOfWork()->triggerEagerLoads(); |
| 47 | return $result; |
| 48 | } |
| 49 | protected function hydrateRowData(array $row, array &$result) |
| 50 | { |
| 51 | $entityName = $this->class->name; |
| 52 | $data = []; |
| 53 | $discrColumnValue = null; |
| 54 | // We need to find the correct entity class name if we have inheritance in resultset |
| 55 | if ($this->class->inheritanceType !== ClassMetadata::INHERITANCE_TYPE_NONE) { |
| 56 | $discrColumn = $this->class->getDiscriminatorColumn(); |
| 57 | $discrColumnName = $this->getSQLResultCasing($this->_platform, $discrColumn['name']); |
| 58 | // Find mapped discriminator column from the result set. |
| 59 | $metaMappingDiscrColumnName = array_search($discrColumnName, $this->resultSetMapping()->metaMappings, \true); |
| 60 | if ($metaMappingDiscrColumnName) { |
| 61 | $discrColumnName = $metaMappingDiscrColumnName; |
| 62 | } |
| 63 | if (!isset($row[$discrColumnName])) { |
| 64 | throw HydrationException::missingDiscriminatorColumn($entityName, $discrColumnName, key($this->resultSetMapping()->aliasMap)); |
| 65 | } |
| 66 | if ($row[$discrColumnName] === '') { |
| 67 | throw HydrationException::emptyDiscriminatorValue(key($this->resultSetMapping()->aliasMap)); |
| 68 | } |
| 69 | $discrMap = $this->class->discriminatorMap; |
| 70 | if (!isset($discrMap[$row[$discrColumnName]])) { |
| 71 | throw HydrationException::invalidDiscriminatorValue($row[$discrColumnName], array_keys($discrMap)); |
| 72 | } |
| 73 | $entityName = $discrMap[$row[$discrColumnName]]; |
| 74 | $discrColumnValue = $row[$discrColumnName]; |
| 75 | unset($row[$discrColumnName]); |
| 76 | } |
| 77 | foreach ($row as $column => $value) { |
| 78 | // An ObjectHydrator should be used instead of SimpleObjectHydrator |
| 79 | if (isset($this->resultSetMapping()->relationMap[$column])) { |
| 80 | throw new Exception(sprintf('Unable to retrieve association information for column "%s"', $column)); |
| 81 | } |
| 82 | $cacheKeyInfo = $this->hydrateColumnInfo($column); |
| 83 | if (!$cacheKeyInfo) { |
| 84 | continue; |
| 85 | } |
| 86 | // If we have inheritance in resultset, make sure the field belongs to the correct class |
| 87 | if (isset($cacheKeyInfo['discriminatorValues']) && !in_array((string) $discrColumnValue, $cacheKeyInfo['discriminatorValues'], \true)) { |
| 88 | continue; |
| 89 | } |
| 90 | // Check if value is null before conversion (because some types convert null to something else) |
| 91 | $valueIsNull = $value === null; |
| 92 | // Convert field to a valid PHP value |
| 93 | if (isset($cacheKeyInfo['type'])) { |
| 94 | $type = $cacheKeyInfo['type']; |
| 95 | $value = $type->convertToPHPValue($value, $this->_platform); |
| 96 | } |
| 97 | if ($value !== null && isset($cacheKeyInfo['enumType'])) { |
| 98 | $originalValue = $currentValue = $value; |
| 99 | try { |
| 100 | if (!is_array($originalValue)) { |
| 101 | $value = $this->buildEnum($originalValue, $cacheKeyInfo['enumType']); |
| 102 | } else { |
| 103 | $value = []; |
| 104 | foreach ($originalValue as $i => $currentValue) { |
| 105 | $value[$i] = $this->buildEnum($currentValue, $cacheKeyInfo['enumType']); |
| 106 | } |
| 107 | } |
| 108 | } catch (ValueError $e) { |
| 109 | throw MappingException::invalidEnumValue($entityName, $cacheKeyInfo['fieldName'], (string) $currentValue, $cacheKeyInfo['enumType'], $e); |
| 110 | } |
| 111 | } |
| 112 | $fieldName = $cacheKeyInfo['fieldName']; |
| 113 | // Prevent overwrite in case of inherit classes using same property name (See AbstractHydrator) |
| 114 | if (!isset($data[$fieldName]) || !$valueIsNull) { |
| 115 | $data[$fieldName] = $value; |
| 116 | } |
| 117 | } |
| 118 | if (isset($this->_hints[Query::HINT_REFRESH_ENTITY])) { |
| 119 | $this->registerManaged($this->class, $this->_hints[Query::HINT_REFRESH_ENTITY], $data); |
| 120 | } |
| 121 | $uow = $this->_em->getUnitOfWork(); |
| 122 | $entity = $uow->createEntity($entityName, $data, $this->_hints); |
| 123 | $result[] = $entity; |
| 124 | if (isset($this->_hints[Query::HINT_INTERNAL_ITERATION]) && $this->_hints[Query::HINT_INTERNAL_ITERATION]) { |
| 125 | $this->_uow->hydrationComplete(); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 |