PHP7 More

This commit is contained in:
Dominik Liebler
2016-09-22 15:51:45 +02:00
parent 2b1de13391
commit 461d612b91
11 changed files with 84 additions and 617 deletions

View File

@@ -2,12 +2,10 @@
namespace DesignPatterns\More\EAV; namespace DesignPatterns\More\EAV;
use SplObjectStorage; class Attribute
class Attribute implements ValueAccessInterface
{ {
/** /**
* @var SplObjectStorage * @var \SplObjectStorage
*/ */
private $values; private $values;
@@ -16,64 +14,27 @@ class Attribute implements ValueAccessInterface
*/ */
private $name; private $name;
public function __construct() public function __construct(string $name)
{ {
$this->values = new SplObjectStorage(); $this->values = new \SplObjectStorage();
$this->name = $name;
}
public function addValue(Value $value)
{
$this->values->attach($value);
} }
/** /**
* @return SplObjectStorage * @return \SplObjectStorage
*/ */
public function getValues() public function getValues(): \SplObjectStorage
{ {
return $this->values; return $this->values;
} }
/** public function __toString(): string
* @param ValueInterface $value
*
* @return $this
*/
public function addValue(ValueInterface $value)
{
if (!$this->values->contains($value)) {
$this->values->attach($value);
}
return $this;
}
/**
* @param ValueInterface $value
*
* @return $this
*/
public function removeValue(ValueInterface $value)
{
if ($this->values->contains($value)) {
$this->values->detach($value);
}
return $this;
}
/**
* @return string
*/
public function getName()
{ {
return $this->name; return $this->name;
} }
/**
* @param string $name
*
* @return $this
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
} }

View File

@@ -2,12 +2,10 @@
namespace DesignPatterns\More\EAV; namespace DesignPatterns\More\EAV;
use SplObjectStorage; class Entity
class Entity implements ValueAccessInterface
{ {
/** /**
* @var SplObjectStorage * @var \SplObjectStorage
*/ */
private $values; private $values;
@@ -16,64 +14,28 @@ class Entity implements ValueAccessInterface
*/ */
private $name; private $name;
public function __construct()
{
$this->values = new SplObjectStorage();
}
/**
* @return SplObjectStorage
*/
public function getValues()
{
return $this->values;
}
/**
* @param ValueInterface $value
*
* @return $this
*/
public function addValue(ValueInterface $value)
{
if (!$this->values->contains($value)) {
$this->values->attach($value);
}
return $this;
}
/**
* @param ValueInterface $value
*
* @return $this
*/
public function removeValue(ValueInterface $value)
{
if ($this->values->contains($value)) {
$this->values->detach($value);
}
return $this;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/** /**
* @param string $name * @param string $name
* * @param Value[] $values
* @return $this
*/ */
public function setName($name) public function __construct(string $name, $values)
{ {
$this->values = new \SplObjectStorage();
$this->name = $name; $this->name = $name;
return $this; 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);
} }
} }

View File

@@ -11,81 +11,6 @@ where the number of attributes (properties, parameters) that can be used
to describe them is potentially vast, but the number that will actually apply to describe them is potentially vast, but the number that will actually apply
to a given entity is relatively modest. to a given entity is relatively modest.
Examples
--------
Check full work example in `example.php`_ file.
.. code-block:: php
use DesignPatterns\More\EAV\Entity;
use DesignPatterns\More\EAV\Attribute;
use DesignPatterns\More\EAV\Value;
// Create color attribute
$color = (new Attribute())->setName('Color');
// Create color values
$colorSilver = (new Value($color))->setName('Silver');
$colorGold = (new Value($color))->setName('Gold');
$colorSpaceGrey = (new Value($color))->setName('Space Grey');
// Create memory attribute
$memory = (new Attribute())->setName('Memory');
// Create memory values
$memory4Gb = (new Value($memory))->setName('4GB');
$memory8Gb = (new Value($memory))->setName('8GB');
$memory16Gb = (new Value($memory))->setName('16GB');
// Create storage attribute
$storage = (new Attribute())->setName('Storage');
// Create 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');
// Create entities with specific values
$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)
;
UML Diagram UML Diagram
----------- -----------
@@ -98,28 +23,32 @@ Code
You can also find these code on `GitHub`_ You can also find these code on `GitHub`_
Entity.php
.. literalinclude:: Entity.php
:language: php
:linenos:
Attribute.php
.. literalinclude:: Attribute.php
:language: php
:linenos:
Value.php
.. literalinclude:: Value.php
:language: php
:linenos:
Test Test
---- ----
Tests/EntityTest.php Tests/EAVTest.php
.. literalinclude:: Tests/EntityTest.php .. literalinclude:: Tests/EntityTest.php
:language: php :language: php
:linenos: :linenos:
Tests/AttributeTest.php
.. literalinclude:: Tests/AttributeTest.php
:language: php
:linenos:
Tests/ValueTest.php
.. literalinclude:: Tests/ValueTest.php
:language: php
:linenos:
.. _`example.php`: https://github.com/domnikl/DesignPatternsPHP/tree/master/More/EAV/example.php
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/More/EAV .. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/More/EAV
.. __: https://en.wikipedia.org/wiki/Entityattributevalue_model .. __: https://en.wikipedia.org/wiki/Entityattributevalue_model

