diff --git a/doc/mapping.md b/doc/mapping.md index fe90e2c0dd..5cb48cc378 100644 --- a/doc/mapping.md +++ b/doc/mapping.md @@ -149,7 +149,7 @@ class Annotation implements Driver } // validate encoding type $mapping = $meta->getFieldMapping($field); - if ($mapping['type'] != 'string') { + if (($mapping->type ?? $mapping['type']) != 'string') { throw new \Exception("Only strings can be encoded"); } // store the metadata diff --git a/src/Blameable/Mapping/Driver/Xml.php b/src/Blameable/Mapping/Driver/Xml.php index 42861f8e17..703a5c19a5 100644 --- a/src/Blameable/Mapping/Driver/Xml.php +++ b/src/Blameable/Mapping/Driver/Xml.php @@ -128,6 +128,6 @@ protected function isValidField($meta, $field) { $mapping = $meta->getFieldMapping($field); - return $mapping && in_array($mapping['type'], self::VALID_TYPES, true); + return $mapping && in_array($mapping->type ?? $mapping['type'], self::VALID_TYPES, true); } } diff --git a/src/Blameable/Mapping/Driver/Yaml.php b/src/Blameable/Mapping/Driver/Yaml.php index e72b334890..02010462ff 100644 --- a/src/Blameable/Mapping/Driver/Yaml.php +++ b/src/Blameable/Mapping/Driver/Yaml.php @@ -134,6 +134,6 @@ protected function isValidField($meta, $field) { $mapping = $meta->getFieldMapping($field); - return $mapping && in_array($mapping['type'], self::VALID_TYPES, true); + return $mapping && in_array($mapping->type ?? $mapping['type'], self::VALID_TYPES, true); } } diff --git a/src/IpTraceable/Mapping/Driver/Xml.php b/src/IpTraceable/Mapping/Driver/Xml.php index 9e64a9efd2..c8759f5c31 100644 --- a/src/IpTraceable/Mapping/Driver/Xml.php +++ b/src/IpTraceable/Mapping/Driver/Xml.php @@ -130,6 +130,6 @@ protected function isValidField($meta, $field) { $mapping = $meta->getFieldMapping($field); - return $mapping && in_array($mapping['type'], self::VALID_TYPES, true); + return $mapping && in_array($mapping->type ?? $mapping['type'], self::VALID_TYPES, true); } } diff --git a/src/IpTraceable/Mapping/Driver/Yaml.php b/src/IpTraceable/Mapping/Driver/Yaml.php index 590969ea8d..66c2509d28 100644 --- a/src/IpTraceable/Mapping/Driver/Yaml.php +++ b/src/IpTraceable/Mapping/Driver/Yaml.php @@ -130,6 +130,6 @@ protected function isValidField($meta, $field) { $mapping = $meta->getFieldMapping($field); - return $mapping && in_array($mapping['type'], self::VALID_TYPES, true); + return $mapping && in_array($mapping->type ?? $mapping['type'], self::VALID_TYPES, true); } } diff --git a/src/Loggable/Entity/Repository/LogEntryRepository.php b/src/Loggable/Entity/Repository/LogEntryRepository.php index 9e3274f80c..2769cbe6d3 100644 --- a/src/Loggable/Entity/Repository/LogEntryRepository.php +++ b/src/Loggable/Entity/Repository/LogEntryRepository.php @@ -165,7 +165,7 @@ protected function mapValue(ClassMetadata $objectMeta, $field, &$value) } $mapping = $objectMeta->getAssociationMapping($field); - $value = $value ? $this->getEntityManager()->getReference($mapping['targetEntity'], $value) : null; + $value = $value ? $this->getEntityManager()->getReference($mapping->targetEntity ?? $mapping['targetEntity'], $value) : null; } /** diff --git a/src/Mapping/Driver/AbstractAnnotationDriver.php b/src/Mapping/Driver/AbstractAnnotationDriver.php index 43c92deae3..fb170f3663 100644 --- a/src/Mapping/Driver/AbstractAnnotationDriver.php +++ b/src/Mapping/Driver/AbstractAnnotationDriver.php @@ -131,7 +131,7 @@ protected function isValidField($meta, $field) { $mapping = $meta->getFieldMapping($field); - return $mapping && in_array($mapping['type'], $this->validTypes, true); + return $mapping && in_array($mapping->type ?? $mapping['type'], $this->validTypes, true); } /** diff --git a/src/ReferenceIntegrity/ReferenceIntegrityListener.php b/src/ReferenceIntegrity/ReferenceIntegrityListener.php index 901c18dc76..c6113ee60d 100644 --- a/src/ReferenceIntegrity/ReferenceIntegrityListener.php +++ b/src/ReferenceIntegrity/ReferenceIntegrityListener.php @@ -86,15 +86,15 @@ public function preRemove(EventArgs $args) throw new InvalidMappingException(sprintf("Reference '%s' on '%s' should have 'mappedBy' option defined", $property, $meta->getName())); } - assert(class_exists($fieldMapping['targetDocument'])); + assert(class_exists($fieldMapping->targetDocument ?? $fieldMapping['targetDocument'])); - $subMeta = $om->getClassMetadata($fieldMapping['targetDocument']); + $subMeta = $om->getClassMetadata($fieldMapping->targetDocument ?? $fieldMapping['targetDocument']); - if (!$subMeta->hasField($fieldMapping['mappedBy'])) { - throw new InvalidMappingException(sprintf('Unable to find reference integrity [%s] as mapped property in entity - %s', $fieldMapping['mappedBy'], $fieldMapping['targetDocument'])); + if (!$subMeta->hasField($fieldMapping->mappedBy ?? $fieldMapping['mappedBy'])) { + throw new InvalidMappingException(sprintf('Unable to find reference integrity [%s] as mapped property in entity - %s', $fieldMapping->mappedBy ?? $fieldMapping['mappedBy'], $fieldMapping->targetDocument ?? $fieldMapping['targetDocument'])); } - $refReflProp = $subMeta->getReflectionProperty($fieldMapping['mappedBy']); + $refReflProp = $subMeta->getReflectionProperty($fieldMapping->mappedBy ?? $fieldMapping['mappedBy']); if ($meta->isCollectionValuedReference($property)) { foreach ($refDoc as $refObj) { @@ -112,19 +112,19 @@ public function preRemove(EventArgs $args) throw new InvalidMappingException(sprintf("Reference '%s' on '%s' should have 'mappedBy' option defined", $property, $meta->getName())); } - assert(class_exists($fieldMapping['targetDocument'])); + assert(class_exists($fieldMapping->targetDocument ?? $fieldMapping['targetDocument'])); - $subMeta = $om->getClassMetadata($fieldMapping['targetDocument']); + $subMeta = $om->getClassMetadata($fieldMapping->targetDocument ?? $fieldMapping['targetDocument']); - if (!$subMeta->hasField($fieldMapping['mappedBy'])) { - throw new InvalidMappingException(sprintf('Unable to find reference integrity [%s] as mapped property in entity - %s', $fieldMapping['mappedBy'], $fieldMapping['targetDocument'])); + if (!$subMeta->hasField($fieldMapping->mappedBy ?? $fieldMapping['mappedBy'])) { + throw new InvalidMappingException(sprintf('Unable to find reference integrity [%s] as mapped property in entity - %s', $fieldMapping->mappedBy ?? $fieldMapping['mappedBy'], $fieldMapping->targetDocument ?? $fieldMapping['targetDocument'])); } - if (!$subMeta->isCollectionValuedReference($fieldMapping['mappedBy'])) { - throw new InvalidMappingException(sprintf('Reference integrity [%s] mapped property in entity - %s should be a Reference Many', $fieldMapping['mappedBy'], $fieldMapping['targetDocument'])); + if (!$subMeta->isCollectionValuedReference($fieldMapping->mappedBy ?? $fieldMapping['mappedBy'])) { + throw new InvalidMappingException(sprintf('Reference integrity [%s] mapped property in entity - %s should be a Reference Many', $fieldMapping->mappedBy ?? $fieldMapping['mappedBy'], $fieldMapping->targetDocument ?? $fieldMapping['targetDocument'])); } - $refReflProp = $subMeta->getReflectionProperty($fieldMapping['mappedBy']); + $refReflProp = $subMeta->getReflectionProperty($fieldMapping->mappedBy ?? $fieldMapping['mappedBy']); if ($meta->isCollectionValuedReference($property)) { foreach ($refDoc as $refObj) { @@ -143,10 +143,10 @@ public function preRemove(EventArgs $args) break; case Validator::RESTRICT: if ($meta->isCollectionValuedReference($property) && $refDoc->count() > 0) { - throw new ReferenceIntegrityStrictException(sprintf("The reference integrity for the '%s' collection is restricted", $fieldMapping['targetDocument'])); + throw new ReferenceIntegrityStrictException(sprintf("The reference integrity for the '%s' collection is restricted", $fieldMapping->targetDocument ?? $fieldMapping['targetDocument'])); } if ($meta->isSingleValuedReference($property) && null !== $refDoc) { - throw new ReferenceIntegrityStrictException(sprintf("The reference integrity for the '%s' document is restricted", $fieldMapping['targetDocument'])); + throw new ReferenceIntegrityStrictException(sprintf("The reference integrity for the '%s' document is restricted", $fieldMapping->targetDocument ?? $fieldMapping['targetDocument'])); } break; diff --git a/src/Sluggable/Mapping/Driver/Xml.php b/src/Sluggable/Mapping/Driver/Xml.php index a54976b85d..89f8d454d7 100644 --- a/src/Sluggable/Mapping/Driver/Xml.php +++ b/src/Sluggable/Mapping/Driver/Xml.php @@ -76,7 +76,7 @@ protected function isValidField($meta, $field) { $mapping = $meta->getFieldMapping($field); - return $mapping && in_array($mapping['type'], self::VALID_TYPES, true); + return $mapping && in_array($mapping->type ?? $mapping['type'], self::VALID_TYPES, true); } /** diff --git a/src/Sluggable/Mapping/Driver/Yaml.php b/src/Sluggable/Mapping/Driver/Yaml.php index eddc7c9c5f..167258a22f 100644 --- a/src/Sluggable/Mapping/Driver/Yaml.php +++ b/src/Sluggable/Mapping/Driver/Yaml.php @@ -85,7 +85,7 @@ protected function isValidField($meta, $field) { $mapping = $meta->getFieldMapping($field); - return $mapping && in_array($mapping['type'], self::VALID_TYPES, true); + return $mapping && in_array($mapping->type ?? $mapping['type'], self::VALID_TYPES, true); } /** diff --git a/src/Sluggable/SluggableListener.php b/src/Sluggable/SluggableListener.php index 2b519fe955..485804505a 100644 --- a/src/Sluggable/SluggableListener.php +++ b/src/Sluggable/SluggableListener.php @@ -440,11 +440,12 @@ private function generateSlug(SluggableAdapter $ea, object $object): void } // cut slug if exceeded in length - if (isset($mapping['length']) && strlen($slug) > $mapping['length']) { - $slug = substr($slug, 0, $mapping['length']); + $length = $mapping->length ?? $mapping['length'] ?? null; + if (null !== $length && strlen($slug) > $length) { + $slug = substr($slug, 0, $length); } - if (isset($mapping['nullable']) && $mapping['nullable'] && 0 === strlen($slug)) { + if (($mapping->nullable ?? $mapping['nullable'] ?? false) && 0 === strlen($slug)) { $slug = null; } @@ -546,11 +547,12 @@ private function makeUniqueSlug(SluggableAdapter $ea, object $object, string $pr } $mapping = $meta->getFieldMapping($config['slug']); - if (isset($mapping['length']) && strlen($generatedSlug) > $mapping['length']) { + $length = $mapping->length ?? $mapping['length'] ?? null; + if (null !== $length && strlen($generatedSlug) > $length) { $generatedSlug = substr( $generatedSlug, 0, - $mapping['length'] - (strlen($uniqueSuffix) + strlen($config['separator'])) + $length - (strlen($uniqueSuffix) + strlen($config['separator'])) ); $this->exponent = strlen($uniqueSuffix) - 1; if (substr($generatedSlug, -strlen($config['separator'])) == $config['separator']) { diff --git a/src/SoftDeleteable/Mapping/Validator.php b/src/SoftDeleteable/Mapping/Validator.php index 0f138e2504..2e9d52bd38 100644 --- a/src/SoftDeleteable/Mapping/Validator.php +++ b/src/SoftDeleteable/Mapping/Validator.php @@ -52,8 +52,8 @@ public static function validateField(ClassMetadata $meta, $field) $fieldMapping = $meta->getFieldMapping($field); - if (!in_array($fieldMapping['type'], self::$validTypes, true)) { - throw new InvalidMappingException(sprintf('Field "%s" (type "%s") must be of one of the following types: "%s" in entity %s', $field, $fieldMapping['type'], implode(', ', self::$validTypes), $meta->getName())); + if (!in_array($fieldMapping->type ?? $fieldMapping['type'], self::$validTypes, true)) { + throw new InvalidMappingException(sprintf('Field "%s" (type "%s") must be of one of the following types: "%s" in entity %s', $field, $fieldMapping->type ?? $fieldMapping['type'], implode(', ', self::$validTypes), $meta->getName())); } } } diff --git a/src/Sortable/Mapping/Driver/Xml.php b/src/Sortable/Mapping/Driver/Xml.php index 658e692c36..e02b81ca18 100644 --- a/src/Sortable/Mapping/Driver/Xml.php +++ b/src/Sortable/Mapping/Driver/Xml.php @@ -90,7 +90,7 @@ protected function isValidField($meta, $field) { $mapping = $meta->getFieldMapping($field); - return $mapping && in_array($mapping['type'], self::VALID_TYPES, true); + return $mapping && in_array($mapping->type ?? $mapping['type'], self::VALID_TYPES, true); } /** diff --git a/src/Sortable/Mapping/Driver/Yaml.php b/src/Sortable/Mapping/Driver/Yaml.php index d09d815ab7..500a4e0250 100644 --- a/src/Sortable/Mapping/Driver/Yaml.php +++ b/src/Sortable/Mapping/Driver/Yaml.php @@ -97,7 +97,7 @@ protected function isValidField($meta, $field) { $mapping = $meta->getFieldMapping($field); - return $mapping && in_array($mapping['type'], self::VALID_TYPES, true); + return $mapping && in_array($mapping->type ?? $mapping['type'], self::VALID_TYPES, true); } /** diff --git a/src/Timestampable/Mapping/Driver/Xml.php b/src/Timestampable/Mapping/Driver/Xml.php index e94415ab42..e217168063 100644 --- a/src/Timestampable/Mapping/Driver/Xml.php +++ b/src/Timestampable/Mapping/Driver/Xml.php @@ -105,6 +105,6 @@ protected function isValidField($meta, $field) { $mapping = $meta->getFieldMapping($field); - return $mapping && in_array($mapping['type'], self::VALID_TYPES, true); + return $mapping && in_array($mapping->type ?? $mapping['type'], self::VALID_TYPES, true); } } diff --git a/src/Timestampable/Mapping/Driver/Yaml.php b/src/Timestampable/Mapping/Driver/Yaml.php index 3c1600f734..0dee60da71 100644 --- a/src/Timestampable/Mapping/Driver/Yaml.php +++ b/src/Timestampable/Mapping/Driver/Yaml.php @@ -109,6 +109,6 @@ protected function isValidField($meta, $field) { $mapping = $meta->getFieldMapping($field); - return $mapping && in_array($mapping['type'], self::VALID_TYPES, true); + return $mapping && in_array($mapping->type ?? $mapping['type'], self::VALID_TYPES, true); } } diff --git a/src/Translatable/Query/TreeWalker/TranslationWalker.php b/src/Translatable/Query/TreeWalker/TranslationWalker.php index 4801ad4d16..7fad4792b3 100644 --- a/src/Translatable/Query/TreeWalker/TranslationWalker.php +++ b/src/Translatable/Query/TreeWalker/TranslationWalker.php @@ -297,7 +297,7 @@ private function prepareTranslatedComponents(): void $mappingFK = $transMeta->getFieldMapping('foreignKey'); $mappingPK = $meta->getFieldMapping($identifier); - $fkColName = $this->getCastedForeignKey($compTblAlias.'.'.$idColName, $mappingFK['type'], $mappingPK['type']); + $fkColName = $this->getCastedForeignKey($compTblAlias.'.'.$idColName, $mappingFK->type ?? $mappingFK['type'], $mappingPK->type ?? $mappingPK['type']); $sql .= ' AND '.$tblAlias.'.'.$quoteStrategy->getColumnName('foreignKey', $transMeta, $this->platform) .' = '.$fkColName; } @@ -309,10 +309,10 @@ private function prepareTranslatedComponents(): void // Treat translation as original field type $fieldMapping = $meta->getFieldMapping($field); if ((($this->platform instanceof AbstractMySQLPlatform) - && in_array($fieldMapping['type'], ['decimal'], true)) + && in_array($fieldMapping->type ?? $fieldMapping['type'], ['decimal'], true)) || (!($this->platform instanceof AbstractMySQLPlatform) - && !in_array($fieldMapping['type'], ['datetime', 'datetimetz', 'date', 'time'], true))) { - $type = Type::getType($fieldMapping['type']); + && !in_array($fieldMapping->type ?? $fieldMapping['type'], ['datetime', 'datetimetz', 'date', 'time'], true))) { + $type = Type::getType($fieldMapping->type ?? $fieldMapping['type']); // In ORM 2.x, $fieldMapping is an array. In ORM 3.x, it's a data object. Always cast to an array for compatibility across versions. $substituteField = 'CAST('.$substituteField.' AS '.$type->getSQLDeclaration((array) $fieldMapping, $this->platform).')'; diff --git a/src/Tree/Mapping/Validator.php b/src/Tree/Mapping/Validator.php index f2d0fcd9fa..8c9258eeda 100644 --- a/src/Tree/Mapping/Validator.php +++ b/src/Tree/Mapping/Validator.php @@ -9,6 +9,7 @@ namespace Gedmo\Tree\Mapping; +use Doctrine\ORM\Mapping\FieldMapping; use Doctrine\Persistence\Mapping\ClassMetadata; use Gedmo\Exception\InvalidMappingException; @@ -97,7 +98,7 @@ public function isValidField($meta, $field) { $mapping = $meta->getFieldMapping($field); - return $mapping && in_array($mapping['type'], self::VALID_TYPES, true); + return $mapping && in_array($this->getMappingType($mapping), self::VALID_TYPES, true); } /** @@ -112,7 +113,7 @@ public function isValidFieldForPath($meta, $field) { $mapping = $meta->getFieldMapping($field); - return $mapping && in_array($mapping['type'], $this->validPathTypes, true); + return $mapping && in_array($this->getMappingType($mapping), $this->validPathTypes, true); } /** @@ -127,7 +128,7 @@ public function isValidFieldForPathSource($meta, $field) { $mapping = $meta->getFieldMapping($field); - return $mapping && in_array($mapping['type'], $this->validPathSourceTypes, true); + return $mapping && in_array($this->getMappingType($mapping), $this->validPathSourceTypes, true); } /** @@ -142,7 +143,7 @@ public function isValidFieldForPathHash($meta, $field) { $mapping = $meta->getFieldMapping($field); - return $mapping && in_array($mapping['type'], $this->validPathHashTypes, true); + return $mapping && in_array($this->getMappingType($mapping), $this->validPathHashTypes, true); } /** @@ -157,7 +158,7 @@ public function isValidFieldForLockTime($meta, $field) { $mapping = $meta->getFieldMapping($field); - return $mapping && ('date' === $mapping['type'] || 'datetime' === $mapping['type'] || 'timestamp' === $mapping['type']); + return $mapping && ('date' === $this->getMappingType($mapping) || 'datetime' === $this->getMappingType($mapping) || 'timestamp' === $this->getMappingType($mapping)); } /** @@ -172,7 +173,7 @@ public function isValidFieldForRoot($meta, $field) { $mapping = $meta->getFieldMapping($field); - return $mapping && in_array($mapping['type'], $this->validRootTypes, true); + return $mapping && in_array($this->getMappingType($mapping), $this->validRootTypes, true); } /** @@ -252,4 +253,16 @@ public function validateMaterializedPathTreeMetadata($meta, array $config) throw new InvalidMappingException('Missing properties: '.implode(', ', $missingFields)." in class - {$meta->getName()}"); } } + + /** + * @param FieldMapping|array $mapping + */ + private function getMappingType($mapping): string + { + if ($mapping instanceof FieldMapping) { + return $mapping->type; + } + + return $mapping['type']; + } } diff --git a/src/Tree/Strategy/AbstractMaterializedPath.php b/src/Tree/Strategy/AbstractMaterializedPath.php index 0f4cc8372f..c1eef0df63 100644 --- a/src/Tree/Strategy/AbstractMaterializedPath.php +++ b/src/Tree/Strategy/AbstractMaterializedPath.php @@ -225,7 +225,7 @@ public function updateNode(ObjectManager $om, $node, AdapterInterface $ea) // default behavior: if PathSource field is a string, we append the ID to the path // path_append_id is true: always append id // path_append_id is false: never append id - if (true === $config['path_append_id'] || ('string' === $fieldMapping['type'] && false !== $config['path_append_id'])) { + if (true === $config['path_append_id'] || ('string' === ($fieldMapping->type ?? $fieldMapping['type']) && false !== $config['path_append_id'])) { if (method_exists($meta, 'getIdentifierValue')) { $identifier = $meta->getIdentifierValue($node); } else { diff --git a/src/Tree/Strategy/ORM/Closure.php b/src/Tree/Strategy/ORM/Closure.php index 3e68f6b3d5..9d80e51a45 100644 --- a/src/Tree/Strategy/ORM/Closure.php +++ b/src/Tree/Strategy/ORM/Closure.php @@ -524,7 +524,9 @@ protected function setLevelFieldOnPendingNodes(ObjectManager $em) } // Avoid type conversion performance penalty - $type = 'integer' === $mapping['type'] ? ArrayParameterType::INTEGER : ArrayParameterType::STRING; + $type = 'integer' === ($mapping->type ?? $mapping['type']) + ? ArrayParameterType::INTEGER + : ArrayParameterType::STRING; // We calculate levels for all nodes $sql = 'SELECT c.descendant, MAX(c.depth) + 1 AS levelNum '; diff --git a/src/Uploadable/Mapping/Validator.php b/src/Uploadable/Mapping/Validator.php index 4dfce4c1b3..73571800bd 100644 --- a/src/Uploadable/Mapping/Validator.php +++ b/src/Uploadable/Mapping/Validator.php @@ -150,7 +150,7 @@ public static function validateField($meta, $field, $uploadableField, $validFiel $fieldMapping = $meta->getFieldMapping($field); - if (!in_array($fieldMapping['type'], $validFieldTypes, true)) { + if (!in_array($fieldMapping->type ?? $fieldMapping['type'], $validFieldTypes, true)) { $msg = 'Field "%s" to work as an "%s" field must be of one of the following types: "%s".'; throw new InvalidMappingException(sprintf($msg, $field, $uploadableField, implode(', ', $validFieldTypes))); diff --git a/tests/Gedmo/Mapping/Mock/Extension/Encoder/Mapping/Driver/Attribute.php b/tests/Gedmo/Mapping/Mock/Extension/Encoder/Mapping/Driver/Attribute.php index eeaf0619a5..2794af8934 100644 --- a/tests/Gedmo/Mapping/Mock/Extension/Encoder/Mapping/Driver/Attribute.php +++ b/tests/Gedmo/Mapping/Mock/Extension/Encoder/Mapping/Driver/Attribute.php @@ -49,7 +49,7 @@ public function readExtendedMetadata($meta, array &$config) // validate encoding type $mapping = $meta->getFieldMapping($field); - if ('string' !== $mapping['type']) { + if ('string' !== ($mapping->type ?? $mapping['type'])) { throw new \Exception('Only strings can be encoded'); }