20240502#1
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Base\Services;
|
||||
namespace Base\Application;
|
||||
|
||||
use ReflectionClass;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Base\Services;
|
||||
namespace Base\Application;
|
||||
|
||||
use Throwable;
|
||||
|
||||
class ServiceException extends \Exception {
|
||||
class ApplicationException extends \Exception {
|
||||
|
||||
public array $data;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Base\Services;
|
||||
namespace Base\Application;
|
||||
|
||||
use Throwable;
|
||||
|
||||
@@ -6,83 +6,12 @@
|
||||
|
||||
namespace Base\Domain;
|
||||
|
||||
use ReflectionClass;
|
||||
|
||||
abstract class AbstractObject {
|
||||
abstract class AbstractObject implements EntityInterface {
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @return static
|
||||
* @throws DomainException
|
||||
* @return int|null
|
||||
*/
|
||||
public static function fromData(array $data) : static {
|
||||
$self = new static();
|
||||
$self->setProperties($data);
|
||||
return $self;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @return void
|
||||
* @throws DomainException
|
||||
*/
|
||||
public function setProperties(array $data) : void {
|
||||
$class = new ReflectionClass(static::class);
|
||||
foreach ($class->getProperties() as $property) {
|
||||
$propertyNameSnakeCase = strtolower(preg_replace("'([A-Z])'", "_$1", $property->getName()));
|
||||
// data[propertyName] ?? data[property_name] ?? null
|
||||
$value = $data[$property->getName()] ?? $data[$propertyNameSnakeCase] ?? null;
|
||||
|
||||
// если есть внутренний метод (приоритетная обработка)
|
||||
if($class->hasMethod('set'.ucfirst($property->getName()))) $this->{'set'.ucfirst($property->getName())}($value);
|
||||
// Если тип свойства класс (valueObject)
|
||||
elseif($property->hasType() && class_exists($property->getType()->getName())) $this->{$property->getName()} = new ($property->getType()->getName())($value);
|
||||
// если значения не пустое
|
||||
elseif(isset($value)) $this->{$property->getName()} = $value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param callable|null $method
|
||||
* @return array
|
||||
*/
|
||||
public function getProperties(callable $method = null) : array {
|
||||
$objectData = get_object_vars($this);
|
||||
foreach ($objectData as $fieldName => $value)
|
||||
{
|
||||
// to option_id
|
||||
$fieldNameSnakeCase = strtolower(preg_replace("'([A-Z])'", "_$1", $fieldName));
|
||||
|
||||
// если есть внутренний метод (приоритетная обработка)
|
||||
if(method_exists($this, 'get'.ucfirst($fieldName))) {
|
||||
$out[$fieldNameSnakeCase] = $this->{'get'.ucfirst($fieldName)}($value);
|
||||
}
|
||||
// если тип свойства класс (valueObject)
|
||||
elseif($value instanceof ValueObjectInterface && null !== $value->get()) {
|
||||
$out[$fieldNameSnakeCase] = $value->get();
|
||||
}
|
||||
// если передана callable функция через которую нужно пропустить все элементы
|
||||
elseif(isset($method) && !is_array($value) && !is_object($value)) {
|
||||
$out[$fieldNameSnakeCase] = $method($value);
|
||||
}
|
||||
// если это логическое значение
|
||||
elseif(is_bool($value)){
|
||||
$out[$fieldNameSnakeCase] = (int) $value;
|
||||
}
|
||||
// если это дробное число
|
||||
elseif(is_float($value)) {
|
||||
$out[$fieldNameSnakeCase] = $value;
|
||||
}
|
||||
// если это целое число
|
||||
elseif(is_int($value)) {
|
||||
$out[$fieldNameSnakeCase] = $value;
|
||||
}
|
||||
// если это строка
|
||||
elseif(is_string($value)) {
|
||||
$out[$fieldNameSnakeCase] = $value;
|
||||
}
|
||||
|
||||
}
|
||||
return $out ?? [];
|
||||
public function getId(): mixed {
|
||||
return (isset($this->id)) ? (($this->id instanceof ValueObjectInterface) ? $this->id->get() : $this->id) : null;
|
||||
}
|
||||
}
|
||||
18
application/src/Domain/EntityInterface.php
Normal file
18
application/src/Domain/EntityInterface.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Zuev Yuri
|
||||
* Date: 23.04.2024
|
||||
* Time: 3:58
|
||||
*/
|
||||
|
||||
namespace Base\Domain;
|
||||
|
||||
interface EntityInterface {
|
||||
|
||||
/**
|
||||
* @return int|null
|
||||
*/
|
||||
public function getId(): mixed;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace Base\Infrastructure\Repository;
|
||||
|
||||
use Base\Domain\EntityInterface;
|
||||
use Rmphp\Storage\Mysql\MysqlStorageInterface;
|
||||
|
||||
abstract class AbstractMysqlRepository extends AbstractRepository {
|
||||
|
||||
public function __construct(
|
||||
public readonly MysqlStorageInterface $mysql
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param EntityInterface $object
|
||||
* @param string $table
|
||||
* @return mixed
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
public function saveEntity(EntityInterface $object, string $table) : mixed {
|
||||
$in = $this->getProperties($object, function ($value){
|
||||
return (is_string($value)) ? $this->mysql->escapeStr($value) : $value;
|
||||
});
|
||||
//dd($object, $in);
|
||||
try {
|
||||
if (!empty($object->getId()) && !empty($this->mysql->findById($table, $object->getId()))) {
|
||||
$this->mysql->updateById($table, $in, $object->getId());
|
||||
return $object->getId();
|
||||
} else {
|
||||
$this->mysql->insert($table, $in);
|
||||
return (is_string($object->getId())) ? $object->getId() : $this->mysql->mysql()->insert_id;
|
||||
}
|
||||
} catch (\Throwable $throwable) {throw new RepositoryException($throwable->getMessage());}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
### Создание объекта из массива
|
||||
|
||||
```php
|
||||
setProperties(array $data) : void
|
||||
```
|
||||
|
||||
|
||||
### Получение массива из объекта
|
||||
|
||||
```php
|
||||
getProperties(callable $method = null) : array
|
||||
```
|
||||
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Zuev Yuri
|
||||
* Date: 25.04.2024
|
||||
* Time: 13:06
|
||||
*/
|
||||
|
||||
namespace Base\Infrastructure\Repository;
|
||||
|
||||
use Base\Domain\ValueObjectInterface;
|
||||
use ReflectionClass;
|
||||
use ReflectionException;
|
||||
|
||||
class AbstractRepository {
|
||||
|
||||
static array $classes = [];
|
||||
|
||||
/**
|
||||
* @param string $class
|
||||
* @param $data
|
||||
* @return mixed
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
public function create(string $class, $data) : mixed {
|
||||
try {
|
||||
if(!isset(static::$classes[$class])) static::$classes[$class] = new ReflectionClass($class);
|
||||
$object = new $class;
|
||||
foreach (static::$classes[$class]->getProperties() as $property) {
|
||||
$propertyNameSnakeCase = strtolower(preg_replace("'([A-Z])'", "_$1", $property->getName()));
|
||||
// data[propertyName] ?? data[property_name] ?? null
|
||||
$value = $data[$property->getName()] ?? $data[$propertyNameSnakeCase] ?? null;
|
||||
|
||||
// если есть внутренний метод (приоритетная обработка)
|
||||
if(static::$classes[$class]->hasMethod('set'.ucfirst($property->getName()))) $object->{'set'.ucfirst($property->getName())}($value);
|
||||
// Если тип свойства класс (valueObject)
|
||||
elseif($property->hasType() && class_exists($property->getType()->getName())) $object->{$property->getName()} = new ($property->getType()->getName())($value);
|
||||
// если значения не пустое
|
||||
elseif(isset($value)) $object->{$property->getName()} = $value;
|
||||
}
|
||||
return $object;
|
||||
}
|
||||
catch (ReflectionException $exception) {
|
||||
throw new RepositoryException($exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param object $class
|
||||
* @param callable|null $method
|
||||
* @return array
|
||||
*/
|
||||
public function getProperties(object $class, callable $method = null) : array {
|
||||
|
||||
$objectData = get_object_vars($class);
|
||||
foreach ($objectData as $fieldName => $value)
|
||||
{
|
||||
// to option_id
|
||||
$fieldNameSnakeCase = strtolower(preg_replace("'([A-Z])'", "_$1", $fieldName));
|
||||
|
||||
// если есть внутренний метод (приоритетная обработка)
|
||||
if(method_exists($this, 'get'.ucfirst($fieldName))) {
|
||||
$out[$fieldNameSnakeCase] = $this->{'get'.ucfirst($fieldName)}($value);
|
||||
}
|
||||
// если тип свойства класс (valueObject)
|
||||
elseif($value instanceof ValueObjectInterface && null !== $value->get()) {
|
||||
$out[$fieldNameSnakeCase] = $value->get();
|
||||
}
|
||||
// если передана callable функция через которую нужно пропустить все элементы
|
||||
elseif(isset($method) && !is_array($value) && !is_object($value)) {
|
||||
$out[$fieldNameSnakeCase] = $method($value);
|
||||
}
|
||||
// если это логическое значение
|
||||
elseif(is_bool($value)){
|
||||
$out[$fieldNameSnakeCase] = (int) $value;
|
||||
}
|
||||
// если это дробное число
|
||||
elseif(is_float($value)) {
|
||||
$out[$fieldNameSnakeCase] = $value;
|
||||
}
|
||||
// если это целое число
|
||||
elseif(is_int($value)) {
|
||||
$out[$fieldNameSnakeCase] = $value;
|
||||
}
|
||||
// если это строка
|
||||
elseif(is_string($value)) {
|
||||
$out[$fieldNameSnakeCase] = $value;
|
||||
}
|
||||
|
||||
}
|
||||
return $out ?? [];
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Base\Repository;
|
||||
namespace Base\Infrastructure\Repository;
|
||||
|
||||
use Throwable;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Base\Controllers;
|
||||
namespace Base\Presentation\Controllers;
|
||||
|
||||
use Laminas\Diactoros\Response\HtmlResponse;
|
||||
use Laminas\Diactoros\Response\JsonResponse;
|
||||
@@ -1,10 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Base\Controllers;
|
||||
namespace Base\Presentation\Controllers;
|
||||
|
||||
use Base\Application\ApplicationException;
|
||||
use Base\Application\DTOException;
|
||||
use Base\Domain\DomainException;
|
||||
use Base\Services\DTOException;
|
||||
use Base\Services\ServiceException;
|
||||
use Exception;
|
||||
use Throwable;
|
||||
|
||||
@@ -29,7 +29,7 @@ abstract class AbstractPageController extends AbstractController {
|
||||
*/
|
||||
public function checkError(Throwable $e) : string {
|
||||
($e instanceof Exception) ? $this->logException($e) : $this->logError($e);
|
||||
if($e instanceof DTOException || $e instanceof DomainException || $e instanceof ServiceException) return $e->getMessage();
|
||||
if($e instanceof DTOException || $e instanceof DomainException || $e instanceof ApplicationException) return $e->getMessage();
|
||||
return "Ошибка. Дата и время: ".date("d-m-Y H:i:s");
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Base\Controllers;
|
||||
namespace Base\Presentation\Controllers;
|
||||
|
||||
class NotFoundException extends \Exception {
|
||||
|
||||
15
application/src/Presentation/templates/base.tpl
Normal file
15
application/src/Presentation/templates/base.tpl
Normal file
@@ -0,0 +1,15 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="ru">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Rmphp - <?=$this->getPoint('title')?></title>
|
||||
<link href="/assets/css/style.css?1" rel="stylesheet">
|
||||
</head>
|
||||
<body>
|
||||
<?=$this->getPoint('main')?>
|
||||
<script type="text/javascript" src="/assets/js/script.js?1"></script>
|
||||
<?=$this->getPoint('jsscript')?>
|
||||
</body>
|
||||
</html>
|
||||
38
application/src/Presentation/templates/error/404.tpl
Normal file
38
application/src/Presentation/templates/error/404.tpl
Normal file
@@ -0,0 +1,38 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="ru">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>404 Page Not Found</title>
|
||||
<style>
|
||||
body{
|
||||
font-family: Arial, sans-serif;
|
||||
font-size: 15px;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
background: #c9d1e1;
|
||||
}
|
||||
.err-page{
|
||||
display: flex;
|
||||
align-items: center;
|
||||
height: 50vh;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
color:#888;
|
||||
}
|
||||
.err-page__header{
|
||||
font-size: 70px;
|
||||
}
|
||||
.err-page__text{
|
||||
font-size: 30px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="err-page">
|
||||
<div class="err-page__header">404</div>
|
||||
<div class="err-page__text">Page Not Found</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
38
application/src/Presentation/templates/error/501.tpl
Normal file
38
application/src/Presentation/templates/error/501.tpl
Normal file
@@ -0,0 +1,38 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="ru">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>501 Not Implemented</title>
|
||||
<style>
|
||||
body{
|
||||
font-family: Arial, sans-serif;
|
||||
font-size: 15px;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
background: #e1c9d1;
|
||||
}
|
||||
.err-page{
|
||||
display: flex;
|
||||
align-items: center;
|
||||
height: 50vh;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
color:#888;
|
||||
}
|
||||
.err-page__header{
|
||||
font-size: 70px;
|
||||
}
|
||||
.err-page__text{
|
||||
font-size: 30px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="err-page">
|
||||
<div class="err-page__header">501</div>
|
||||
<div class="err-page__text">Not Implemented</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
9
application/src/Presentation/templates/error/errpage.tpl
Normal file
9
application/src/Presentation/templates/error/errpage.tpl
Normal file
@@ -0,0 +1,9 @@
|
||||
<main>
|
||||
<div class="main-section">
|
||||
<div class="errBlock">
|
||||
<div class="center">
|
||||
<?=(!empty($this->data->errorText))?$this->data->errorText:""?>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
12
application/src/Presentation/templates/inc/header.tpl
Normal file
12
application/src/Presentation/templates/inc/header.tpl
Normal file
@@ -0,0 +1,12 @@
|
||||
<header class="header">
|
||||
<div class="header__logo">
|
||||
<a class="header__logolink" href="/"> </a>
|
||||
</div>
|
||||
<div class="header__freearea"></div>
|
||||
<div class="header__user"><?=$this->user->fio ?? "Гость"?></div>
|
||||
<div class="header__out">
|
||||
<a href="/?logout">
|
||||
<i class="header__outicon fa-solid fa-right-from-bracket"></i>
|
||||
</a>
|
||||
</div>
|
||||
</header>
|
||||
@@ -1,35 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Base\Repository;
|
||||
|
||||
use Base\Domain\AbstractObject;
|
||||
use Rmphp\Storage\Mysql\MysqlStorageInterface;
|
||||
|
||||
abstract class AbstractMysqlRepository {
|
||||
|
||||
public function __construct(
|
||||
public readonly MysqlStorageInterface $mysql
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
public function saveEntity(AbstractObject $object, string $table) : AbstractObject {
|
||||
$in = $object->getProperties(function ($value){
|
||||
return (is_string($value)) ? $this->mysql->escapeStr($value) : $value;
|
||||
});
|
||||
try {
|
||||
if (!empty($object->id) && !empty($this->mysql->findById($table, $object->id))) {
|
||||
$this->mysql->updateById($table, $in, $object->id);
|
||||
} else {
|
||||
$this->mysql->insert($table, $in);
|
||||
$object->id = $this->mysql->mysql()->insert_id;
|
||||
}
|
||||
} catch (\Throwable $throwable) {throw new RepositoryException($throwable->getMessage());}
|
||||
return $object;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Base\Services;
|
||||
|
||||
use Rmphp\Kernel\Main;
|
||||
|
||||
abstract class AbstractService extends Main {
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user