View File

@@ -1,66 +0,0 @@
<?php
namespace DesignPatterns\More\EAV\Tests;
use DesignPatterns\More\EAV\Attribute;
use DesignPatterns\More\EAV\Value;
/**
* AttributeTest tests the Attribute model of EAV pattern.
*/
class AttributeTest extends \PHPUnit_Framework_TestCase
{
public function testCreationSuccess()
{
$attribute = new Attribute();
$this->assertInstanceOf('\DesignPatterns\More\EAV\Attribute', $attribute);
}
/**
* @depends testCreationSuccess
*/
public function testSetGetName()
{
$attribute = new Attribute();
$attribute->setName('Color');
$this->assertEquals('Color', $attribute->getName());
}
/**
* @depends testCreationSuccess
*/
public function testAddValue()
{
$attribute = new Attribute();
$attribute->setName('Color');
$colorSilver = new Value($attribute);
$colorSilver->setName('Silver');
$colorGold = new Value($attribute);
$colorGold->setName('Gold');
$this->assertTrue($attribute->getValues()->contains($colorSilver));
$this->assertTrue($attribute->getValues()->contains($colorGold));
}
/**
* @depends testAddValue
*/
public function testRemoveValue()
{
$attribute = new Attribute();
$attribute->setName('Color');
$colorSilver = new Value($attribute);
$colorSilver->setName('Silver');
$colorGold = new Value($attribute);
$colorGold->setName('Gold');
$attribute->removeValue($colorSilver);
$this->assertFalse($attribute->getValues()->contains($colorSilver));
$this->assertTrue($attribute->getValues()->contains($colorGold));
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace DesignPatterns\More\EAV\Tests;
use DesignPatterns\More\EAV\Attribute;
use DesignPatterns\More\EAV\Entity;
use DesignPatterns\More\EAV\Value;
class EAVTest extends \PHPUnit_Framework_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);
}
}

View File

@@ -1,145 +0,0 @@
<?php
namespace DesignPatterns\More\EAV\Tests;
use DesignPatterns\More\EAV\Attribute;
use DesignPatterns\More\EAV\Entity;
use DesignPatterns\More\EAV\Value;
/**
* EntityTest tests the Entity model of EAV pattern.
*/
class EntityTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider valueProvider
*
* @var string
*/
public function testSetGetName($name)
{
$macBook = new Entity();
$macBook->setName($name);
$this->assertEquals($name, $macBook->getName());
}
/**
* @dataProvider valueProvider
*
* @var string
* @var Value[] $values
*/
public function testAddValue($name, array $values)
{
$macBook = new Entity();
$macBook->setName($name);
foreach ($values as $value) {
$macBook->addValue($value);
$this->assertTrue($macBook->getValues()->contains($value));
}
$this->assertCount(count($values), $macBook->getValues());
}
/**
* @depends testAddValue
* @dataProvider valueProvider
*
* @var string
* @var Value[] $values
*/
public function testRemoveValue($name, array $values)
{
$macBook = new Entity();
$macBook->setName($name);
foreach ($values as $value) {
$macBook->addValue($value);
}
$macBook->removeValue($values[0]);
$this->assertFalse($macBook->getValues()->contains($values[0]));
unset($values[0]);
$this->assertCount(count($values), $macBook->getValues());
}
/**
* @return array
*/
public function valueProvider()
{
// color attribute
$color = new Attribute();
$color->setName('Color');
// color values
$colorSilver = new Value($color);
$colorSilver->setName('Silver');
$colorGold = new Value($color);
$colorGold->setName('Gold');
$colorSpaceGrey = new Value($color);
$colorSpaceGrey->setName('Space Grey');
// memory attribute
$memory = new Attribute();
$memory->setName('Memory');
// memory values
$memory4Gb = new Value($memory);
$memory4Gb->setName('4GB');
$memory8Gb = new Value($memory);
$memory8Gb->setName('8GB');
$memory16Gb = new Value($memory);
$memory16Gb->setName('16GB');
// storage attribute
$storage = new Attribute();
$storage->setName('Storage');
// storage values
$storage128Gb = new Value($storage);
$storage128Gb->setName('128GB');
$storage256Gb = new Value($storage);
$storage256Gb->setName('256GB');
$storage512Gb = new Value($storage);
$storage512Gb->setName('512GB');
$storage1Tb = new Value($storage);
$storage1Tb->setName('1TB');
return array(
array(
'MacBook',
array(
$colorSilver,
$colorGold,
$colorSpaceGrey,
$memory8Gb,
$storage256Gb,
$storage512Gb,
),
),
array(
'MacBook Air',
array(
$colorSilver,
$memory4Gb,
$memory8Gb,
$storage128Gb,
$storage256Gb,
$storage512Gb,
),
),
array(
'MacBook Pro',
array(
$colorSilver,
$memory8Gb,
$memory16Gb,
$storage128Gb,
$storage256Gb,
$storage512Gb,
$storage1Tb,
),
),
);
}
}

