scheb / property-access
此包已被废弃,不再维护。未建议替代包。
一个轻量级的库,用于从对象到数组读取/写入值
v1.0.1
2018-09-06 21:07 UTC
Requires
- php: ^7.1.3
Requires (Dev)
- phpunit/phpunit: ^7.2
This package is auto-updated.
Last update: 2022-01-11 13:25:15 UTC
README
一个从对象和数组读取/写入值的库。
它与 symfony/property-access 类似,但构建方式更为轻量级,因此语法更为简单。
特性
- 从对象/数组读取属性
- 从对象/数组写入属性
- 内置对驼峰式获取器和设置器的支持
- 可以扩展您自己的访问策略
安装
composer require scheb/property-access
如何使用
$valueObject = ...; // Array or object $strategies = [ new \Scheb\PropertyAccess\Strategy\ArrayAccessStrategy(), new \Scheb\PropertyAccess\Strategy\ObjectPropertyAccessStrategy(), new \Scheb\PropertyAccess\Strategy\ObjectGetterSetterAccessStrategy(), ]; $accessor = new \Scheb\PropertyAccess\PropertyAccess($strategies); // Returns the value or null $accessor->getPropertyValue($valueObject, 'propertyName'); // Returns the modified value object or throws FailedSettingPropertyException $accessor->setPropertyValue($valueObject, 'propertyName', 'newValue');
如何扩展
要创建自己的访问策略,实现 Scheb\PropertyAccess\Strategy\PropertyAccessStrategyInterface
并将其实例传递给 Scheb\PropertyAccess\PropertyAccess
构造函数的 $propertyAccessStrategies
参数。
贡献
您可以通过在问题部分创建拉取请求或功能请求来为此库做出贡献。对于拉取请求,请遵循以下指南
- Symfony 代码风格
- PHP7.1 类型提示(包括:返回类型、
void
、可空类型) - 请添加/更新测试用例
- 测试方法应命名为
[method]_[scenario]_[expected result]
要运行测试套件,请使用 composer install
安装依赖项,然后执行 bin/phpunit
。
许可
此扩展包可在 MIT 许可证 下使用。