Add Entity-Attribute-Value (EAV) pattern

This commit is contained in:
victor
2015-07-20 18:32:30 +03:00
parent dab22757a7
commit 1b068456c1
5 changed files with 286 additions and 0 deletions

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

@@ -0,0 +1,71 @@
<?php
namespace DesignPatterns\More\EAV;
/**
* Class Attribute
*/
class Attribute implements ValueInterface
{
/**
* @var Value[]|ValueInterface[]|array
*/
private $values = [];
/**
* @var string
*/
private $name;
/**
* @return Value[]|ValueInterface[]|array
*/
public function getValues()
{
return $this->values;
}
/**
* @param Value|ValueInterface $value
* @return $this
*/
public function addValue(ValueInterface $value)
{
// @TODO I think the $value should be checked for uniqueness first to avoid duplication in array.
$this->values[] = $value;
return $this;
}
/**
* @param Value|ValueInterface $value
* @return $this
*/
public function removeValue(ValueInterface $value)
{
if ($key = array_search($value, $this->values, true)) {
unset($this->values[$key]);
}
return $this;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param string $name
* @return $this
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
}

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

@@ -0,0 +1,71 @@
<?php
namespace DesignPatterns\More\EAV;
/**
* Class Entity
*/
class Entity implements ValueInterface
{
/**
* @var Value[]|ValueInterface[]|array
*/
private $values = [];
/**
* @var string
*/
private $name;
/**
* @return Value[]|ValueInterface[]|array
*/
public function getValues()
{
return $this->values;
}
/**
* @param Value|ValueInterface $value
* @return $this
*/
public function addValue(ValueInterface $value)
{
// @TODO I think the $value should be checked for uniqueness first to avoid duplication in array.
$this->values[] = $value;
return $this;
}
/**
* @param Value|ValueInterface $value
* @return $this
*/
public function removeValue(ValueInterface $value)
{
if ($key = array_search($value, $this->values, true)) {
unset($this->values[$key]);
}
return $this;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param string $name
* @return $this
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
}

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

@@ -0,0 +1,52 @@
<?php
namespace DesignPatterns\More\EAV;
/**
* Class Value
*/
class Value
{
/**
* @var Attribute
*/
private $attribute;
/**
* @var string
*/
private $name;
public function __construct(Attribute $attribute)
{
$attribute->addValue($this);
$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 ValueInterface
*/
interface ValueInterface
{
/**
* @return Value[]|ValueInterface[]|array
*/
public function getValues();
/**
* @param Value|ValueInterface $value
*/
public function addValue(ValueInterface $value);
/**
* @param Value|ValueInterface $value
*/
public function removeValue(ValueInterface $value);
}

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

@@ -0,0 +1,68 @@
<?php
use DesignPatterns\More\EAV\Entity;
use DesignPatterns\More\EAV\Attribute;
use DesignPatterns\More\EAV\Value;
// color attribute
$color = (new Attribute())->setName('Color');
// color values
$colorSilver = (new Value($color))->setName('Silver');
$colorGold = (new Value($color))->setName('Gold');
$colorSpaceGrey = (new Value($color))->setName('Space Grey');
// memory attribute
$memory = (new Attribute())->setName('Memory');
// memory values
$memory4Gb = (new Value($memory))->setName('4GB');
$memory8Gb = (new Value($memory))->setName('8GB');
$memory16Gb = (new Value($memory))->setName('16GB');
// storage attribute
$storage = (new Attribute())->setName('Storage');
// 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');
// entities
$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)
;