From 461d612b9102b73aa9e1056f484c75e62568d526 Mon Sep 17 00:00:00 2001 From: Dominik Liebler Date: Thu, 22 Sep 2016 15:51:45 +0200 Subject: [PATCH] PHP7 More --- More/EAV/Attribute.php | 65 +++----------- More/EAV/Entity.php | 76 ++++------------ More/EAV/README.rst | 109 ++++------------------ More/EAV/Tests/AttributeTest.php | 66 -------------- More/EAV/Tests/EAVTest.php | 24 +++++ More/EAV/Tests/EntityTest.php | 145 ------------------------------ More/EAV/Tests/ValueTest.php | 46 ---------- More/EAV/Value.php | 55 ++---------- More/EAV/ValueAccessInterface.php | 24 ----- More/EAV/ValueInterface.php | 24 ----- More/EAV/example.php | 67 -------------- 11 files changed, 84 insertions(+), 617 deletions(-) delete mode 100644 More/EAV/Tests/AttributeTest.php create mode 100644 More/EAV/Tests/EAVTest.php delete mode 100644 More/EAV/Tests/EntityTest.php delete mode 100644 More/EAV/Tests/ValueTest.php delete mode 100644 More/EAV/ValueAccessInterface.php delete mode 100644 More/EAV/ValueInterface.php delete mode 100644 More/EAV/example.php diff --git a/More/EAV/Attribute.php b/More/EAV/Attribute.php index dda66e9..10ac638 100644 --- a/More/EAV/Attribute.php +++ b/More/EAV/Attribute.php @@ -2,12 +2,10 @@ namespace DesignPatterns\More\EAV; -use SplObjectStorage; - -class Attribute implements ValueAccessInterface +class Attribute { /** - * @var SplObjectStorage + * @var \SplObjectStorage */ private $values; @@ -16,64 +14,27 @@ class Attribute implements ValueAccessInterface */ 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; } - /** - * @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() + public function __toString(): string { return $this->name; } - - /** - * @param string $name - * - * @return $this - */ - public function setName($name) - { - $this->name = $name; - - return $this; - } } diff --git a/More/EAV/Entity.php b/More/EAV/Entity.php index e98f11a..96d6583 100644 --- a/More/EAV/Entity.php +++ b/More/EAV/Entity.php @@ -2,12 +2,10 @@ namespace DesignPatterns\More\EAV; -use SplObjectStorage; - -class Entity implements ValueAccessInterface +class Entity { /** - * @var SplObjectStorage + * @var \SplObjectStorage */ private $values; @@ -16,64 +14,28 @@ class Entity implements ValueAccessInterface */ 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 - * - * @return $this + * @param Value[] $values */ - public function setName($name) + public function __construct(string $name, $values) { + $this->values = new \SplObjectStorage(); $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); } } diff --git a/More/EAV/README.rst b/More/EAV/README.rst index b8a8fbe..f23c71b 100644 --- a/More/EAV/README.rst +++ b/More/EAV/README.rst @@ -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 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 ----------- @@ -98,28 +23,32 @@ Code 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 ---- -Tests/EntityTest.php +Tests/EAVTest.php .. literalinclude:: Tests/EntityTest.php :language: php :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 .. __: https://en.wikipedia.org/wiki/Entity–attribute–value_model diff --git a/More/EAV/Tests/AttributeTest.php b/More/EAV/Tests/AttributeTest.php deleted file mode 100644 index 4affe41..0000000 --- a/More/EAV/Tests/AttributeTest.php +++ /dev/null @@ -1,66 +0,0 @@ -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)); - } -} diff --git a/More/EAV/Tests/EAVTest.php b/More/EAV/Tests/EAVTest.php new file mode 100644 index 0000000..a210098 --- /dev/null +++ b/More/EAV/Tests/EAVTest.php @@ -0,0 +1,24 @@ +assertEquals('MacBook Pro, color: silver, color: black, memory: 8GB', (string) $entity); + } +} diff --git a/More/EAV/Tests/EntityTest.php b/More/EAV/Tests/EntityTest.php deleted file mode 100644 index ecd6c40..0000000 --- a/More/EAV/Tests/EntityTest.php +++ /dev/null @@ -1,145 +0,0 @@ -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, - ), - ), - ); - } -} diff --git a/More/EAV/Tests/ValueTest.php b/More/EAV/Tests/ValueTest.php deleted file mode 100644 index f80f070..0000000 --- a/More/EAV/Tests/ValueTest.php +++ /dev/null @@ -1,46 +0,0 @@ -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()); - } -} diff --git a/More/EAV/Value.php b/More/EAV/Value.php index e3556c6..f316728 100644 --- a/More/EAV/Value.php +++ b/More/EAV/Value.php @@ -2,7 +2,7 @@ namespace DesignPatterns\More\EAV; -class Value implements ValueInterface +class Value { /** * @var Attribute @@ -14,53 +14,16 @@ class Value implements ValueInterface */ private $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) + public function __construct(Attribute $attribute, string $name) { $this->name = $name; + $this->attribute = $attribute; - return $this; + $attribute->addValue($this); + } + + public function __toString(): string + { + return sprintf('%s: %s', $this->attribute, $this->name); } } diff --git a/More/EAV/ValueAccessInterface.php b/More/EAV/ValueAccessInterface.php deleted file mode 100644 index dde67b2..0000000 --- a/More/EAV/ValueAccessInterface.php +++ /dev/null @@ -1,24 +0,0 @@ -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);