20250222#1
This commit is contained in:
93
src/Repository/AbstractRepository.php
Normal file
93
src/Repository/AbstractRepository.php
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
16
src/Repository/RepositoryException.php
Normal file
16
src/Repository/RepositoryException.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace Rmphp\Storage\Repository;
|
||||
|
||||
use Throwable;
|
||||
|
||||
class RepositoryException extends \Exception {
|
||||
|
||||
public array $data;
|
||||
|
||||
public function __construct($message="", $code=0, array $data = [], Throwable $previous=null) {
|
||||
parent::__construct($message, $code, $previous);
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
}
|
||||
46
src/Repository/RepositoryInterface.php
Normal file
46
src/Repository/RepositoryInterface.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Zuev Yuri
|
||||
* Date: 12.01.2025
|
||||
* Time: 21:48
|
||||
*/
|
||||
|
||||
namespace Rmphp\Storage\Repository;
|
||||
|
||||
|
||||
interface RepositoryInterface {
|
||||
|
||||
/**
|
||||
* @param object $object
|
||||
* @param callable|null $method
|
||||
* @return array
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
public function getProperties(object $object, callable $method = null) : array;
|
||||
|
||||
|
||||
/**
|
||||
* @param string $class
|
||||
* @param array|object $data
|
||||
* @return mixed
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
public function createFromData(string $class, array|object $data) : mixed;
|
||||
|
||||
|
||||
/**
|
||||
* @param object $object
|
||||
* @param array|object $data
|
||||
* @return mixed
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
public function updateFromData(object $object, array|object $data) : mixed;
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getRepositoryStack() : array;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user