The Entity--attribute--value EAV mode can facilitate PHP's implementation of the EAV model.
purpose
Entity-attribute-value EAV is a data model that describes the attributes (attributes, parameters) of entities. It can be used to describe their potential huge, but in fact, the number of entities that will be applied to a given entity is relative less.
In mathematics, this pattern is called a sparse matrix.
EAV is also known as the object's attribute value model, vertical database model and open architecture.
UML graphics
Code
- Entity.php
<?php
namespace DesignPatterns\More\EAV;
class Entity
{
/**
* @var \SplObjectStorage
*/
private $values;
/**
* @var string
*/
private $name;
/**
* @param string $name
* @param Value[] $values
*/
public function __construct(string $name, $values)
{
$this->values = new \SplObjectStorage();
$this->name = $name;
foreach ($values as $value) {
$this->values->attach($value);
}
}
public function __toString(): string
{
$text = [$this->name];
foreach ($this->values as $value) {
$text[] = (string) $value;
}
return join(', ', $text);
}
}
- Attribute.php
Value.php
<?php
namespace DesignPatterns\More\EAV;
class Attribute
{
/**
* @var \SplObjectStorage
*/
private $values;
/**
* @var string
*/
private $name;
public function __construct(string $name)
{
$this->values = new \SplObjectStorage();
$this->name = $name;
}
public function addValue(Value $value)
{
$this->values->attach($value);
}
/**
* @return \SplObjectStorage
*/
public function getValues(): \SplObjectStorage
{
return $this->values;
}
public function __toString(): string
{
return $this->name;
}
}
- Value.php
<?php
namespace DesignPatterns\More\EAV;
class Value
{
/**
* @var Attribute
*/
private $attribute;
/**
* @var string
*/
private $name;
public function __construct(Attribute $attribute, string $name)
{
$this->name = $name;
$this->attribute = $attribute;
$attribute->addValue($this);
}
public function __toString(): string
{
return sprintf('%s: %s', $this->attribute, $this->name);
}
}
test
- Tests/EAVTest.php
<?php
namespace DesignPatterns\More\EAV\Tests;
use DesignPatterns\More\EAV\Attribute;
use DesignPatterns\More\EAV\Entity;
use DesignPatterns\More\EAV\Value;
use PHPUnit\Framework\TestCase;
class EAVTest extends TestCase
{
public function testCanAddAttributeToEntity()
{
$colorAttribute = new Attribute('color');
$colorSilver = new Value($colorAttribute, 'silver');
$colorBlack = new Value($colorAttribute, 'black');
$memoryAttribute = new Attribute('memory');
$memory8Gb = new Value($memoryAttribute, '8GB');
$entity = new Entity('MacBook Pro', [$colorSilver, $colorBlack, $memory8Gb]);
$this->assertEquals('MacBook Pro, color: silver, color: black, memory: 8GB', (string) $entity);
}
}
The Growth Path of PHP Internet Architect * The Ultimate Guide to "Design Patterns"
PHP Internet Architect 50K Growth Guide + Industry Problem Solving Guide (Continuous Update)
Interview with 10 companies, get 9 offers, PHP interview questions in 2020
★If you like my article and want to communicate and learn with more senior developers, get more technical consultation and guidance related to interviews with big companies, welcome to join our group, password: phpzh
The latest PHP advanced tutorial in 2020, full series!
If the content is good, I hope everyone will support and encourage you to give a like/like, and welcome to communicate together; in addition, if you have any questions, you can suggest what you want to see in the comments.