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/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;
}
}