timostamm / data-attributes
dev-master
2017-10-04 11:53 UTC
Requires
- php: >=5.5.9
Requires (Dev)
- phpunit/phpunit: ~6.3
This package is auto-updated.
Last update: 2024-09-05 18:41:26 UTC
README
此库提供简单的属性查找、可编辑属性和命名空间属性。
它旨在由其他库使用,并可以通过提供的特性轻松实现。
只读属性集合
use TS\Data\Attributes; $attr = new Attributes(['target' => '_blank']); $attr->count(); // => 1 count($attr); // => 1 $attr->get('target'); // => "_blank" $attr->has('target'); // => true $attr->toArray(); // => ['target' => '_blank'] foreach ($attr as $k => $v) { print $k . '=' . $v; // => "target = _blank" }
可变属性集合
use TS\Data\Attributes\Mutable; $attr = new Attributes(['target' => '_blank']); $attr->set('foo', 'bar'); $attr->remove('target'); $attr->toReadOnly(); // => TS\Data\Attributes\Attributes
命名空间属性集合
use TS\Data\Attributes\Namespaced; $attr = new Attributes([ 'html::target' => '_blank', 'other::foo' => 'bar' ]); // list used namespaces $attr->namespaces(); // => ['html', 'other'] // extract entries with the given namespace $attr->extract('html')->toArray(); // => ['target' => '_blank'] // keep the namespace $attr->extract('html', Attributes::KEEP_NAMESPACE)->toArray(); // => ['html::target' => '_blank']
可变命名空间属性集合
use TS\Data\Attributes\MutableNamespaced; $attr = new Attributes([ 'html::target' => '_blank', 'other::foo' => 'bar' ]); $attr->set('abc::foo', 'bar'); $attr->namespaces(); // => ['html', 'other', 'abc']
属性容器
use TS\Data\Attributes\AttributesContainerInterface; use TS\Data\Attributes\AttributesContainerTrait; class My implements AttributesContainerInterface { use AttributesContainerTrait; public function __construct($attrs) { $this->initAttributes($attrs); } } $my = new My([ 'target' => '_blank' ]); $my->hasAttribute('target'); // => true $my->getAttribute('target'); // => "_blank" $my->getAttributes(); // => AttributesInterface
也可提供可变、命名空间和组合容器
use TS\Data\Attributes\Mutable\MutableContainerInterface; use TS\Data\Attributes\Mutable\MutableContainerTrait; class My implements MutableContainerInterface { use MutableContainerTrait; public function __construct($attrs) { $this->initAttributes($attrs); } } $my = new My([ 'target' => '_blank' ]); $my->setAttribute('foo', 'bar');