20250222#1

This commit is contained in:
User
2025-02-22 16:22:21 +03:00
parent a54b55cc4e
commit dfbcfdf321
8 changed files with 249 additions and 139 deletions

View File

@@ -0,0 +1,93 @@
<?php
namespace Rmphp\Storage\Repository;
use Exception;
use ReflectionClass;
use ReflectionException;
use Rmphp\Storage\Component\AbstractDataObject;
use Rmphp\Storage\Entity\ValueObjectInterface;
abstract class AbstractRepository extends AbstractDataObject implements RepositoryInterface {
private static array $classes = [];
/** @inheritDoc */
public function getProperties(object $object, callable $method = null) : array {
try{
$class = get_class($object);
if(!isset(self::$classes[$class])) self::$classes[$class] = new ReflectionClass($class);
$fieldValue = [];
foreach(self::$classes[$class]->getProperties() as $property){
if(!$property->isInitialized($object) || is_array($property->getValue($object))) continue;
if(self::$classes[$class]->hasMethod('get'.ucfirst($property->getName()))){
$fieldValue[$property->getName()] = $object->{'get'.ucfirst($property->getName())}($property->getValue($object));
}
elseif($property->hasType() && class_exists($property->getType()->getName()) && $property->getValue($object) instanceof ValueObjectInterface){
$voClass = $property->getType()->getName();
if(!isset(self::$classes[$voClass])) self::$classes[$voClass] = new ReflectionClass($voClass);
$dd[$property->getName()] = self::$classes[$voClass];
$fieldValue[$property->getName()] = $property->getValue($object)->getValue();
}
elseif(is_bool($property->getValue($object))){
$fieldValue[$property->getName()] = (int) $property->getValue($object);
}
else {
$fieldValue[$property->getName()] = $property->getValue($object);
}
if(array_key_exists($property->getName(), $fieldValue)) {
$out[strtolower(preg_replace("'([A-Z])'", "_$1", $property->getName()))] = $fieldValue[$property->getName()];
}
}
//dd($fieldValue);
//dd($dd, self::$classes);
return (isset($method)) ? array_map($method, $out ?? []) : $out ?? [];
}
catch (ReflectionException $exception) {
throw new RepositoryException($exception->getMessage());
}
}
/** @inheritDoc */
public function createFromData(string $class, array|object $data) : object {
try {
if(!isset(self::$classes[$class])) self::$classes[$class] = new ReflectionClass($class);
return self::fillObject(self::$classes[$class], new $class, (is_object($data)) ? get_object_vars($data) : $data);
}
catch (Exception $exception) {
throw new RepositoryException($exception->getMessage());
}
}
/** @inheritDoc */
public function updateFromData(object $object, array|object $data) : object {
try {
$class = get_class($object);
if(!isset(self::$classes[$class])) self::$classes[$class] = new ReflectionClass($class);
return self::fillObject(self::$classes[$class], clone $object, (is_object($data)) ? get_object_vars($data) : $data, true);
}
catch (Exception $exception) {
throw new RepositoryException($exception->getMessage());
}
}
/**
* @return array
*/
public function getRepositoryStack() : array {
return $this->getFillObjectStack();
}
/**
* @return array
*/
public function getClassesCash() : array {
return self::$classes;
}
}