chippyash/attributes

Monadic Attribute Maps

1.1.1 2019-02-22 10:45 UTC

This package is auto-updated.

Last update: 2024-12-22 23:22:21 UTC


README

Quality Assurance

PHP 5.6 PHP 7 Build Status Test Coverage Code Climate

The above badges represent the current development branch. As a rule, I don't push to GitHub unless tests, coverage and usability are acceptable. This may not be true for short periods of time; on holiday, need code for some other downstream project etc. If you need stable code, use a tagged version. Read 'Further Documentation' and 'Installation'.

What?

Provides a very strongly typed, but simple, general purpose Attribute Container based on Monadic principles.

Why?

How many times have you written a class with a load of protected parameters (Attributes) and then had to write all the getters and setters? This library provides a general purpose container for those attributes. In addition the Attribute container and the Attributes are Monadic and immutable, meaning that you can guarantee their state.

How

Use the Attribution trait.

use Chippyash\Attributes\Attribution;

class MyClass
{
	use Attribution;
}

Your class now has 2 methods:

public function getA(StringType $name): Attribute

public function hasA(StringType $name): bool

To retrieve and test for an Attribute:

You can further use the AttributionSettable trait to add a setter. NB. you need to use both together, as Attributal provides the protected $attributes var to the class;

use Chippyash\Attributes\Attribution;
use Chippyash\Attributes\AttributionSettable;

class MyClass
{
	use Attribution;
	use AttributionSettable;
}

which adds

public function setA(StringType $name, Attribute $attribute): Attribution

	use Chippyash\Attributes\Attribute;
	use Chippyash\Type\String\StringType;
	
	$myClass = new MyClass();
	$attr = new Attribute('bar');
	$attrName = new StringType('foo);
	$test = $myClass->setA($attrName, $attr)->getA($attrName);
	
	echo ($myClass->hasA($attrName) ? 'true' : 'false');
	echo ($myClass->hasA(new StringType('bar)) ? 'true' : 'false');
	

Attributes, being Monadic, have a value which can be retrieved thus:

	$attrValue = $myClass->getA($attrName)->value();
	//or
	$attr = $myClass->getA($attrName);
	$attrValue = $attr();  //using the invokable interface

If you need to, you can define your class to have an Attributal interface when using the trait to communicate to other objects, that attributes can be tested and retrieved. You can use the AttributalSettable interface to communicate that the class allows setting of Attributes, (this of course breaks the immutable interface, so you might want to consider keeping a state history on the AttributeMap);

use Chippyash\Attributes\Attribution;
use Chippyash\Attributes\AttributionSettable;
use Chippyash\Attributes\Attributal;
use Chippyash\Attributes\AttributalSettable;

class MyClass implements Attributal, AttributalSettable
{
	use Attribution;
	use AttributionSettable;
}

Further documentation

More about Monads and here

Test Contract in the docs directory.

Check out ZF4 Packages for more packages

Changing the library

  1. fork it
  2. write the test
  3. amend it
  4. do a pull request

Found a bug you can't figure out?

  1. fork it
  2. write the test
  3. do a pull request

NB. Make sure you rebase to HEAD before your pull request

Or - raise an issue ticket.

Where?

The library is hosted at Github. It is available at Packagist.org

Installation

Install Composer

For production

    "chippyash/attributes": ">=1,<2"

Or to use the latest, possibly unstable version:

    "chippyash/attributes": "dev-master"

For development

Clone this repo, and then run Composer in local repo root to pull in dependencies

    git clone git@github.com:chippyash/attributes.git Attributes
    cd Attributes
    composer install

To run the tests:

    cd Attributes
    vendor/bin/phpunit -c test/phpunit.xml test/

License

This software library is released under the BSD 3 Clause license

This software library is Copyright (c) 2017, Ashley Kitson, UK

History

V1.0.0 Initial Release

V1.0.1 Update dependencies

V1.1.0 Change of license from GPL V3 to BSD 3 Clause