From e306954f92e2501f71876815a70c73b76335915b Mon Sep 17 00:00:00 2001 From: Thomas Date: Thu, 10 Aug 2023 12:02:04 +0200 Subject: [PATCH] remove formatting --- src/ORM/DataObject.php | 173 +++++++++++++++++++++-------------------- 1 file changed, 87 insertions(+), 86 deletions(-) diff --git a/src/ORM/DataObject.php b/src/ORM/DataObject.php index d3b6b850116..0744f18312d 100644 --- a/src/ORM/DataObject.php +++ b/src/ORM/DataObject.php @@ -371,13 +371,13 @@ public function __construct($record = [], $creationType = self::CREATE_OBJECT, $ $this->record = []; switch ($creationType) { - // Hydrate a record + // Hydrate a record case self::CREATE_HYDRATED: case self::CREATE_MEMORY_HYDRATED: $this->hydrate($record, $creationType === self::CREATE_HYDRATED); break; - // Create a new object, using the constructor argument as the initial content + // Create a new object, using the constructor argument as the initial content case self::CREATE_OBJECT: if ($record instanceof stdClass) { $record = (array)$record; @@ -392,7 +392,7 @@ public function __construct($record = [], $creationType = self::CREATE_OBJECT, $ user_error( "DataObject::__construct passed $passed. It's supposed to be passed an array," - . " taken straight from the database. Perhaps you should use DataList::create()->First(); instead?", + . " taken straight from the database. Perhaps you should use DataList::create()->First(); instead?", E_USER_WARNING ); $record = []; @@ -553,27 +553,27 @@ protected function duplicateRelations($sourceObject, $destinationObject, $relati foreach ($relations as $relation) { switch (true) { case array_key_exists($relation, $manyMany): { - $this->duplicateManyManyRelation($sourceObject, $destinationObject, $relation); - break; - } + $this->duplicateManyManyRelation($sourceObject, $destinationObject, $relation); + break; + } case array_key_exists($relation, $hasMany): { - $this->duplicateHasManyRelation($sourceObject, $destinationObject, $relation); - break; - } + $this->duplicateHasManyRelation($sourceObject, $destinationObject, $relation); + break; + } case array_key_exists($relation, $hasOne): { - $this->duplicateHasOneRelation($sourceObject, $destinationObject, $relation); - break; - } + $this->duplicateHasOneRelation($sourceObject, $destinationObject, $relation); + break; + } case array_key_exists($relation, $belongsTo): { - $this->duplicateBelongsToRelation($sourceObject, $destinationObject, $relation); - break; - } + $this->duplicateBelongsToRelation($sourceObject, $destinationObject, $relation); + break; + } default: { - $sourceType = get_class($sourceObject); - throw new InvalidArgumentException( - "Cannot duplicate unknown relation {$relation} on parent type {$sourceType}" - ); - } + $sourceType = get_class($sourceObject); + throw new InvalidArgumentException( + "Cannot duplicate unknown relation {$relation} on parent type {$sourceType}" + ); + } } } } @@ -1034,7 +1034,7 @@ public function update($data) } else { user_error( "DataObject::update(): Can't traverse relationship '$relation'," . - "it has to be a has_one relationship or return a single DataObject", + "it has to be a has_one relationship or return a single DataObject", E_USER_NOTICE ); // unset relation object so we don't write properties to the wrong object @@ -1348,7 +1348,7 @@ protected function validateWrite() if ($this->ObsoleteClassName) { return new ValidationException( "Object is of class '{$this->ObsoleteClassName}' which doesn't exist - " . - "you need to change the ClassName before you can write it" + "you need to change the ClassName before you can write it" ); } @@ -1382,7 +1382,7 @@ protected function preWrite() if ($this->brokenOnWrite) { throw new LogicException( static::class . " has a broken onBeforeWrite() function." - . " Make sure that you call parent::onBeforeWrite()." + . " Make sure that you call parent::onBeforeWrite()." ); } } @@ -1730,7 +1730,7 @@ public function delete() if ($this->brokenOnDelete) { throw new LogicException( static::class . " has a broken onBeforeDelete() function." - . " Make sure that you call parent::onBeforeDelete()." + . " Make sure that you call parent::onBeforeDelete()." ); } @@ -2080,66 +2080,66 @@ public function inferReciprocalComponent($remoteClass, $remoteRelation) $relationType = $remote->getRelationType($remoteRelation); switch ($relationType) { case 'has_one': { - // Mock has_many - $joinField = "{$remoteRelation}ID"; - $componentClass = $schema->classForField($remoteClass, $joinField); - $result = HasManyList::create($componentClass, $joinField); - return $result - ->setDataQueryParam($this->getInheritableQueryParams()) - ->forForeignID($this->ID); - } + // Mock has_many + $joinField = "{$remoteRelation}ID"; + $componentClass = $schema->classForField($remoteClass, $joinField); + $result = HasManyList::create($componentClass, $joinField); + return $result + ->setDataQueryParam($this->getInheritableQueryParams()) + ->forForeignID($this->ID); + } case 'belongs_to': case 'has_many': { - // These relations must have a has_one on the other end, so find it - $joinField = $schema->getRemoteJoinField( - $remoteClass, - $remoteRelation, - $relationType, - $polymorphic - ); - // If relation is polymorphic, do not infer recriprocal relationship automatically - if ($polymorphic) { - return null; - } - $joinID = $this->getField($joinField); - if (empty($joinID)) { - return null; - } - // Get object by joined ID - return DataObject::get($remoteClass) - ->filter('ID', $joinID) - ->setDataQueryParam($this->getInheritableQueryParams()) - ->first(); + // These relations must have a has_one on the other end, so find it + $joinField = $schema->getRemoteJoinField( + $remoteClass, + $remoteRelation, + $relationType, + $polymorphic + ); + // If relation is polymorphic, do not infer recriprocal relationship automatically + if ($polymorphic) { + return null; + } + $joinID = $this->getField($joinField); + if (empty($joinID)) { + return null; } + // Get object by joined ID + return DataObject::get($remoteClass) + ->filter('ID', $joinID) + ->setDataQueryParam($this->getInheritableQueryParams()) + ->first(); + } case 'many_many': case 'belongs_many_many': { - // Get components and extra fields from parent - $manyMany = $remote->getSchema()->manyManyComponent($remoteClass, $remoteRelation); - $extraFields = $schema->manyManyExtraFieldsForComponent($remoteClass, $remoteRelation) ?: []; - - // Reverse parent and component fields and create an inverse ManyManyList - /** @var RelationList $result */ - $result = Injector::inst()->create( - $manyMany['relationClass'], - $manyMany['parentClass'], // Substitute parent class for dataClass - $manyMany['join'], - $manyMany['parentField'], // Reversed parent / child field - $manyMany['childField'], // Reversed parent / child field - $extraFields, - $manyMany['childClass'], // substitute child class for parentClass - $remoteClass // In case ManyManyThroughList needs to use PolymorphicHasManyList internally - ); - $this->extend('updateManyManyComponents', $result); + // Get components and extra fields from parent + $manyMany = $remote->getSchema()->manyManyComponent($remoteClass, $remoteRelation); + $extraFields = $schema->manyManyExtraFieldsForComponent($remoteClass, $remoteRelation) ?: []; + + // Reverse parent and component fields and create an inverse ManyManyList + /** @var RelationList $result */ + $result = Injector::inst()->create( + $manyMany['relationClass'], + $manyMany['parentClass'], // Substitute parent class for dataClass + $manyMany['join'], + $manyMany['parentField'], // Reversed parent / child field + $manyMany['childField'], // Reversed parent / child field + $extraFields, + $manyMany['childClass'], // substitute child class for parentClass + $remoteClass // In case ManyManyThroughList needs to use PolymorphicHasManyList internally + ); + $this->extend('updateManyManyComponents', $result); - // If this is called on a singleton, then we return an 'orphaned relation' that can have the - // foreignID set elsewhere. - return $result - ->setDataQueryParam($this->getInheritableQueryParams()) - ->forForeignID($this->ID); - } + // If this is called on a singleton, then we return an 'orphaned relation' that can have the + // foreignID set elsewhere. + return $result + ->setDataQueryParam($this->getInheritableQueryParams()) + ->forForeignID($this->ID); + } default: { - return null; - } + return null; + } } } @@ -2369,24 +2369,24 @@ public function scaffoldSearchFields($_params = null) if ($params['fieldClasses'] && isset($params['fieldClasses'][$fieldName])) { $fieldClass = $params['fieldClasses'][$fieldName]; $field = new $fieldClass($fieldName); - // If we explicitly set a field, then construct that + // If we explicitly set a field, then construct that } elseif (isset($spec['field'])) { // If it's a string, use it as a class name and construct if (is_string($spec['field'])) { $fieldClass = $spec['field']; $field = new $fieldClass($fieldName); - // If it's a FormField object, then just use that object directly. + // If it's a FormField object, then just use that object directly. } elseif ($spec['field'] instanceof FormField) { $field = $spec['field']; - // Otherwise we have a bug + // Otherwise we have a bug } else { user_error("Bad value for searchable_fields, 'field' value: " . var_export($spec['field'], true), E_USER_WARNING); } - // Otherwise, use the database field's scaffolder + // Otherwise, use the database field's scaffolder } elseif ($object = $this->relObject($fieldName)) { if (is_object($object) && $object->hasMethod('scaffoldSearchField')) { $field = $object->scaffoldSearchField(); @@ -2687,7 +2687,7 @@ protected function loadLazyFields($class = null) } } - // No data means that the query returned nothing; assign 'null' to all the requested fields + // No data means that the query returned nothing; assign 'null' to all the requested fields } else { foreach ($columns as $k) { $this->record[$k] = null; @@ -2845,7 +2845,7 @@ public function setField($fieldName, $val) $val->bindTo($this); $this->setFieldValue($fieldName, $val); } - // Situation 2: Passing a literal or non-DBField object + // Situation 2: Passing a literal or non-DBField object } else { $this->setFieldValue($fieldName, $val); } @@ -2885,7 +2885,7 @@ private function setFieldValue(string $fieldName, mixed $val): void // Value has changed as well, not just the type $this->changed[$fieldName] = self::CHANGE_VALUE; } - // Value has been restored to its original, remove any record of the change + // Value has been restored to its original, remove any record of the change } elseif (isset($this->changed[$fieldName])) { unset($this->changed[$fieldName]); } @@ -2954,7 +2954,8 @@ public function castingHelper($field) public function hasField($field) { $schema = static::getSchema(); - return (array_key_exists($field, $this->record ?? []) + return ( + array_key_exists($field, $this->record ?? []) || array_key_exists($field, $this->components ?? []) || $schema->fieldSpec(static::class, $field) || $schema->unaryComponent(static::class, $field) @@ -3472,7 +3473,7 @@ public static function reset() public static function get_by_id($classOrID, $idOrCache = null, $cache = true) { // Shift arguments if passing id in first or second argument - list($class, $id, $cached) = is_numeric($classOrID) + list ($class, $id, $cached) = is_numeric($classOrID) ? [get_called_class(), (int) $classOrID, isset($idOrCache) ? $idOrCache : $cache] : [$classOrID, (int) $idOrCache, $cache]; if ($id < 1) {