brenoroosevelt/php-attributes

处理PHP属性的简单方法

1.0.0 2021-08-05 18:18 UTC

This package is auto-updated.

Last update: 2024-08-28 20:53:09 UTC


README

Build codecov Scrutinizer Code Quality Latest Version Software License

简单提取和处理PHP属性的方法。

要求

  • PHP >= 8.1

安装

composer require brenoroosevelt/php-attributes

使用方法

而不是这样做

<?php

$myAttribute = Attr::class;
$attributes = [];
$relfectionClass = new ReflectionClass(MyClass::class);
foreach ($relfectionClass->getAttributes($myAttribute) as $attribute) {
    $attributes[] = $attribute;
}

foreach ($relfectionClass->getMethods() as $methods) {
    foreach ($methods->getAttributes($myAttribute) as $attribute) {
        $attributes[] = $attribute;
    }
}

foreach ($relfectionClass->getProperties() as $property) {
     /** ... */
}

foreach ($relfectionClass->getReflectionConstants() as $property) {
    /** ... */
}

$instances = array_map(fn(ReflectionAttribute $attr) => $attr->newInstance(), $attributes);

使用此包,您可以简化

<?php
use BrenoRoosevelt\PhpAttributes\Attributes;

$instances = Attributes::extract(MyAttr::class)->fromClass(MyClass::class)->getInstances();

详细说明参数

<?php
use BrenoRoosevelt\PhpAttributes\ParsedAttribtubeCollection;

$extract = 
     Attributes::extract(
        // $attribute: the attribute name (string)
        // default values is NULL (search for all attributes)
        Attribute::class,
        
        // $flag: flags to filter attributes.     
        // default values is 0 (no filter will be applied)
        ReflectionAttribute::IS_INSTANCEOF
    );

所有这些方法都将返回一个ParsedAttribute集合。

  • fromClass(string|object|array $objectOrClass): Collection
  • fromProperties(string|object|array $objectOrClass, string ...$property)
  • fromMethods(string|object|array $objectOrClass, string ...$method)
  • fromClassConstants(string|object|array $objectOrClass, string ...$constant)
  • fromParameters(string|object|array $objectOrClass, string $method, string ...$parameter)
  • fromConstructor(string|object|array $objectOrClass)
  • fromConstructorParameters(string|object|array $objectOrClass, string ...$parameter)

Collection类是不可变且流畅的

<?php
/* $attributes = Attributes::extract()->from... */ 

// Collection
$attributes->add(new ParsedAttribute(...)) // new Collection instance (immutable)
$attributes->merge(new Collection);        // new Collection instance (immutable)
$attributes->getInstances();               // object[] array with attributes instances
$attributes->getTargets();                 // Reflector[] array with Reflection objects target by attributes
$attributes->getAttributes();              // ReflectionAttribute[]
$attributes->count();                      // int
$attributes->isEmpty();                    // bool
$attributes->first();                      // null|(object) ParsedAttribute
$attributes->toArray();                    // ParsedAttribute[]

// Iterable (ParsedAttribute[])
foreach ($attributes as $attr) {
    $attr->attribute(); // ReflectionAttribute
    $attr->target();    // ReflectionClass|ReflectionClassConstant|
                        // ReflectionProperty|ReflectionMethod|ReflectionParameter
}

贡献

运行测试套件

composer test

运行分析

  • 测试套件
  • 静态分析
  • 编码规范
composer check

许可

本项目受MIT许可条款的约束。有关许可权利和限制,请参阅LICENSE文件。