mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-03 13:37:27 +02:00
Refactor with SplObjectStorage
This commit is contained in:
@@ -2,23 +2,30 @@
|
|||||||
|
|
||||||
namespace DesignPatterns\More\EAV;
|
namespace DesignPatterns\More\EAV;
|
||||||
|
|
||||||
|
use SplObjectStorage;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Attribute
|
* Class Attribute
|
||||||
*/
|
*/
|
||||||
class Attribute implements ValueAccessInterface
|
class Attribute implements ValueAccessInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var ValueInterface[]
|
* @var SplObjectStorage
|
||||||
*/
|
*/
|
||||||
private $values = array();
|
private $values;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
private $name;
|
private $name;
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->values = new SplObjectStorage();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return ValueInterface[]
|
* @return SplObjectStorage
|
||||||
*/
|
*/
|
||||||
public function getValues()
|
public function getValues()
|
||||||
{
|
{
|
||||||
@@ -31,8 +38,9 @@ class Attribute implements ValueAccessInterface
|
|||||||
*/
|
*/
|
||||||
public function addValue(ValueInterface $value)
|
public function addValue(ValueInterface $value)
|
||||||
{
|
{
|
||||||
// @TODO I think the $value should be checked for uniqueness first to avoid duplication in array.
|
if (!$this->values->contains($value)) {
|
||||||
$this->values[] = $value;
|
$this->values->attach($value);
|
||||||
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@@ -43,10 +51,8 @@ class Attribute implements ValueAccessInterface
|
|||||||
*/
|
*/
|
||||||
public function removeValue(ValueInterface $value)
|
public function removeValue(ValueInterface $value)
|
||||||
{
|
{
|
||||||
$index = array_search($value, $this->values, true);
|
if ($this->values->contains($value)) {
|
||||||
|
$this->values->detach($value);
|
||||||
if (false !== $index) {
|
|
||||||
unset($this->values[$index]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
|
@@ -2,23 +2,30 @@
|
|||||||
|
|
||||||
namespace DesignPatterns\More\EAV;
|
namespace DesignPatterns\More\EAV;
|
||||||
|
|
||||||
|
use SplObjectStorage;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Entity
|
* Class Entity
|
||||||
*/
|
*/
|
||||||
class Entity implements ValueAccessInterface
|
class Entity implements ValueAccessInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var ValueInterface[]
|
* @var SplObjectStorage
|
||||||
*/
|
*/
|
||||||
private $values = array();
|
private $values;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
private $name;
|
private $name;
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->values = new SplObjectStorage();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return ValueInterface[]
|
* @return SplObjectStorage
|
||||||
*/
|
*/
|
||||||
public function getValues()
|
public function getValues()
|
||||||
{
|
{
|
||||||
@@ -31,8 +38,9 @@ class Entity implements ValueAccessInterface
|
|||||||
*/
|
*/
|
||||||
public function addValue(ValueInterface $value)
|
public function addValue(ValueInterface $value)
|
||||||
{
|
{
|
||||||
// @TODO I think the $value should be checked for uniqueness first to avoid duplication in array.
|
if (!$this->values->contains($value)) {
|
||||||
$this->values[] = $value;
|
$this->values->attach($value);
|
||||||
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@@ -43,10 +51,8 @@ class Entity implements ValueAccessInterface
|
|||||||
*/
|
*/
|
||||||
public function removeValue(ValueInterface $value)
|
public function removeValue(ValueInterface $value)
|
||||||
{
|
{
|
||||||
$index = array_search($value, $this->values, true);
|
if ($this->values->contains($value)) {
|
||||||
|
$this->values->detach($value);
|
||||||
if (false !== $index) {
|
|
||||||
unset($this->values[$index]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
|
@@ -38,12 +38,11 @@ class AttributeTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
$colorSilver = new Value($attribute);
|
$colorSilver = new Value($attribute);
|
||||||
$colorSilver->setName('Silver');
|
$colorSilver->setName('Silver');
|
||||||
$values[] = $colorSilver;
|
|
||||||
$colorGold = new Value($attribute);
|
$colorGold = new Value($attribute);
|
||||||
$colorGold->setName('Gold');
|
$colorGold->setName('Gold');
|
||||||
$values[] = $colorGold;
|
|
||||||
|
|
||||||
$this->assertEquals($values, $attribute->getValues());
|
$this->assertTrue($attribute->getValues()->contains($colorSilver));
|
||||||
|
$this->assertTrue($attribute->getValues()->contains($colorGold));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -51,21 +50,17 @@ class AttributeTest extends \PHPUnit_Framework_TestCase
|
|||||||
*/
|
*/
|
||||||
public function testRemoveValue()
|
public function testRemoveValue()
|
||||||
{
|
{
|
||||||
$values = array();
|
|
||||||
|
|
||||||
$attribute = new Attribute();
|
$attribute = new Attribute();
|
||||||
$attribute->setName('Color');
|
$attribute->setName('Color');
|
||||||
|
|
||||||
$colorSilver = new Value($attribute);
|
$colorSilver = new Value($attribute);
|
||||||
$colorSilver->setName('Silver');
|
$colorSilver->setName('Silver');
|
||||||
$values[] = $colorSilver;
|
|
||||||
$colorGold = new Value($attribute);
|
$colorGold = new Value($attribute);
|
||||||
$colorGold->setName('Gold');
|
$colorGold->setName('Gold');
|
||||||
$values[] = $colorGold;
|
|
||||||
|
|
||||||
$attribute->removeValue($values[0]);
|
$attribute->removeValue($colorSilver);
|
||||||
unset($values[0]);
|
|
||||||
|
|
||||||
$this->assertEquals($values, $attribute->getValues());
|
$this->assertFalse($attribute->getValues()->contains($colorSilver));
|
||||||
|
$this->assertTrue($attribute->getValues()->contains($colorGold));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -37,9 +37,10 @@ class EntityTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
foreach ($values as $value) {
|
foreach ($values as $value) {
|
||||||
$macBook->addValue($value);
|
$macBook->addValue($value);
|
||||||
|
$this->assertTrue($macBook->getValues()->contains($value));
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->assertEquals($values, $macBook->getValues());
|
$this->assertCount(count($values), $macBook->getValues());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -57,11 +58,11 @@ class EntityTest extends \PHPUnit_Framework_TestCase
|
|||||||
foreach ($values as $value) {
|
foreach ($values as $value) {
|
||||||
$macBook->addValue($value);
|
$macBook->addValue($value);
|
||||||
}
|
}
|
||||||
|
|
||||||
$macBook->removeValue($values[0]);
|
$macBook->removeValue($values[0]);
|
||||||
unset($values[0]);
|
|
||||||
|
|
||||||
$this->assertEquals($values, $macBook->getValues());
|
$this->assertFalse($macBook->getValues()->contains($values[0]));
|
||||||
|
unset($values[0]);
|
||||||
|
$this->assertCount(count($values), $macBook->getValues());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -8,7 +8,7 @@ namespace DesignPatterns\More\EAV;
|
|||||||
interface ValueAccessInterface
|
interface ValueAccessInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @return ValueInterface[]
|
* @return \SplObjectStorage
|
||||||
*/
|
*/
|
||||||
public function getValues();
|
public function getValues();
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user