optimize code readability and perforamnce

This commit is contained in:
Dennis Eichhorn 2020-03-07 19:48:58 +01:00
parent 2a8ece6fcc
commit e5baf46dce

View File

@ -699,6 +699,10 @@ class DataMapperAbstract implements DataMapperInterface
private static function createHasMany(\ReflectionClass $refClass, object $obj, $objId) : void private static function createHasMany(\ReflectionClass $refClass, object $obj, $objId) : void
{ {
foreach (static::$hasMany as $propertyName => $rel) { foreach (static::$hasMany as $propertyName => $rel) {
if (!isset(static::$hasMany[$propertyName]['mapper'])) {
throw new InvalidMapperException();
}
$property = $refClass->getProperty($propertyName); $property = $refClass->getProperty($propertyName);
if (!($isPublic = $property->isPublic())) { if (!($isPublic = $property->isPublic())) {
@ -716,14 +720,10 @@ class DataMapperAbstract implements DataMapperInterface
$property->setAccessible(false); $property->setAccessible(false);
} }
if (!isset(static::$hasMany[$propertyName]['mapper'])) {
throw new InvalidMapperException();
}
/** @var string $mapper */ /** @var string $mapper */
$mapper = static::$hasMany[$propertyName]['mapper']; $mapper = static::$hasMany[$propertyName]['mapper'];
$objsIds = []; $objsIds = [];
$relReflectionClass = null; $relReflectionClass = !empty($values) ? new \ReflectionClass(reset($values)) : null;;
foreach ($values as $key => $value) { foreach ($values as $key => $value) {
if (!\is_object($value)) { if (!\is_object($value)) {
@ -733,10 +733,6 @@ class DataMapperAbstract implements DataMapperInterface
continue; continue;
} }
if ($relReflectionClass === null) {
$relReflectionClass = new \ReflectionClass($value);
}
$primaryKey = $mapper::getObjectId($value, $relReflectionClass); $primaryKey = $mapper::getObjectId($value, $relReflectionClass);
// already in db // already in db
@ -785,12 +781,12 @@ class DataMapperAbstract implements DataMapperInterface
private static function createHasManyArray(array &$obj, $objId) : void private static function createHasManyArray(array &$obj, $objId) : void
{ {
foreach (static::$hasMany as $propertyName => $rel) { foreach (static::$hasMany as $propertyName => $rel) {
$values = $obj[$propertyName];
if (!isset(static::$hasMany[$propertyName]['mapper'])) { if (!isset(static::$hasMany[$propertyName]['mapper'])) {
throw new InvalidMapperException(); throw new InvalidMapperException();
} }
$values = $obj[$propertyName];
/** @var string $mapper */ /** @var string $mapper */
$mapper = static::$hasMany[$propertyName]['mapper']; $mapper = static::$hasMany[$propertyName]['mapper'];
$objsIds = []; $objsIds = [];
@ -842,7 +838,10 @@ class DataMapperAbstract implements DataMapperInterface
*/ */
private static function createOwnsOne(string $propertyName, $obj) private static function createOwnsOne(string $propertyName, $obj)
{ {
if (\is_object($obj)) { if (!\is_object($obj)) {
return $obj;
}
$mapper = static::$ownsOne[$propertyName]['mapper']; $mapper = static::$ownsOne[$propertyName]['mapper'];
$primaryKey = $mapper::getObjectId($obj); $primaryKey = $mapper::getObjectId($obj);
@ -853,9 +852,6 @@ class DataMapperAbstract implements DataMapperInterface
return $primaryKey; return $primaryKey;
} }
return $obj;
}
/** /**
* Create owns one * Create owns one
* *
@ -894,7 +890,10 @@ class DataMapperAbstract implements DataMapperInterface
*/ */
private static function createBelongsTo(string $propertyName, $obj) private static function createBelongsTo(string $propertyName, $obj)
{ {
if (\is_object($obj)) { if (!\is_object($obj)) {
return $obj;
}
/** @var string $mapper */ /** @var string $mapper */
$mapper = static::$belongsTo[$propertyName]['mapper']; $mapper = static::$belongsTo[$propertyName]['mapper'];
$primaryKey = $mapper::getObjectId($obj); $primaryKey = $mapper::getObjectId($obj);
@ -906,9 +905,6 @@ class DataMapperAbstract implements DataMapperInterface
return $primaryKey; return $primaryKey;
} }
return $obj;
}
/** /**
* Create owns one * Create owns one
* *
@ -950,7 +946,10 @@ class DataMapperAbstract implements DataMapperInterface
private static function createRelationTable(string $propertyName, array $objsIds, $objId) : void private static function createRelationTable(string $propertyName, array $objsIds, $objId) : void
{ {
/** @var string $table */ /** @var string $table */
if (!empty($objsIds) && isset(static::$hasMany[$propertyName]['self'])) { if (empty($objsIds) || !isset(static::$hasMany[$propertyName]['self'])) {
return;
}
$relQuery = new Builder(self::$db); $relQuery = new Builder(self::$db);
$relQuery->prefix(self::$db->getPrefix()) $relQuery->prefix(self::$db->getPrefix())
->into(static::$hasMany[$propertyName]['table']) ->into(static::$hasMany[$propertyName]['table'])
@ -972,7 +971,6 @@ class DataMapperAbstract implements DataMapperInterface
\var_dump($relQuery->toSql()); \var_dump($relQuery->toSql());
} }
} }
}
/** /**
* Parse value * Parse value
@ -1035,6 +1033,10 @@ class DataMapperAbstract implements DataMapperInterface
continue; continue;
} }
if (!isset(static::$hasMany[$propertyName]['mapper'])) {
throw new InvalidMapperException();
}
$property = $refClass->getProperty($propertyName); $property = $refClass->getProperty($propertyName);
if (!($isPublic = $property->isPublic())) { if (!($isPublic = $property->isPublic())) {
@ -1047,13 +1049,9 @@ class DataMapperAbstract implements DataMapperInterface
$property->setAccessible(false); $property->setAccessible(false);
} }
if (!isset(static::$hasMany[$propertyName]['mapper'])) {
throw new InvalidMapperException();
}
/** @var string $mapper */ /** @var string $mapper */
$mapper = static::$hasMany[$propertyName]['mapper']; $mapper = static::$hasMany[$propertyName]['mapper'];
$relReflectionClass = null; $relReflectionClass = !empty($values) ? new \ReflectionClass(reset($values)) : null;
$objsIds[$propertyName] = []; $objsIds[$propertyName] = [];
foreach ($values as $key => &$value) { foreach ($values as $key => &$value) {
@ -1064,10 +1062,6 @@ class DataMapperAbstract implements DataMapperInterface
continue; continue;
} }
if ($relReflectionClass === null) {
$relReflectionClass = new \ReflectionClass($value);
}
$primaryKey = $mapper::getObjectId($value, $relReflectionClass); $primaryKey = $mapper::getObjectId($value, $relReflectionClass);
// already in db // already in db
@ -1128,12 +1122,12 @@ class DataMapperAbstract implements DataMapperInterface
continue; continue;
} }
$values = $obj[$propertyName];
if (!isset(static::$hasMany[$propertyName]['mapper'])) { if (!isset(static::$hasMany[$propertyName]['mapper'])) {
throw new InvalidMapperException(); throw new InvalidMapperException();
} }
$values = $obj[$propertyName];
/** @var string $mapper */ /** @var string $mapper */
$mapper = static::$hasMany[$propertyName]['mapper']; $mapper = static::$hasMany[$propertyName]['mapper'];
$objsIds[$propertyName] = []; $objsIds[$propertyName] = [];
@ -1210,17 +1204,20 @@ class DataMapperAbstract implements DataMapperInterface
* @param array $objsIds Object ids to insert * @param array $objsIds Object ids to insert
* @param mixed $objId Model to reference * @param mixed $objId Model to reference
* *
* @return mixed * @return void
* *
* @since 1.0.0 * @since 1.0.0
*/ */
private static function deleteRelationTable(string $propertyName, array $objsIds, $objId) private static function deleteRelationTable(string $propertyName, array $objsIds, $objId) : void
{ {
/** @var string $table */ /** @var string $table */
if (!empty($objsIds) if (empty($objsIds)
&& static::$hasMany[$propertyName]['table'] !== static::$table || static::$hasMany[$propertyName]['table'] === static::$table
&& static::$hasMany[$propertyName]['table'] !== static::$hasMany[$propertyName]['mapper']::$table || static::$hasMany[$propertyName]['table'] === static::$hasMany[$propertyName]['mapper']::$table
) { ) {
return;
}
foreach ($objsIds as $key => $src) { foreach ($objsIds as $key => $src) {
$relQuery = new Builder(self::$db); $relQuery = new Builder(self::$db);
$relQuery->prefix(self::$db->getPrefix()) $relQuery->prefix(self::$db->getPrefix())
@ -1232,7 +1229,6 @@ class DataMapperAbstract implements DataMapperInterface
self::$db->con->prepare($relQuery->toSql())->execute(); self::$db->con->prepare($relQuery->toSql())->execute();
} }
} }
}
/** /**
* Update owns one * Update owns one
@ -1326,16 +1322,16 @@ class DataMapperAbstract implements DataMapperInterface
*/ */
private static function updateBelongsToArray(string $propertyName, $obj, int $relations = RelationType::ALL, int $depth = 1) private static function updateBelongsToArray(string $propertyName, $obj, int $relations = RelationType::ALL, int $depth = 1)
{ {
if (\is_array($obj)) { if (!\is_array($obj)) {
return $obj;
}
/** @var string $mapper */ /** @var string $mapper */
$mapper = static::$belongsTo[$propertyName]['mapper']; $mapper = static::$belongsTo[$propertyName]['mapper'];
return $mapper::updateArray($obj, $relations, $depth); return $mapper::updateArray($obj, $relations, $depth);
} }
return $obj;
}
/** /**
* Update object in db. * Update object in db.
* *
@ -1582,6 +1578,10 @@ class DataMapperAbstract implements DataMapperInterface
private static function deleteHasMany(\ReflectionClass $refClass, object $obj, $objId, int $relations) : void private static function deleteHasMany(\ReflectionClass $refClass, object $obj, $objId, int $relations) : void
{ {
foreach (static::$hasMany as $propertyName => $rel) { foreach (static::$hasMany as $propertyName => $rel) {
if (!isset(static::$hasMany[$propertyName]['mapper'])) {
throw new InvalidMapperException();
}
$property = $refClass->getProperty($propertyName); $property = $refClass->getProperty($propertyName);
if (!($isPublic = $property->isPublic())) { if (!($isPublic = $property->isPublic())) {
@ -1594,14 +1594,10 @@ class DataMapperAbstract implements DataMapperInterface
$property->setAccessible(false); $property->setAccessible(false);
} }
if (!isset(static::$hasMany[$propertyName]['mapper'])) {
throw new InvalidMapperException();
}
/** @var string $mapper */ /** @var string $mapper */
$mapper = static::$hasMany[$propertyName]['mapper']; $mapper = static::$hasMany[$propertyName]['mapper'];
$objsIds = []; $objsIds = [];
$relReflectionClass = null; $relReflectionClass = !empty($values) ? new \ReflectionClass(reset($values)) : null;;
foreach ($values as $key => &$value) { foreach ($values as $key => &$value) {
if (!\is_object($value)) { if (!\is_object($value)) {
@ -1611,19 +1607,11 @@ class DataMapperAbstract implements DataMapperInterface
continue; continue;
} }
if ($relReflectionClass === null) {
$relReflectionClass = new \ReflectionClass($value);
}
$primaryKey = $mapper::getObjectId($value, $relReflectionClass); $primaryKey = $mapper::getObjectId($value, $relReflectionClass);
// already in db // already in db
if (!empty($primaryKey)) { if (!empty($primaryKey)) {
if ($relations === RelationType::ALL) { $objsIds[$key] = $relations === RelationType::ALL ? $mapper::delete($value) : $primaryKey;
$objsIds[$key] = $mapper::delete($value);
} else {
$objsIds[$key] = $primaryKey;
}
continue; continue;
} }
@ -1653,15 +1641,16 @@ class DataMapperAbstract implements DataMapperInterface
*/ */
private static function deleteOwnsOne(string $propertyName, $obj) private static function deleteOwnsOne(string $propertyName, $obj)
{ {
if (\is_object($obj)) { if (!\is_object($obj)) {
return $obj;
}
/** @var string $mapper */ /** @var string $mapper */
$mapper = static::$ownsOne[$propertyName]['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? // 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 $mapper::delete($obj);
}
return $obj;
} }
/** /**
@ -1678,14 +1667,15 @@ class DataMapperAbstract implements DataMapperInterface
*/ */
private static function deleteBelongsTo(string $propertyName, $obj) private static function deleteBelongsTo(string $propertyName, $obj)
{ {
if (\is_object($obj)) { if (!\is_object($obj)) {
return $obj;
}
/** @var string $mapper */ /** @var string $mapper */
$mapper = static::$belongsTo[$propertyName]['mapper']; $mapper = static::$belongsTo[$propertyName]['mapper'];
return $mapper::delete($obj); return $mapper::delete($obj);
}
return $obj;
} }
/** /**
@ -2116,7 +2106,10 @@ class DataMapperAbstract implements DataMapperInterface
public static function populateAbstractArray(array $result, array $obj, array $columns, int $depth = 3) : array public static function populateAbstractArray(array $result, array $obj, array $columns, int $depth = 3) : array
{ {
foreach ($result as $column => $value) { foreach ($result as $column => $value) {
if (isset($columns[$column]['internal'])) { if (!isset($columns[$column]['internal'])) {
continue;
}
$path = $columns[$column]['internal']; $path = $columns[$column]['internal'];
if (\stripos($path, '/') !== false) { if (\stripos($path, '/') !== false) {
$path = \explode('/', $path); $path = \explode('/', $path);
@ -2139,7 +2132,6 @@ class DataMapperAbstract implements DataMapperInterface
$obj = ArrayUtils::setArray($path, $obj, $value, '/', true); $obj = ArrayUtils::setArray($path, $obj, $value, '/', true);
} }
}
return $obj; return $obj;
} }
@ -2496,14 +2488,9 @@ class DataMapperAbstract implements DataMapperInterface
$obj = []; $obj = [];
foreach ($byKey as $key => $value) { foreach ($byKey as $key => $value) {
$toLoad = []; $toLoad = isset(static::$hasMany[$by]) && static::$hasMany[$by]['self'] !== null
? self::getHasManyPrimaryKeys($value, $by) // todo maybe wrong
if (isset(static::$hasMany[$by]) && static::$hasMany[$by]['self'] !== null) { : self::getPrimaryKeysBy($value, self::getColumnByMember($by));
// todo: maybe wrong?!
$toLoad = self::getHasManyPrimaryKeys($value, $by);
} else {
$toLoad = self::getPrimaryKeysBy($value, self::getColumnByMember($by));
}
$obj[$value] = self::get($toLoad, $relations, $fill, $depth); $obj[$value] = self::get($toLoad, $relations, $fill, $depth);
} }
@ -2543,14 +2530,9 @@ class DataMapperAbstract implements DataMapperInterface
$columnValue = []; $columnValue = [];
foreach ($byKey as $key => $value) { foreach ($byKey as $key => $value) {
$toLoad = []; $toLoad = isset(static::$hasMany[$by]) && static::$hasMany[$by]['self'] !== null
? self::getHasManyPrimaryKeys($value, $by) // @todo: maybe wrong
if (isset(static::$hasMany[$by]) && static::$hasMany[$by]['self'] !== null) { : self::getPrimaryKeysBy($value, self::getColumnByMember($by));
// todo: maybe wrong?!
$toLoad = self::getHasManyPrimaryKeys($value, $by);
} else {
$toLoad = self::getPrimaryKeysBy($value, self::getColumnByMember($by));
}
$columnValue[$value] = self::getColumn($toLoad, $member); $columnValue[$value] = self::getColumn($toLoad, $member);
} }
@ -2592,13 +2574,9 @@ class DataMapperAbstract implements DataMapperInterface
$obj = []; $obj = [];
foreach ($refKey as $key => $value) { foreach ($refKey as $key => $value) {
$toLoad = []; $toLoad = isset(static::$hasMany[$ref]) && static::$hasMany[$ref]['self'] !== null
? self::getHasManyPrimaryKeys($value, $ref)
if (isset(static::$hasMany[$ref]) && static::$hasMany[$ref]['self'] !== null) { : self::getPrimaryKeysBy($value, self::getColumnByMember($ref));
$toLoad = self::getHasManyPrimaryKeys($value, $ref);
} else {
$toLoad = self::getPrimaryKeysBy($value, self::getColumnByMember($ref));
}
$obj[$value] = self::getArray($toLoad, $relations, $depth); $obj[$value] = self::getArray($toLoad, $relations, $depth);
} }
@ -2680,11 +2658,7 @@ class DataMapperAbstract implements DataMapperInterface
$result = $sth->fetchAll(\PDO::FETCH_ASSOC); $result = $sth->fetchAll(\PDO::FETCH_ASSOC);
if ($result === false) { return $result === false ? [] : self::populateIterable($result);
return [];
}
return self::populateIterable($result);
} }
/** /**
@ -2892,8 +2866,6 @@ class DataMapperAbstract implements DataMapperInterface
$results = $sth->fetch(\PDO::FETCH_ASSOC); $results = $sth->fetch(\PDO::FETCH_ASSOC);
return $results === false ? [] : $results; return $results === false ? [] : $results;
} }
@ -2919,11 +2891,8 @@ class DataMapperAbstract implements DataMapperInterface
$sth->execute(); $sth->execute();
$result = $sth->fetchAll(\PDO::FETCH_NUM); $result = $sth->fetchAll(\PDO::FETCH_NUM);
if ($result === false) {
return [];
}
return \array_column($result, 0); return $result === false ? [] : \array_column($result, 0);
} }
/** /**
@ -2948,11 +2917,8 @@ class DataMapperAbstract implements DataMapperInterface
$sth->execute(); $sth->execute();
$result = $sth->fetchAll(\PDO::FETCH_NUM); $result = $sth->fetchAll(\PDO::FETCH_NUM);
if ($result === false) {
return [];
}
return \array_column($result, 0); return $result === false ? [] : \array_column($result, 0);
} }
/** /**
@ -2975,8 +2941,6 @@ class DataMapperAbstract implements DataMapperInterface
$sth = self::$db->con->prepare($query->toSql()); $sth = self::$db->con->prepare($query->toSql());
$sth->execute(); $sth->execute();
$results = $sth->fetchAll(\PDO::FETCH_ASSOC); $results = $sth->fetchAll(\PDO::FETCH_ASSOC);
return $results === false ? [] : $results; return $results === false ? [] : $results;
@ -3015,28 +2979,11 @@ class DataMapperAbstract implements DataMapperInterface
->where($value['table'] . '.' . $value['external'], '=', $primaryKey); ->where($value['table'] . '.' . $value['external'], '=', $primaryKey);
foreach (self::$conditionals as $condKey => $condValue) { foreach (self::$conditionals as $condKey => $condValue) {
if (($column = $value['mapper']::getColumnByMember($condKey)) === null) { if (($column = $value['mapper']::getColumnByMember($condKey)) !== null) {
continue;
}
$query->andWhere($value['table'] . '.' . $column, '=', $condValue); $query->andWhere($value['table'] . '.' . $column, '=', $condValue);
} }
} /*elseif ($relations === RelationType::NEWEST) { }
SELECT c.*, p1.* }
FROM customer c
JOIN purchase p1 ON (c.id = p1.customer_id)
LEFT OUTER JOIN purchase p2 ON (c.id = p2.customer_id AND
(p1.date < p2.date OR p1.date = p2.date AND p1.id < p2.id))
WHERE p2.id IS NULL;
$query->select(static::$table . '.' . static::$primaryField, $value['table'] . '.' . $value['self'])
->from(static::$table)
->join($value['table'])
->on(static::$table . '.' . static::$primaryField, '=', $value['table'] . '.' . $value['external'])
->leftOuterJoin($value['table'])
->on(new And('1', new And(new Or('d1', 'd2'), 'id')))
->where($value['table'] . '.' . $value['external'], '=', 'NULL');
}*/
$sth = self::$db->con->prepare($query->toSql()); $sth = self::$db->con->prepare($query->toSql());
$sth->execute(); $sth->execute();