mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-10 17:04:01 +02:00
Add Entity-Attribute-Value (EAV) pattern
This commit is contained in:
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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user