Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
3e199fc460 | |||
a58b6f1358 |
29
sources/attributes/GetOnly.php
Normal file
29
sources/attributes/GetOnly.php
Normal file
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
/**
|
||||
* Отключаю несущественные инспекции (из-за Attribute)
|
||||
*
|
||||
* @noinspection PhpMultipleClassDeclarationsInspection
|
||||
*/
|
||||
|
||||
namespace goodboyalex\php_components_pack\attributes;
|
||||
|
||||
use Attribute;
|
||||
|
||||
/**
|
||||
* Атрибут указания, что параметр является параметром только для чтения и не подлежит маппингу.
|
||||
*
|
||||
* @author Александр Бабаев
|
||||
* @package php_components_pack
|
||||
* @version 1.0
|
||||
* @since 1.0.25
|
||||
*/
|
||||
#[Attribute(flags: Attribute::TARGET_PROPERTY)]
|
||||
final readonly class GetOnly
|
||||
{
|
||||
/**
|
||||
* Конструктор
|
||||
*/
|
||||
public function __construct ()
|
||||
{
|
||||
}
|
||||
}
|
@ -5,8 +5,10 @@ namespace goodboyalex\php_components_pack\classes;
|
||||
use DateTimeImmutable;
|
||||
use DateTimeInterface;
|
||||
use Exception;
|
||||
use goodboyalex\php_components_pack\attributes\GetOnly;
|
||||
use ReflectionClass;
|
||||
use ReflectionException;
|
||||
use ReflectionProperty;
|
||||
use stdClass;
|
||||
use UnitEnum;
|
||||
|
||||
@ -15,7 +17,7 @@ use UnitEnum;
|
||||
*
|
||||
* @author Александр Бабаев
|
||||
* @package php_components_pack
|
||||
* @version 1.0
|
||||
* @version 1.0.1
|
||||
* @since 1.0
|
||||
*/
|
||||
final class ClassMapper
|
||||
@ -59,6 +61,11 @@ final class ClassMapper
|
||||
// -- пропускаю
|
||||
continue;
|
||||
|
||||
// - если свойство маркируется как GetOnly
|
||||
if (self::HasGetOnlyAttributes($from, $name))
|
||||
// -- пропускаю
|
||||
continue;
|
||||
|
||||
// - если свойство не разрешено
|
||||
if (count($options['allowed']) > 0 && !in_array($name, $options['allowed']))
|
||||
// -- пропускаю
|
||||
@ -71,6 +78,32 @@ final class ClassMapper
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Проверяет, есть ли у свойства класса $class атрибуты GetOnly.
|
||||
*
|
||||
* @param object $class Объект класса.
|
||||
* @param string $propertyName Имя свойства.
|
||||
*
|
||||
* @return bool true если у свойства есть атрибут GetOnly, иначе false.
|
||||
*/
|
||||
private static function HasGetOnlyAttributes (object $class, string $propertyName): bool
|
||||
{
|
||||
// Создаем отражение свойства класса
|
||||
try {
|
||||
$reflectionProperty = new ReflectionProperty(get_class($class), $propertyName);
|
||||
}
|
||||
catch (ReflectionException) {
|
||||
// - возвращаю false, если произошла ошибка создания отражения свойства класса
|
||||
return false;
|
||||
}
|
||||
|
||||
// Получаем список атрибутов у данного свойства
|
||||
$attributes = $reflectionProperty->getAttributes(GetOnly::class);
|
||||
|
||||
// Возвращаем true, если атрибут найден, иначе false
|
||||
return !empty($attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Подготавливает значения свойств класса.
|
||||
*
|
||||
|
@ -27,6 +27,7 @@ class ClassMapperTest extends TestCase
|
||||
{
|
||||
require_once __DIR__ . '/../data/A.php';
|
||||
require_once __DIR__ . '/../data/B.php';
|
||||
require_once __DIR__ . '/../../sources/attributes/GetOnly.php';
|
||||
require_once __DIR__ . '/../../sources/classes/classMapper.php';
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user