mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-02 13:07:27 +02:00
Add Entity-Attribute-Value (EAV) pattern
This commit is contained in:
71
More/EAV/Attribute.php
Normal file
71
More/EAV/Attribute.php
Normal 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
71
More/EAV/Entity.php
Normal 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
52
More/EAV/Value.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
24
More/EAV/ValueInterface.php
Normal file
24
More/EAV/ValueInterface.php
Normal 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
68
More/EAV/example.php
Normal 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)
|
||||||
|
;
|
Reference in New Issue
Block a user