Merge pull request #163 from bocharsky-bw/eav

Add Entity-Attribute-Value (EAV) pattern
This commit is contained in:
Dominik Liebler 2015-08-26 11:29:10 +02:00
commit 8c7054ce47
13 changed files with 1434 additions and 0 deletions

79
More/EAV/Attribute.php Normal file
View File

@ -0,0 +1,79 @@
<?php
namespace DesignPatterns\More\EAV;
use SplObjectStorage;
/**
* Class Attribute
*/
class Attribute implements ValueAccessInterface
{
/**
* @var SplObjectStorage
*/
private $values;
/**
* @var string
*/
private $name;
public function __construct()
{
$this->values = new SplObjectStorage();
}
/**
* @return SplObjectStorage
*/
public function getValues()
{
return $this->values;
}
/**
* @param ValueInterface $value
* @return $this
*/
public function addValue(ValueInterface $value)
{
if (!$this->values->contains($value)) {
$this->values->attach($value);
}
return $this;
}
/**
* @param ValueInterface $value
* @return $this
*/
public function removeValue(ValueInterface $value)
{
if ($this->values->contains($value)) {
$this->values->detach($value);
}
return $this;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param string $name
* @return $this
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
}

79
More/EAV/Entity.php Normal file
View File

@ -0,0 +1,79 @@
<?php
namespace DesignPatterns\More\EAV;
use SplObjectStorage;
/**
* Class Entity
*/
class Entity implements ValueAccessInterface
{
/**
* @var SplObjectStorage
*/
private $values;
/**
* @var string
*/
private $name;
public function __construct()
{
$this->values = new SplObjectStorage();
}
/**
* @return SplObjectStorage
*/
public function getValues()
{
return $this->values;
}
/**
* @param ValueInterface $value
* @return $this
*/
public function addValue(ValueInterface $value)
{
if (!$this->values->contains($value)) {
$this->values->attach($value);
}
return $this;
}
/**
* @param ValueInterface $value
* @return $this
*/
public function removeValue(ValueInterface $value)
{
if ($this->values->contains($value)) {
$this->values->detach($value);
}
return $this;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param string $name
* @return $this
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
}

125
More/EAV/README.rst Normal file
View File

@ -0,0 +1,125 @@
`Entity-Attribute-Value (EAV)`__
================================
The Entityattributevalue (EAV) pattern in order to implement EAV model with PHP.
Purpose
-------
The Entityattributevalue (EAV) model is a data model to describe entities
where the number of attributes (properties, parameters) that can be used
to describe them is potentially vast, but the number that will actually apply
to a given entity is relatively modest.
Examples
--------
Check full work example in `example.php`_ file.
.. code-block:: php
use DesignPatterns\More\EAV\Entity;
use DesignPatterns\More\EAV\Attribute;
use DesignPatterns\More\EAV\Value;
// Create color attribute
$color = (new Attribute())->setName('Color');
// Create color values
$colorSilver = (new Value($color))->setName('Silver');
$colorGold = (new Value($color))->setName('Gold');
$colorSpaceGrey = (new Value($color))->setName('Space Grey');
// Create memory attribute
$memory = (new Attribute())->setName('Memory');
// Create memory values
$memory4Gb = (new Value($memory))->setName('4GB');
$memory8Gb = (new Value($memory))->setName('8GB');
$memory16Gb = (new Value($memory))->setName('16GB');
// Create storage attribute
$storage = (new Attribute())->setName('Storage');
// Create storage values
$storage128Gb = (new Value($storage))->setName('128GB');
$storage256Gb = (new Value($storage))->setName('256GB');
$storage512Gb = (new Value($storage))->setName('512GB');
$storage1Tb = (new Value($storage))->setName('1TB');
// Create entities with specific values
$mac = (new Entity())
->setName('MacBook')
// colors
->addValue($colorSilver)
->addValue($colorGold)
->addValue($colorSpaceGrey)
// memories
->addValue($memory8Gb)
// storages
->addValue($storage256Gb)
->addValue($storage512Gb)
;
$macAir = (new Entity())
->setName('MacBook Air')
// colors
->addValue($colorSilver)
// memories
->addValue($memory4Gb)
->addValue($memory8Gb)
// storages
->addValue($storage128Gb)
->addValue($storage256Gb)
->addValue($storage512Gb)
;
$macPro = (new Entity())
->setName('MacBook Pro')
// colors
->addValue($colorSilver)
// memories
->addValue($memory8Gb)
->addValue($memory16Gb)
// storages
->addValue($storage128Gb)
->addValue($storage256Gb)
->addValue($storage512Gb)
->addValue($storage1Tb)
;
UML Diagram
-----------
.. image:: uml/uml.png
:alt: EAV UML Diagram
:align: center
Code
----
You can also find these code on `GitHub`_
Test
----
Tests/EntityTest.php
.. literalinclude:: Tests/EntityTest.php
:language: php
:linenos:
Tests/AttributeTest.php
.. literalinclude:: Tests/AttributeTest.php
:language: php
:linenos:
Tests/ValueTest.php
.. literalinclude:: Tests/ValueTest.php
:language: php
:linenos:
.. _`example.php`: https://github.com/domnikl/DesignPatternsPHP/tree/master/More/EAV/example.php
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/More/EAV
.. __: https://en.wikipedia.org/wiki/Entityattributevalue_model

View File

@ -0,0 +1,66 @@
<?php
namespace DesignPatterns\More\EAV\Tests;
use DesignPatterns\More\EAV\Attribute;
use DesignPatterns\More\EAV\Value;
/**
* AttributeTest tests the Attribute model of EAV pattern
*/
class AttributeTest extends \PHPUnit_Framework_TestCase
{
public function testCreationSuccess()
{
$attribute = new Attribute();
$this->assertInstanceOf('\DesignPatterns\More\EAV\Attribute', $attribute);
}
/**
* @depends testCreationSuccess
*/
public function testSetGetName()
{
$attribute = new Attribute();
$attribute->setName('Color');
$this->assertEquals('Color', $attribute->getName());
}
/**
* @depends testCreationSuccess
*/
public function testAddValue()
{
$attribute = new Attribute();
$attribute->setName('Color');
$colorSilver = new Value($attribute);
$colorSilver->setName('Silver');
$colorGold = new Value($attribute);
$colorGold->setName('Gold');
$this->assertTrue($attribute->getValues()->contains($colorSilver));
$this->assertTrue($attribute->getValues()->contains($colorGold));
}
/**
* @depends testAddValue
*/
public function testRemoveValue()
{
$attribute = new Attribute();
$attribute->setName('Color');
$colorSilver = new Value($attribute);
$colorSilver->setName('Silver');
$colorGold = new Value($attribute);
$colorGold->setName('Gold');
$attribute->removeValue($colorSilver);
$this->assertFalse($attribute->getValues()->contains($colorSilver));
$this->assertTrue($attribute->getValues()->contains($colorGold));
}
}

View File

@ -0,0 +1,145 @@
<?php
namespace DesignPatterns\More\EAV\Tests;
use DesignPatterns\More\EAV\Entity;
use DesignPatterns\More\EAV\Attribute;
use DesignPatterns\More\EAV\Value;
/**
* EntityTest tests the Entity model of EAV pattern
*/
class EntityTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider valueProvider
*
* @var string $name
*/
public function testSetGetName($name)
{
$macBook = new Entity();
$macBook->setName($name);
$this->assertEquals($name, $macBook->getName());
}
/**
* @dataProvider valueProvider
*
* @var string $name
* @var Value[] $values
*/
public function testAddValue($name, array $values)
{
$macBook = new Entity();
$macBook->setName($name);
foreach ($values as $value) {
$macBook->addValue($value);
$this->assertTrue($macBook->getValues()->contains($value));
}
$this->assertCount(count($values), $macBook->getValues());
}
/**
* @depends testAddValue
* @dataProvider valueProvider
*
* @var string $name
* @var Value[] $values
*/
public function testRemoveValue($name, array $values)
{
$macBook = new Entity();
$macBook->setName($name);
foreach ($values as $value) {
$macBook->addValue($value);
}
$macBook->removeValue($values[0]);
$this->assertFalse($macBook->getValues()->contains($values[0]));
unset($values[0]);
$this->assertCount(count($values), $macBook->getValues());
}
/**
* @return array
*/
public function valueProvider()
{
// color attribute
$color = new Attribute();
$color->setName('Color');
// color values
$colorSilver = new Value($color);
$colorSilver->setName('Silver');
$colorGold = new Value($color);
$colorGold->setName('Gold');
$colorSpaceGrey = new Value($color);
$colorSpaceGrey->setName('Space Grey');
// memory attribute
$memory = new Attribute();
$memory->setName('Memory');
// memory values
$memory4Gb = new Value($memory);
$memory4Gb->setName('4GB');
$memory8Gb = new Value($memory);
$memory8Gb->setName('8GB');
$memory16Gb = new Value($memory);
$memory16Gb->setName('16GB');
// storage attribute
$storage = new Attribute();
$storage->setName('Storage');
// storage values
$storage128Gb = new Value($storage);
$storage128Gb->setName('128GB');
$storage256Gb = new Value($storage);
$storage256Gb->setName('256GB');
$storage512Gb = new Value($storage);
$storage512Gb->setName('512GB');
$storage1Tb = new Value($storage);
$storage1Tb->setName('1TB');
return array(
array(
'MacBook',
array(
$colorSilver,
$colorGold,
$colorSpaceGrey,
$memory8Gb,
$storage256Gb,
$storage512Gb,
),
),
array(
'MacBook Air',
array(
$colorSilver,
$memory4Gb,
$memory8Gb,
$storage128Gb,
$storage256Gb,
$storage512Gb,
),
),
array(
'MacBook Pro',
array(
$colorSilver,
$memory8Gb,
$memory16Gb,
$storage128Gb,
$storage256Gb,
$storage512Gb,
$storage1Tb,
),
),
);
}
}

View File

@ -0,0 +1,46 @@
<?php
namespace DesignPatterns\More\EAV\Tests;
use DesignPatterns\More\EAV\Attribute;
use DesignPatterns\More\EAV\Value;
/**
* ValueTest tests the Value model of EAV pattern
*/
class ValueTest extends \PHPUnit_Framework_TestCase
{
public function testCreationSuccessWithAttribute()
{
$attribute = new Attribute();
$attribute->setName('Color');
$value = new Value($attribute);
$this->assertInstanceOf('\DesignPatterns\More\EAV\Value', $value);
}
public function testSetGetName()
{
$attribute = new Attribute();
$attribute->setName('Color');
$value = new Value($attribute);
$value->setName('Silver');
$this->assertEquals('Silver', $value->getName());
}
public function testSetGetAttribute()
{
$attribute = new Attribute();
$attribute->setName('Color');
$value = new Value($attribute);
$value->setName('Silver');
$this->assertSame($attribute, $value->getAttribute());
$value->setAttribute($attribute);
$this->assertSame($attribute, $value->getAttribute());
}
}

65
More/EAV/Value.php Normal file
View File

@ -0,0 +1,65 @@
<?php
namespace DesignPatterns\More\EAV;
/**
* Class Value
*/
class Value implements ValueInterface
{
/**
* @var Attribute
*/
private $attribute;
/**
* @var string
*/
private $name;
/**
* @param Attribute $attribute
*/
public function __construct(Attribute $attribute)
{
$attribute->addValue($this);
$this->attribute = $attribute;
}
/**
* @param Attribute $attribute
*/
public function setAttribute(Attribute $attribute)
{
$this->attribute->removeValue($this); // Remove value from current attribute
$attribute->addValue($this); // Add value to new attribute
$this->attribute = $attribute;
}
/**
* @return Attribute
*/
public function getAttribute()
{
return $this->attribute;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param string $name
* @return $this
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace DesignPatterns\More\EAV;
/**
* Interface ValueAccessInterface
*/
interface ValueAccessInterface
{
/**
* @return \SplObjectStorage
*/
public function getValues();
/**
* @param ValueInterface $value
*/
public function addValue(ValueInterface $value);
/**
* @param ValueInterface $value
*/
public function removeValue(ValueInterface $value);
}

View File

@ -0,0 +1,24 @@
<?php
namespace DesignPatterns\More\EAV;
/**
* Interface ValueInterface
*/
interface ValueInterface
{
/**
* @param Attribute $attribute
*/
public function __construct(Attribute $attribute);
/**
* @param Attribute $attribute
*/
public function setAttribute(Attribute $attribute);
/**
* @return Attribute
*/
public function getAttribute();
}

70
More/EAV/example.php Normal file
View File

@ -0,0 +1,70 @@
<?php
require '../../vendor/autoload.php';
use DesignPatterns\More\EAV\Entity;
use DesignPatterns\More\EAV\Attribute;
use DesignPatterns\More\EAV\Value;
// Create color attribute
$color = (new Attribute())->setName('Color');
// Create color values
$colorSilver = (new Value($color))->setName('Silver');
$colorGold = (new Value($color))->setName('Gold');
$colorSpaceGrey = (new Value($color))->setName('Space Grey');
// Create memory attribute
$memory = (new Attribute())->setName('Memory');
// Create memory values
$memory4Gb = (new Value($memory))->setName('4GB');
$memory8Gb = (new Value($memory))->setName('8GB');
$memory16Gb = (new Value($memory))->setName('16GB');
// Create storage attribute
$storage = (new Attribute())->setName('Storage');
// Create storage values
$storage128Gb = (new Value($storage))->setName('128GB');
$storage256Gb = (new Value($storage))->setName('256GB');
$storage512Gb = (new Value($storage))->setName('512GB');
$storage1Tb = (new Value($storage))->setName('1TB');
// Create entities with specific values
$mac = (new Entity())
->setName('MacBook')
// colors
->addValue($colorSilver)
->addValue($colorGold)
->addValue($colorSpaceGrey)
// memories
->addValue($memory8Gb)
// storages
->addValue($storage256Gb)
->addValue($storage512Gb)
;
$macAir = (new Entity())
->setName('MacBook Air')
// colors
->addValue($colorSilver)
// memories
->addValue($memory4Gb)
->addValue($memory8Gb)
// storages
->addValue($storage128Gb)
->addValue($storage256Gb)
->addValue($storage512Gb)
;
$macPro = (new Entity())
->setName('MacBook Pro')
// colors
->addValue($colorSilver)
// memories
->addValue($memory8Gb)
->addValue($memory16Gb)
// storages
->addValue($storage128Gb)
->addValue($storage256Gb)
->addValue($storage512Gb)
->addValue($storage1Tb)
;

43
More/EAV/uml/EAV.uml Normal file
View File

@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<Diagram>
<ID>PHP</ID>
<OriginalElement>\DesignPatterns\More\Delegation\JuniorDeveloper</OriginalElement>
<nodes>
<node x="256.0" y="553.0">\DesignPatterns\More\EAV\Entity</node>
<node x="0.0" y="154.0">\DesignPatterns\More\EAV\Value</node>
<node x="0.0" y="553.0">\DesignPatterns\More\EAV\Attribute</node>
<node x="150.0" y="400.0">\DesignPatterns\More\EAV\ValueAccessInterface</node>
<node x="-6.0" y="0.0">\DesignPatterns\More\EAV\ValueInterface</node>
</nodes>
<notes />
<edges>
<edge source="\DesignPatterns\More\EAV\Value" target="\DesignPatterns\More\EAV\ValueInterface">
<point x="0.0" y="-100.5" />
<point x="0.0" y="52.0" />
</edge>
<edge source="\DesignPatterns\More\EAV\Entity" target="\DesignPatterns\More\EAV\ValueAccessInterface">
<point x="0.0" y="-100.0" />
<point x="374.0" y="528.0" />
<point x="291.75" y="528.0" />
<point x="45.75" y="51.5" />
</edge>
<edge source="\DesignPatterns\More\EAV\Attribute" target="\DesignPatterns\More\EAV\ValueAccessInterface">
<point x="0.0" y="-100.0" />
<point x="118.0" y="528.0" />
<point x="200.25" y="528.0" />
<point x="-45.75" y="51.5" />
</edge>
</edges>
<settings layout="Hierarchic Group" zoom="1.0" x="246.0" y="376.5" />
<SelectedNodes>
<node>\DesignPatterns\More\EAV\ValueAccessInterface</node>
</SelectedNodes>
<Categories>
<Category>Fields</Category>
<Category>Constants</Category>
<Category>Constructors</Category>
<Category>Methods</Category>
</Categories>
<VISIBILITY>private</VISIBILITY>
</Diagram>

BIN
More/EAV/uml/uml.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

668
More/EAV/uml/uml.svg Normal file

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 92 KiB