jonmldr/property-accessor

此包已被弃用且不再维护。未建议替代包。

PropertyAccessor

v0.1.0 2019-11-17 12:53 UTC

This package is auto-updated.

Last update: 2022-10-23 11:52:37 UTC


README

此PropertyAccessor允许您使用getter、isser、hasser或直接访问(如果属性是公开的)来访问属性。

此库还提供了一个getAccessMethod函数,该函数返回一个实现AccessMethodInterface的对象。此函数告诉您属性如何被访问。

安装

composer require jonmldr/property-accessor

示例

$propertyAccessor = new PropertyAccessor();
// Will throw an `NoAccessMethodException` if there is no method to access the property.
$repositoryName = $propertyAccessor->getValue('users[0].repositories[0].name', $userGroup);

访问方法

$accessMethod = $this->propertyAccessor->getAccessMethod('licensePlate', Car::class);

if ($accessMethod instanceof MethodAccessMethod) {
    // The value will be accessed by a method.
    $methodName = $accessMethod->getMethodName();
} elseif ($accessMethod instanceof ProperyAccessMethod) {
    // The value will be accessed by a (public) property.
    $propertyName = $accessMethod->getPropertyName();
} elseif ($accessMethod === null) {
    // No access method found.
}

测试

通过执行以下命令运行单元测试

./vendor/bin/phpunit tests/ --colors=auto