diff --git a/DataStorage/Database/DataMapperAbstract.php b/DataStorage/Database/DataMapperAbstract.php index f857571d8..a9d787cc8 100644 --- a/DataStorage/Database/DataMapperAbstract.php +++ b/DataStorage/Database/DataMapperAbstract.php @@ -724,22 +724,26 @@ class DataMapperAbstract implements DataMapperInterface * The reference is stored in the main model * * @param string $propertyName Property name to initialize - * @param object $obj Object to create + * @param mixed $obj Object to create * * @return mixed * * @since 1.0.0 */ - private static function createOwnsOne(string $propertyName, object $obj) + private static function createOwnsOne(string $propertyName, $obj) { - $mapper = static::$ownsOne[$propertyName]['mapper']; - $primaryKey = $mapper::getObjectId($obj); + if (is_object($obj)) { + $mapper = static::$ownsOne[$propertyName]['mapper']; + $primaryKey = $mapper::getObjectId($obj); - if (empty($primaryKey)) { - return $mapper::create($obj); + if (empty($primaryKey)) { + return $mapper::create($obj); + } + + return $primaryKey; } - return $primaryKey; + return $obj; } /** @@ -776,23 +780,27 @@ class DataMapperAbstract implements DataMapperInterface * The reference is stored in the main model * * @param string $propertyName Property name to initialize - * @param object $obj Object to create + * @param mixed $obj Object to create * * @return mixed * * @since 1.0.0 */ - private static function createBelongsTo(string $propertyName, object $obj) + private static function createBelongsTo(string $propertyName, $obj) { - /** @var string $mapper */ - $mapper = static::$belongsTo[$propertyName]['mapper']; - $primaryKey = $mapper::getObjectId($obj); + if (is_object($obj)) { + /** @var string $mapper */ + $mapper = static::$belongsTo[$propertyName]['mapper']; + $primaryKey = $mapper::getObjectId($obj); - if (empty($primaryKey)) { - return $mapper::create($obj); + if (empty($primaryKey)) { + return $mapper::create($obj); + } + + return $primaryKey; } - return $primaryKey; + return $obj; } /** @@ -1069,18 +1077,22 @@ class DataMapperAbstract implements DataMapperInterface * The reference is stored in the main model * * @param string $propertyName Property name to initialize - * @param object $obj Object to update + * @param mixed $obj Object to update * * @return mixed * * @since 1.0.0 */ - private static function updateBelongsTo(string $propertyName, object $obj) + private static function updateBelongsTo(string $propertyName, $obj) { - /** @var string $mapper */ - $mapper = static::$belongsTo[$propertyName]['mapper']; + if (is_object($obj)) { + /** @var string $mapper */ + $mapper = static::$belongsTo[$propertyName]['mapper']; - return $mapper::update($obj); + return $mapper::update($obj); + } + + return $obj; } /** @@ -1269,19 +1281,23 @@ class DataMapperAbstract implements DataMapperInterface * The reference is stored in the main model * * @param string $propertyName Property name to initialize - * @param object $obj Object to delete + * @param mixed $obj Object to delete * * @return mixed * * @since 1.0.0 */ - private static function deleteOwnsOne(string $propertyName, object $obj) + private static function deleteOwnsOne(string $propertyName, $obj) { - /** @var string $mapper */ - $mapper = static::$ownsOne[$propertyName]['mapper']; + if (is_object($obj)) { + /** @var string $mapper */ + $mapper = static::$ownsOne[$propertyName]['mapper']; - // todo: delete owned one object is not recommended since it can be owned by by something else? or does owns one mean that nothing else can have a relation to this one? - return $mapper::delete($obj); + // todo: delete owned one object is not recommended since it can be owned by by something else? or does owns one mean that nothing else can have a relation to this one? + return $mapper::delete($obj); + } + + return $obj; } /** @@ -1290,18 +1306,22 @@ class DataMapperAbstract implements DataMapperInterface * The reference is stored in the main model * * @param string $propertyName Property name to initialize - * @param object $obj Object to delete + * @param mixed $obj Object to delete * * @return mixed * * @since 1.0.0 */ - private static function deleteBelongsTo(string $propertyName, object $obj) + private static function deleteBelongsTo(string $propertyName, $obj) { - /** @var string $mapper */ - $mapper = static::$belongsTo[$propertyName]['mapper']; + if (is_object($obj)) { + /** @var string $mapper */ + $mapper = static::$belongsTo[$propertyName]['mapper']; - return $mapper::delete($obj); + return $mapper::delete($obj); + } + + return $obj; } /** @@ -2694,14 +2714,14 @@ class DataMapperAbstract implements DataMapperInterface /** * Test if object is null object * - * @param object $obj Object to check + * @param mixed $obj Object to check * * @return bool * * @since 1.0.0 */ - private static function isNullObject(object $obj) : bool + private static function isNullObject($obj) : bool { - return strpos(get_class($obj), '\Null') !== false; + return \is_object($obj) && \strpos(\get_class($obj), '\Null') !== false; } } diff --git a/Utils/TestUtils.php b/Utils/TestUtils.php index bef0dc703..52ad9ff16 100644 --- a/Utils/TestUtils.php +++ b/Utils/TestUtils.php @@ -40,17 +40,17 @@ final class TestUtils /** * Set private object member * - * @param object $obj Object to modify - * @param string $name Member name to modify - * @param mixed $value Value to set + * @param object|string $obj Object to modify + * @param string $name Member name to modify + * @param mixed $value Value to set * * @return bool The function returns true after setting the member * * @since 1.0.0 */ - public static function setMember(object $obj, string $name, $value) : bool + public static function setMember($obj, string $name, $value) : bool { - $reflectionClass = new \ReflectionClass(\get_class($obj)); + $reflectionClass = new \ReflectionClass(\is_string($obj) ? $obj : \get_class($obj)); if (!$reflectionClass->hasProperty($name)) { return false; @@ -62,7 +62,11 @@ final class TestUtils $reflectionProperty->setAccessible(true); } - $reflectionProperty->setValue($obj, $value); + if (\is_string($obj)) { + $reflectionProperty->setValue($value); + } elseif (\is_object($obj)) { + $reflectionProperty->setValue($obj, $value); + } if (!$accessible) { $reflectionProperty->setAccessible(false); @@ -74,16 +78,16 @@ final class TestUtils /** * Get private object member * - * @param object $obj Object to read - * @param string $name Member name to read + * @param object|string $obj Object to read + * @param string $name Member name to read * * @return mixed Returns the member variable value * * @since 1.0.0 */ - public static function getMember(object $obj, string $name) + public static function getMember($obj, string $name) { - $reflectionClass = new \ReflectionClass(\get_class($obj)); + $reflectionClass = new \ReflectionClass(\is_string($obj) ? $obj : \get_class($obj)); if (!$reflectionClass->hasProperty($name)) { return null; @@ -95,7 +99,12 @@ final class TestUtils $reflectionProperty->setAccessible(true); } - $value = $reflectionProperty->getValue($obj); + $value = null; + if (\is_string($obj)) { + $value = $reflectionProperty->getValue(); + } elseif (\is_object($obj)) { + $value = $reflectionProperty->getValue($obj); + } if (!$accessible) { $reflectionProperty->setAccessible(false);