PHPackages                             chippyash/attributes - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. chippyash/attributes

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

chippyash/attributes
====================

Monadic Attribute Maps

1.1.1(7y ago)088111BSD-3-ClausePHPPHP &gt;=5.6CI failing

Since Feb 22Pushed 7y ago1 watchersCompare

[ Source](https://github.com/chippyash/attributes)[ Packagist](https://packagist.org/packages/chippyash/attributes)[ RSS](/packages/chippyash-attributes/feed)WikiDiscussions master Synced 2w ago

READMEChangelogDependencies (4)Versions (2)Used By (1)

chippyash/attributes
====================

[](#chippyashattributes)

Quality Assurance
-----------------

[](#quality-assurance)

[![PHP 5.6](https://camo.githubusercontent.com/88093c79af42bd3c07f4d6aa378289e1f5450411c56753b0323bd7d8b9b1f9ee/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d352e362d626c75652e737667)](https://camo.githubusercontent.com/88093c79af42bd3c07f4d6aa378289e1f5450411c56753b0323bd7d8b9b1f9ee/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d352e362d626c75652e737667)[![PHP 7](https://camo.githubusercontent.com/d23ce60b89c28c023d0ca69981ec9afbb17eb08a9cd1b609fd84c15d0732b7ce/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d372d626c75652e737667)](https://camo.githubusercontent.com/d23ce60b89c28c023d0ca69981ec9afbb17eb08a9cd1b609fd84c15d0732b7ce/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d372d626c75652e737667)[![Build Status](https://camo.githubusercontent.com/27b67b215d554b2e6506316fe501508e410106d6d5cb9c31ef71d1e1c1231317/68747470733a2f2f7472617669732d63692e6f72672f6368697070796173682f617474726962757465732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/chippyash/attributes.svg?branch=master)[![Test Coverage](https://camo.githubusercontent.com/9cb60811f7bc861eec2086b33261a9dab2960274ab78a269439b3f55240caaac/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f6368697070796173682f617474726962757465732f6261646765732f636f7665726167652e737667)](https://codeclimate.com/github/chippyash/attributes/badges)[![Code Climate](https://camo.githubusercontent.com/ccca7215a7345cf1cb6637ddb58ed4fd0ec53efaf085aa9e52bac45f6390a0ba/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f6368697070796173682f617474726962757465732f6261646765732f6770612e737667)](https://codeclimate.com/github/chippyash/attributes/badges)

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?
-----

[](#what)

Provides a very strongly typed, but simple, general purpose Attribute Container based on [Monadic](https://github.com/chippyash/monad) principles.

Why?
----

[](#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
---

[](#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
---------------------

[](#further-documentation)

More about [Monads](https://github.com/chippyash/monad) and [here](http://zf4.biz/blog/functional-programming-monads)

[Test Contract](https://github.com/chippyash/attributes/blob/master/docs/Test-Contract.md) in the docs directory.

Check out [ZF4 Packages](http://zf4.biz/packages?utm_source=github&utm_medium=web&utm_campaign=blinks&utm_content=validation) for more packages

Changing the library
--------------------

[](#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?
------

[](#where)

The library is hosted at [Github](https://github.com/chippyash/attributes). It is available at [Packagist.org](https://packagist.org/packages/chippyash/attributes)

### Installation

[](#installation)

Install [Composer](https://getcomposer.org/)

#### For production

[](#for-production)

```
    "chippyash/attributes": ">=1,
