PHPackages                             chippyash/identity - 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. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. chippyash/identity

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

chippyash/identity
==================

Class Identity Management

1.1.0(7y ago)02.4k11BSD-3-ClausePHPPHP &gt;=5.6CI failing

Since Dec 27Pushed 7y ago1 watchersCompare

[ Source](https://github.com/chippyash/identity)[ Packagist](https://packagist.org/packages/chippyash/identity)[ Docs](http://zf4.biz/packages?utm_source=packagist&utm_medium=web&utm_campaign=blinks&utm_content=identity)[ RSS](/packages/chippyash-identity/feed)WikiDiscussions master Synced 2w ago

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

chippyash/Identity
==================

[](#chippyashidentity)

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/dd9c22858706240a16808c83b264f467b6123ecb0b2dc0e0f037ccf8a00b41ba/68747470733a2f2f7472617669732d63692e6f72672f6368697070796173682f6964656e746974792e737667)](https://travis-ci.org/chippyash/Identity)[![Test Coverage](https://camo.githubusercontent.com/3d9ff0d4bb5a5af265b5e87845df94e60f91db7de15925e306c1f84aa923b64d/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f66633838353461653431386561636439386433642f746573745f636f766572616765)](https://codeclimate.com/github/chippyash/identity/test_coverage)[![Maintainability](https://camo.githubusercontent.com/8e9dd391647375cb81eb7884bfe51241984813abb45d218687b1c4c2874e8ea2/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f66633838353461653431386561636439386433642f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/chippyash/identity/maintainability)

See the [Test Contract](https://github.com/chippyash/identity/blob/master/docs/Test-Contract.md)

What?
-----

[](#what)

Provides a simple helper capability for class Identity management

Why?
----

[](#why)

This is one of those bits of code you write over and over. Many applications require the classes they use to have some form of Identity. Typically in data driven applications this will equate to the `id` primary key field. This library provides support for that.

### Roadmap

[](#roadmap)

If you want more, either suggest it, or better still, fork it and provide a pull request.

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

How
---

[](#how)

### Coding Basics

[](#coding-basics)

The code is supplied as an interface and a trait. Simply declare your class as implementing the `Identifiable` interface and then use the `Identifying` trait in the class to supply the functionality.

Identities are based on Chippyash\\Type\\Interfaces\\TypeInterface. This allows for strong typing and enforcement of identity rules. For instance, here is an example of a product id that is a fixed length digit string.

```
use Chippyash\Type\String\DigitType;

/**
 * A unique identifier
 */
class Identifier extends DigitType
{
    /**
     * Length of product id, it will be front padded with zeros to this length
     * or cut to this length
     *
     * @var int
     */
    protected $length = 10;

    /**
     * @return int
     */
    public function getLength()
    {
        return $this->length;
    }

    /**
     * @param mixed $value
     *
     * @return string
     */
    protected function typeOf($value)
    {
        $v = parent::typeOf($value);
        if (strlen($v) > $this->length) {
            return substr($v, -$this->length);
        }

        return str_pad($v, $this->length, '0',STR_PAD_LEFT);
    }
}
```

Your class can then simpy use it thus:

```
use Chippyash\Identity\Identifiable;
use Chippyash\Identity\Identifying;

class Product implements Identifiable
{
	use Identifying;

	public function __construct(Identifier $id)
	{
		$this->id = $id;
		//or maybe there is some other way of establishing the identity
	}
}

$product = new Product(new Identifier(53));
$id = $product->id(); // returns Identifier
$vId = $product->vId(); //returns '0000000053'
$vId = $product->id()->get(); //returns '0000000053'
```

### 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/identity). It is available at [Packagist.org](https://packagist.org/packages/chippyash/identity)

### Installation

[](#installation)

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

#### For production

[](#for-production)

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