2017 © Pedro Peláez
 

library attributes

Monadic Attribute Maps

image

chippyash/attributes

Monadic Attribute Maps

  • Thursday, July 5, 2018
  • by chippyash
  • Repository
  • 1 Watchers
  • 0 Stars
  • 159 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 35 % Grown

The README.md

chippyash/attributes

Quality Assurance

PHP 5.6 PHP 7 Build Status Test Coverage Code Climate, (*1)

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'., (*2)

What?

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

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., (*4)

How

Use the Attribution trait., (*5)

use Chippyash\Attributes\Attribution;

class MyClass
{
    use Attribution;
}

Your class now has 2 methods:, (*6)

public function getA(StringType $name): Attribute, (*7)

public function hasA(StringType $name): bool, (*8)

To retrieve and test for an Attribute:, (*9)

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;, (*10)

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

class MyClass
{
    use Attribution;
    use AttributionSettable;
}

which adds, (*11)

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

    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:, (*13)

    $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);, (*14)

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, (*15)

Test Contract in the docs directory., (*16)

Check out ZF4 Packages for more packages, (*17)

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?, (*18)

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

NB. Make sure you rebase to HEAD before your pull request, (*19)

Or - raise an issue ticket., (*20)

Where?

The library is hosted at Github. It is available at Packagist.org, (*21)

Installation

Install Composer, (*22)

For production

<, (*23)

pre> "chippyash/attributes": ">=1,<2" , (*24)

Or to use the latest, possibly unstable version:, (*25)

, (*26)

    "chippyash/attributes": "dev-master"

For development

Clone this repo, and then run Composer in local repo root to pull in dependencies, (*27)

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

To run the tests:, (*28)

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

License

This software library is released under the BSD 3 Clause license, (*29)

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

History

V1.0.0 Initial Release, (*31)

V1.0.1 Update dependencies, (*32)

V1.1.0 Change of license from GPL V3 to BSD 3 Clause, (*33)

The Versions

05/07 2018

dev-master

9999999-dev

Monadic Attribute Maps

  Sources   Download

GPL-3.0 BSD-3-Clause

The Requires

 

The Development Requires

by Ashle Kitson

05/07 2018

1.1.0

1.1.0.0

Monadic Attribute Maps

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Ashle Kitson

03/04 2018

1.0.1

1.0.1.0

Monadic Attribute Maps

  Sources   Download

GPL-3.0

The Requires

 

The Development Requires

by Ashle Kitson

12/06 2017

1.0.0

1.0.0.0

Monadic Attribute Maps

  Sources   Download

GPL-3.0

The Requires

 

The Development Requires

by Ashle Kitson