rinsvent/attribute-extractor

PHP 8 attribute extractor

v1.0.1 2022-11-03 14:44 UTC

This package is auto-updated.

Last update: 2024-04-30 00:32:23 UTC


README

pipeline status coverage report

PHP 8 attribute extractor

Установка

composer require rinsvent/attribute-extractor

Пример класса с атрибутами

<?php

namespace Rinsvent\AttributeExtractor\Tests\unit\Listener\fixtures;

use Rinsvent\AttributeExtractor\Tests\unit\Listener\fixtures\Annotation\HeaderKey;
use Rinsvent\AttributeExtractor\Tests\unit\Listener\fixtures\Annotation\PropertyPath;
use Rinsvent\AttributeExtractor\Tests\unit\Listener\fixtures\Annotation\RequestDTO;
use Rinsvent\AttributeExtractor\Tests\unit\Listener\fixtures\Annotation\MultiplePropertyPath;

#[RequestDTO(className: 'HelloRequestDTO', jsonPath: '$.customObject')]
class HelloRequest
{
    #[HeaderKey(key: 'X-Surname')]
    public string $surname;
    public int $age;
    #[PropertyPath(path: 'DTO')]
    public string $dto;
    
    #[MultiplePropertyPath(path: 'dto1')]
    #[MultiplePropertyPath(path: 'dto2')]
    public function getDto(): string
    {
        return $this->dto;
    }
}

Получаем атрибуты класса

use Rinsvent\AttributeExtractor\ClassIterator;

// Инициалищируем
$classIterator = new ClassIterator(HelloRequest::class, RequestDTO::class);
// Получаем первый атрибут
$result = $classIterator[0];

Получаем атрибуты свойства класса

use Rinsvent\AttributeExtractor\PropertyIterator;

// Инициалищируем
$propertyIterator = new PropertyIterator(HelloRequest::class, 'dto', PropertyPath::class);
// Получаем первый атрибут
$result = $propertyIterator[0];

Получаем атрибуты метода класса

use Rinsvent\AttributeExtractor\MethodIterator;

// Инициалищируем
$methodIterator = new MethodIterator(HelloRequest::class, 'getDto', MultiplePropertyPath::class);
// Перебираем все атрибуты
foreach ($methodIterator as $result) {
    // todo реализация 
}

Реализована поддержка получение атрибутов наследников

#[\Attribute]
class RequestDTO
{
    public function __construct(
        public string $className,
        public string $jsonPath = '$',
    ) {}
}

#[\Attribute]
class UserRequestDTO extends RequestDTO
{
    public function __construct(
        public string $className,
        public string $jsonPath = '$.user',
    ) {}
}
use Rinsvent\AttributeExtractor\Tests\unit\Listener\fixtures\Annotation\UserRequestDTO;

class ExtendsRequest
{
    #[UserRequestDTO(className: 'HelloRequestDTO')]
    public object $user;
}
$propertyIterator = new PropertyIterator(ExtendsRequest::class, 'user', RequestDTO::class);
$result = $propertyIterator[0];