2017 © Pedro Peláez
 

library data-attributes

image

timostamm/data-attributes

  • Wednesday, October 4, 2017
  • by timostamm
  • Repository
  • 1 Watchers
  • 0 Stars
  • 5 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

PHP Attributes data structure

This library provides a simple attribute lookup, editable attributes and namespaced attributes., (*1)

It is intended to be used by other libraries and makes it easy to implement with the provided Traits., (*2)

Readonly Attributes Collection

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"
}

Mutable Attributes Collection

use TS\Data\Attributes\Mutable;

$attr = new Attributes(['target' => '_blank']);
$attr->set('foo', 'bar');
$attr->remove('target');
$attr->toReadOnly(); // => TS\Data\Attributes\Attributes

Namespaced Attributes Collection

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']

Mutable Namespaced Attributes Collection

use TS\Data\Attributes\MutableNamespaced;

$attr = new Attributes([
  'html::target' => '_blank', 
  'other::foo' => 'bar'
]);

$attr->set('abc::foo', 'bar');
$attr->namespaces(); // => ['html', 'other', 'abc']

Attribute Containers

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

Mutable, Namespaced and combined containers are also available:, (*3)

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');

The Versions

04/10 2017

dev-master

9999999-dev

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.5.9

 

The Development Requires