brimshot/pattr

在 PHP 中处理属性的简单方法

v0.1.1 2022-11-03 14:12 UTC

This package is auto-updated.

Last update: 2024-09-05 16:24:29 UTC


README

一个用于在 PHP 中使用属性的简单直接的库。

安装

使用 composer 安装

composer require brimshot/Pattr

或者直接从

https://github.com/brimshot/Pattr

使用方法

Pattr 提供了一些函数,旨在简化 PHP 中的属性操作。

通过包含 vendor/autoload.php(当使用 composer 时)或直接包含 src/Pattr.php,使这些函数在你的代码中可用。

使用 use 指令在源文件顶部导入你打算使用的每个函数,例如。

use function brimshot\Pattr\has_attribute;

库中的函数在类(或实例化的对象)、公共类属性、类方法、类常量和全局可用函数上操作。

要获取关于类或对象属性或方法的信息,将详细信息作为数组传递,即 [Object, MethodName]

函数列表

 

has_attribute(mixed $item, string|array $attribute_or_array_of_attributes, bool $matchChildAttributes = true) : bool

返回布尔值,指示提供的项目是否具有提供的属性或当 $attribute_or_array_of_attributes 是数组时,是否具有提供的所有属性。

默认情况下,将匹配提供的属性的下级类,但可以通过传递第三个参数为 false 来禁用(严格模式)。

$attribute_or_array_of_attributes 也可以是一个字符串(或字符串),在这种情况下,将匹配未限定的类名。

示例

namespace brimshot\Pattr\examples;

use function brimshot\Pattr\has_attribute;

// Define some attributes

#[\Attribute]
class HasALightSaber
{
	public function __construct(public string $color) {}
}

#[\Attribute]
class IsFromTatooine {}


// Define some classes

#[HasALightSaber(color: 'blue')]
class ObiWan {}

#[HasALightSaber(color: 'green')]
#[IsFromTatooine]
class Luke {}


// Check attributes

has_attribute(ObiWan::class, HasALightSaber::class); // returns true

has_attribute(Luke::class, 'HasALightSaber'); // returns true

has_attribute(ObiWan::class, [HasALightSaber::class, IsFromTatooine::class]); // returns false

has_attribute(Luke::class, [HasALightSaber::class, IsFromTatooine::class]); // returns true

has_attribute_callback(mixed $item, string $attribute, callable $callback, $matchChildAttributes = true) : bool

返回布尔值,指示提供的项目是否具有提供的属性,并且当属性实例化时,满足提供的回调。默认情况下,也将匹配提供的属性的下级属性。

示例

use function brimshot\Pattr\has_attribute_callback;

has_attribute_callback(Luke::class, HasALightSaber::class, fn($a) => $a->color == 'green'); // returns true

has_attribute_callback(ObiWan::class, HasALightSaber::class, fn($a) => $a->color == 'green'); // returns false

does_not_have_attribute(mixed $item, string|array $attribute_or_array_of_attributes, bool $matchChildAttributes = true) : bool

如果提供的项目没有提供的属性或如果 $attribute_or_array_of_attributes 是数组,则返回 true,如果项目没有提供的任何属性,则返回 true。

示例

use function brimshot\Pattr\does_not_have_attribute;

does_not_have_attribute(ObiWan::class, IsFromTatooine::class); // returns true

does_not_have_attribute(ObiWan::class, [IsFromTatooine::class, HasALightSaber::class]); // returns false

get_attribute_names(mixed $item, bool $shortNames = false) : array

返回提供的项目属性的名称。默认为完全限定的类名,但可以通过传递 true 为 $shortNames 来缩短名称。

示例

use function brimshot\Pattr\get_attribute_names;

/*
Array
(
    [0] => Pattr\examples\HasALightSaber
    [1] => Pattr\examples\IsFromTatooine
)
*/
print_r(get_attribute_names(Luke::class));

/*
Array
(
    [0] => HasALightSaber
    [1] => IsFromTatooine
)
*/
print_r(get_attribute_names(Luke::class, true)); // Pass true as the second argument to return short names