View File

@@ -1,46 +0,0 @@
<?php
namespace DesignPatterns\More\EAV\Tests;
use DesignPatterns\More\EAV\Attribute;
use DesignPatterns\More\EAV\Value;
/**
* ValueTest tests the Value model of EAV pattern.
*/
class ValueTest extends \PHPUnit_Framework_TestCase
{
public function testCreationSuccessWithAttribute()
{
$attribute = new Attribute();
$attribute->setName('Color');
$value = new Value($attribute);
$this->assertInstanceOf('\DesignPatterns\More\EAV\Value', $value);
}
public function testSetGetName()
{
$attribute = new Attribute();
$attribute->setName('Color');
$value = new Value($attribute);
$value->setName('Silver');
$this->assertEquals('Silver', $value->getName());
}
public function testSetGetAttribute()
{
$attribute = new Attribute();
$attribute->setName('Color');
$value = new Value($attribute);
$value->setName('Silver');
$this->assertSame($attribute, $value->getAttribute());
$value->setAttribute($attribute);
$this->assertSame($attribute, $value->getAttribute());
}
}

View File

@@ -2,7 +2,7 @@
namespace DesignPatterns\More\EAV; namespace DesignPatterns\More\EAV;
class Value implements ValueInterface class Value
{ {
/** /**
* @var Attribute * @var Attribute
@@ -14,53 +14,16 @@ class Value implements ValueInterface
*/ */
private $name; private $name;
/** public function __construct(Attribute $attribute, string $name)
* @param Attribute $attribute
*/
public function __construct(Attribute $attribute)
{
$this->setAttribute($attribute);
}
/**
* @param Attribute $attribute
*
* @return $this
*/
public function setAttribute(Attribute $attribute)
{
$this->attribute && $this->attribute->removeValue($this); // Remove value from current attribute
$attribute->addValue($this); // Add value to new attribute
$this->attribute = $attribute;
return $this;
}
/**
* @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; $this->name = $name;
$this->attribute = $attribute;
return $this; $attribute->addValue($this);
}
public function __toString(): string
{
return sprintf('%s: %s', $this->attribute, $this->name);
} }
} }

View File

@@ -1,24 +0,0 @@
<?php
namespace DesignPatterns\More\EAV;
/**
* Interface ValueAccessInterface.
*/
interface ValueAccessInterface
{
/**
* @return \SplObjectStorage
*/
public function getValues();
/**
* @param ValueInterface $value
*/
public function addValue(ValueInterface $value);
/**
* @param ValueInterface $value
*/
public function removeValue(ValueInterface $value);
}

View File

@@ -1,24 +0,0 @@
<?php
namespace DesignPatterns\More\EAV;
/**
* Interface ValueInterface.
*/
interface ValueInterface
{
/**
* @param Attribute $attribute
*/
public function __construct(Attribute $attribute);
/**
* @param Attribute $attribute
*/
public function setAttribute(Attribute $attribute);
/**
* @return Attribute
*/
public function getAttribute();
}

View File

@@ -1,67 +0,0 @@
<?php
require '../../vendor/autoload.php';
use DesignPatterns\More\EAV\Attribute;
use DesignPatterns\More\EAV\Entity;
use DesignPatterns\More\EAV\Value;
// Create color attribute
$color = (new Attribute())->setName('Color');
// Create color values
$colorSilver = (new Value($color))->setName('Silver');
$colorGold = (new Value($color))->setName('Gold');
$colorSpaceGrey = (new Value($color))->setName('Space Grey');
// Create memory attribute
$memory = (new Attribute())->setName('Memory');
// Create memory values
$memory4Gb = (new Value($memory))->setName('4GB');
$memory8Gb = (new Value($memory))->setName('8GB');
$memory16Gb = (new Value($memory))->setName('16GB');
// Create storage attribute
$storage = (new Attribute())->setName('Storage');
// Create 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');
// Create entities with specific values
$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);