4.3. Encja-Atrybut-Wartość (Entity-Attribute-Value (EAV))¶
Wzorzec Encja-Atrybut-Wartość pozwala zaimplementować model EAV w PHP.
4.3.1. Przeznaczenie¶
Model Encja-Atrybut-Wartość (EAW, ang. Entity-Attribute-Value - EAV) jest modelem danych, który upraszcza opisywanie encji posiadających potencjalnie wiele atrybutów (właściwości, parametrów), kiedy nie wszystkie z nich są na raz używane.
4.3.2. Diagram UML¶

4.3.3. Code¶
Ten kod znajdziesz również na GitHub.
Entity.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | <?php
declare(strict_types=1);
namespace DesignPatterns\More\EAV;
use SplObjectStorage;
class Entity implements \Stringable
{
/**
* @var SplObjectStorage<Value,Value>
*/
private $values;
/**
* @param Value[] $values
*/
public function __construct(private string $name, array $values)
{
$this->values = new SplObjectStorage();
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <?php
declare(strict_types=1);
namespace DesignPatterns\More\EAV;
use SplObjectStorage;
class Attribute implements \Stringable
{
private SplObjectStorage $values;
public function __construct(private string $name)
{
$this->values = new SplObjectStorage();
}
public function addValue(Value $value): void
{
$this->values->attach($value);
}
public function getValues(): SplObjectStorage
{
return $this->values;
}
public function __toString(): string
{
return $this->name;
}
}
|
Value.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?php
declare(strict_types=1);
namespace DesignPatterns\More\EAV;
class Value implements \Stringable
{
public function __construct(private Attribute $attribute, private string $name)
{
$attribute->addValue($this);
}
public function __toString(): string
{
return sprintf('%s: %s', (string) $this->attribute, $this->name);
}
}
|
4.3.4. Testy¶
Tests/EAVTest.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <?php
declare(strict_types=1);
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(): void
{
$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);
}
}
|