get_attribute(mixed $item, string $attribute, bool $matchChildAttributes = true, int $index = 0) : ?object

返回从提供的项目请求的属性的实例,如果项目没有所需的属性,则返回 null。

可以提供一个可选的索引参数,用于与重复属性一起使用以选择返回的内容(索引从 0 开始)。

示例

use function brimshot\Pattr\get_attribute;

$a = get_attribute(Luke::class, HasALightSaber::class);

// Pattr\examples\HasALightSaber
echo get_class($a) . "\n";

// "green"
echo $a->color . "\n";

get_attributes(mixed $item, array $attribute_list = [], bool $matchChildAttributes = true) : array

返回提供的项目属性的实例数组。可以通过传递 $attribute_list 来过滤到所需的子集。默认情况下,筛选列表中提供的属性的后代包括在结果集中。

示例

namespace brimshot\Pattr\examples;

use function brimshot\Pattr\get_attributes;

#[HasALightSaber(color: 'green')]
#[IsFromTatooine]
class Luke 
{
    #[UsesTheForce]
    #[JediPower]
    public function useJediMindTrick() {}
}

/*
Array
(
    [0] => brimshot\Pattr\examples\UsesTheForce Object
        (
        )

    [1] => brimshot\Pattr\examples\JediPower Object
        (
        )

)
*/
print_r(get_attributes([Luke::class, 'useJediMindTrick'])); // Get the attributes of a class method

/*
Array
(
    [0] => Pattr\examples\HasALightSaber Object
        (
            [color] => green
        )
)
*/
print_r(get_attributes(Luke::class, [HasALightSaber::class])); // Get the attributes of a class, filtered to only return the desired results
print_r(get_attributes(Luke::class, ['HasALightSaber'])); // Same result

get_attributes_callback(mixed $item, string $attribute, callable $callback, $matchChildAttributes = true) : array

返回从提供的项目请求的属性(数组包含重复属性的多项结果)的实例数组,当这些属性满足提供的回调时。如果没有匹配项,则返回空数组。

use function brimshot\Pattr\get_attributes_callback;

$matches = get_attributes_callback(Luke::class, HasALightSaber::class, fn($a) => $a->color == 'green');
echo empty($matches) ? 'empty' : get_class($matches[0]); // brimshot\Pattr\examples\HasALightSaber

$matches = get_attributes_callback(ObiWan::class, HasALightSaber::class, fn($a) => $a->color == 'green');
echo empty($matches) ? 'empty' : get_class($matches[0]); // 'empty'

get_class_methods_with_attribute(object|string $object_or_class, string|array $attribute_or_array_of_attributes, bool $matchChildAttributes = true) : array

从给定的类或对象中返回具有提供属性或属性数组的函数名数组。默认情况下,搜索列表中提供的属性的子属性包括在结果集中。

示例

use function brimshot\Pattr\get_class_methods_with_attribute;

#[\Attribute]
class UsesTheForce {}

class Luke 
{
    function huntWompRats() {}

    #[UsesTheForce]
    function retrieveLightSaber() {}
    
    #[UsesTheForce]
    function tryToLiftXWing() {}
}

/**
Array
(
    [0] => retrieveLightSaber
    [1] => tryToLiftXWing
)
*/
print_r(get_class_methods_with_attribute(Luke::class, UsesTheForce::class));

get_class_methods_with_attribute_callback(object|string $object_or_class, string $attribute, callable $callback, bool $matchChildAttributes = true) : array

从给定的类或对象中返回具有提供属性且该属性满足提供回调的函数名数组。默认情况下,搜索列表中提供的属性的子属性包括在结果集中。

示例

use function brimshot\Pattr\get_class_properties_with_attribute_callback;

#[\Attribute]
class DoesNotUseTheForce
{
    public function __construct(public string $location) {}
}

class Luke 
{
    #[DoesNotUseTheForce('anywhere')]
    function huntWompRats() {}

    #[DoesNotUseTheForce('Tatooine')]
    function pickUpPowerConverters() {}
}

/**
Array
(
    [0] => pickUpPowerConverters
)
*/
print_r(get_class_methods_with_attribute_callback(Luke::class, DoesNotUseTheForce::class, fn($a) => $a->location == 'Tatooine'));

