HierarchyDiscriminatorResolver.php
9 months ago
IdentifierFlattener.php
9 months ago
LockSqlHelper.php
9 months ago
PersisterHelper.php
9 months ago
index.php
9 months ago
PersisterHelper.php
145 lines
| 1 | <?php |
| 2 | declare (strict_types=1); |
| 3 | namespace MailPoetVendor\Doctrine\ORM\Utility; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | use BackedEnum; |
| 6 | use MailPoetVendor\Doctrine\DBAL\Connection; |
| 7 | use MailPoetVendor\Doctrine\DBAL\Types\Type; |
| 8 | use MailPoetVendor\Doctrine\ORM\EntityManagerInterface; |
| 9 | use MailPoetVendor\Doctrine\ORM\Mapping\ClassMetadata; |
| 10 | use MailPoetVendor\Doctrine\ORM\Proxy\DefaultProxyClassNameResolver; |
| 11 | use MailPoetVendor\Doctrine\ORM\Query\QueryException; |
| 12 | use RuntimeException; |
| 13 | use function array_map; |
| 14 | use function array_merge; |
| 15 | use function is_array; |
| 16 | use function is_object; |
| 17 | use function sprintf; |
| 18 | class PersisterHelper |
| 19 | { |
| 20 | public static function getTypeOfField($fieldName, ClassMetadata $class, EntityManagerInterface $em) |
| 21 | { |
| 22 | if (isset($class->fieldMappings[$fieldName])) { |
| 23 | return [$class->fieldMappings[$fieldName]['type']]; |
| 24 | } |
| 25 | if (!isset($class->associationMappings[$fieldName])) { |
| 26 | return []; |
| 27 | } |
| 28 | $assoc = $class->associationMappings[$fieldName]; |
| 29 | if (!$assoc['isOwningSide']) { |
| 30 | return self::getTypeOfField($assoc['mappedBy'], $em->getClassMetadata($assoc['targetEntity']), $em); |
| 31 | } |
| 32 | if ($assoc['type'] & ClassMetadata::MANY_TO_MANY) { |
| 33 | $joinData = $assoc['joinTable']; |
| 34 | } else { |
| 35 | $joinData = $assoc; |
| 36 | } |
| 37 | $types = []; |
| 38 | $targetClass = $em->getClassMetadata($assoc['targetEntity']); |
| 39 | foreach ($joinData['joinColumns'] as $joinColumn) { |
| 40 | $types[] = self::getTypeOfColumn($joinColumn['referencedColumnName'], $targetClass, $em); |
| 41 | } |
| 42 | return $types; |
| 43 | } |
| 44 | public static function getTypeOfColumn($columnName, ClassMetadata $class, EntityManagerInterface $em) |
| 45 | { |
| 46 | if (isset($class->fieldNames[$columnName])) { |
| 47 | $fieldName = $class->fieldNames[$columnName]; |
| 48 | if (isset($class->fieldMappings[$fieldName])) { |
| 49 | return $class->fieldMappings[$fieldName]['type']; |
| 50 | } |
| 51 | } |
| 52 | // iterate over to-one association mappings |
| 53 | foreach ($class->associationMappings as $assoc) { |
| 54 | if (!isset($assoc['joinColumns'])) { |
| 55 | continue; |
| 56 | } |
| 57 | foreach ($assoc['joinColumns'] as $joinColumn) { |
| 58 | if ($joinColumn['name'] === $columnName) { |
| 59 | $targetColumnName = $joinColumn['referencedColumnName']; |
| 60 | $targetClass = $em->getClassMetadata($assoc['targetEntity']); |
| 61 | return self::getTypeOfColumn($targetColumnName, $targetClass, $em); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | // iterate over to-many association mappings |
| 66 | foreach ($class->associationMappings as $assoc) { |
| 67 | if (!(isset($assoc['joinTable']) && isset($assoc['joinTable']['joinColumns']))) { |
| 68 | continue; |
| 69 | } |
| 70 | foreach ($assoc['joinTable']['joinColumns'] as $joinColumn) { |
| 71 | if ($joinColumn['name'] === $columnName) { |
| 72 | $targetColumnName = $joinColumn['referencedColumnName']; |
| 73 | $targetClass = $em->getClassMetadata($assoc['targetEntity']); |
| 74 | return self::getTypeOfColumn($targetColumnName, $targetClass, $em); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | throw new RuntimeException(sprintf('Could not resolve type of column "%s" of class "%s"', $columnName, $class->getName())); |
| 79 | } |
| 80 | public static function inferParameterTypes(string $field, $value, ClassMetadata $class, EntityManagerInterface $em) : array |
| 81 | { |
| 82 | $types = []; |
| 83 | switch (\true) { |
| 84 | case isset($class->fieldMappings[$field]): |
| 85 | $types = array_merge($types, [$class->fieldMappings[$field]['type']]); |
| 86 | break; |
| 87 | case isset($class->associationMappings[$field]): |
| 88 | $assoc = $class->associationMappings[$field]; |
| 89 | $class = $em->getClassMetadata($assoc['targetEntity']); |
| 90 | if (!$assoc['isOwningSide']) { |
| 91 | $assoc = $class->associationMappings[$assoc['mappedBy']]; |
| 92 | $class = $em->getClassMetadata($assoc['targetEntity']); |
| 93 | } |
| 94 | $columns = $assoc['type'] === ClassMetadata::MANY_TO_MANY ? $assoc['relationToTargetKeyColumns'] : $assoc['sourceToTargetKeyColumns']; |
| 95 | foreach ($columns as $column) { |
| 96 | $types[] = self::getTypeOfColumn($column, $class, $em); |
| 97 | } |
| 98 | break; |
| 99 | default: |
| 100 | $types[] = null; |
| 101 | break; |
| 102 | } |
| 103 | if (is_array($value)) { |
| 104 | return array_map(static function ($type) { |
| 105 | $type = Type::getType($type); |
| 106 | return $type->getBindingType() + Connection::ARRAY_PARAM_OFFSET; |
| 107 | }, $types); |
| 108 | } |
| 109 | return $types; |
| 110 | } |
| 111 | public static function convertToParameterValue($value, EntityManagerInterface $em) : array |
| 112 | { |
| 113 | if (is_array($value)) { |
| 114 | $newValue = []; |
| 115 | foreach ($value as $itemValue) { |
| 116 | $newValue = array_merge($newValue, self::convertToParameterValue($itemValue, $em)); |
| 117 | } |
| 118 | return [$newValue]; |
| 119 | } |
| 120 | return self::convertIndividualValue($value, $em); |
| 121 | } |
| 122 | private static function convertIndividualValue($value, EntityManagerInterface $em) : array |
| 123 | { |
| 124 | if (!is_object($value)) { |
| 125 | return [$value]; |
| 126 | } |
| 127 | if ($value instanceof BackedEnum) { |
| 128 | return [$value->value]; |
| 129 | } |
| 130 | $valueClass = DefaultProxyClassNameResolver::getClass($value); |
| 131 | if ($em->getMetadataFactory()->isTransient($valueClass)) { |
| 132 | return [$value]; |
| 133 | } |
| 134 | $class = $em->getClassMetadata($valueClass); |
| 135 | if ($class->isIdentifierComposite) { |
| 136 | $newValue = []; |
| 137 | foreach ($class->getIdentifierValues($value) as $innerValue) { |
| 138 | $newValue = array_merge($newValue, self::convertToParameterValue($innerValue, $em)); |
| 139 | } |
| 140 | return $newValue; |
| 141 | } |
| 142 | return [$em->getUnitOfWork()->getSingleIdentifierValue($value)]; |
| 143 | } |
| 144 | } |
| 145 |