get_object_properties_with_attribute(object $object, string|array $attribute_or_array_of_attributes, bool $matchChildAttributes = true) : array

从实例化的对象中返回具有提供属性或属性数组的属性名及其值的数组。默认情况下,搜索列表中提供的属性的子属性包括在结果集中。

use function brimshot\Pattr\get_object_properties_with_attribute;

#[\Attribute]
class Weapon {}

class XWing
{
    #[Weapon]
    public $laserCannons = 4;
    
    #[Weapon]
    public $protonTorpedoLaunchers = 2;
}

$ship = new XWing();

/*
Array
(
    [laserCannons] => 4
    [protonTorpedoLaunchers] => 2
)
*/
print_r(get_object_properties_with_attribute($ship, Weapon::class));

get_object_properties_with_attribute_callback(object $object, string $attribute, callable $callback, $matchChildAttributes = true) : array

从实例化的对象中返回具有提供属性且该属性满足提供回调的属性名及其值的数组。

示例

use function brimshot\Pattr\get_object_properties_with_attribute_callback;

#[\Attribute]
class PassengerData
{
    public function __construct(public string $type) {}
}

class MilleniumFalcon
{
    #[PassengerData('public')]
    public $mainRoomCapacity = 30;
    
    #[PassengerData('secret')]
    public $floorCompartmentCapacity = 2;
}

$ship = new MilleniumFalcon();

/*
Array
(
    [mainRoomCapacity] => 30
)
*/
print_r(get_object_properties_with_attribute_callback($ship, PassengerData::class, fn($a) => $a->type == 'public'));

get_class_properties_with_attribute(string $class, string|array $attribute_or_array_of_attributes, bool $matchChildAttributes = true) : array

从类(非实例化)中返回具有提供属性或属性数组的属性名数组。默认情况下,搜索列表中提供的属性的子属性包括在结果集中。

示例

use function brimshot\Pattr\get_class_properties_with_attribute;

/*
(
    [0] => laserCannons
    [1] => protonTorpedoLaunchers
)
*/
print_r(get_class_properties_with_attribute(XWing::class, Weapon::class));

get_class_properties_with_attribute_callback(string $class, string $attribute, callable $callback) : array

从类(非实例化)中返回具有提供属性且该属性满足提供回调的属性名数组。

示例

use function brimshot\Pattr\get_class_properties_with_attribute_callback;

/*
Array
(
    [0] => mainRoomCapacity
)
*/
print_r(get_class_properties_with_attribute_callback(MilleniumFalcon::class, PassengerData::class, fn($a) => $a->type == 'public'));

get_class_constants_with_attribute(string|object $class_or_object, string|array $attribute_or_array_of_attributes) : array

从类或对象中返回具有提供属性或属性数组的常量名及其值的数组。默认情况下,搜索列表中提供的属性的子属性包括在结果集中。

示例

use function brimshot\Pattr\get_class_constants_with_attribute;

#[\Attribute]
class CrewData
{
    public function __construct(public string $type) {}
}

class XWing
{
    #[CrewData('human')]
    const PILOTS = 1;
    
    #[CrewData('droid')]
    const R2_UNITS = 1;
}

/*
Array
(
    [PILOTS] => 1
    [R2_UNITS] => 1
)
*/
print_r(get_class_constants_with_attribute(XWing::class, CrewData::class));

get_class_constants_with_attribute_callback(string|object $class_or_object, string $attribute, callable $callback, bool $matchAttributeChildren = true) : array

从类或对象中返回具有提供属性且该属性满足提供回调的常量名及其值的数组。默认情况下,搜索列表中提供的属性的子属性包括在结果集中。

示例

use function brimshot\Pattr\get_object_properties_with_attribute_callback;

/*
Array
(
    [R2_UNITS] => 1
)
*/
print_r(get_class_constants_with_attribute_callback(XWing::class, CrewData::class, fn($a) => $a->type == 'droid'));

版权所有 (c) Chris Brim 2022,请参阅LICENSE文件