mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-03-15 11:50:10 +01:00
Refactor with SplObjectStorage
This commit is contained in:
parent
47c323d36b
commit
a2d372d4bb
@ -2,23 +2,30 @@
|
||||
|
||||
namespace DesignPatterns\More\EAV;
|
||||
|
||||
use SplObjectStorage;
|
||||
|
||||
/**
|
||||
* Class Attribute
|
||||
*/
|
||||
class Attribute implements ValueAccessInterface
|
||||
{
|
||||
/**
|
||||
* @var ValueInterface[]
|
||||
* @var SplObjectStorage
|
||||
*/
|
||||
private $values = array();
|
||||
private $values;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $name;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->values = new SplObjectStorage();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ValueInterface[]
|
||||
* @return SplObjectStorage
|
||||
*/
|
||||
public function getValues()
|
||||
{
|
||||
@ -31,8 +38,9 @@ class Attribute implements ValueAccessInterface
|
||||
*/
|
||||
public function addValue(ValueInterface $value)
|
||||
{
|
||||
// @TODO I think the $value should be checked for uniqueness first to avoid duplication in array.
|
||||
$this->values[] = $value;
|
||||
if (!$this->values->contains($value)) {
|
||||
$this->values->attach($value);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -43,10 +51,8 @@ class Attribute implements ValueAccessInterface
|
||||
*/
|
||||
public function removeValue(ValueInterface $value)
|
||||
{
|
||||
$index = array_search($value, $this->values, true);
|
||||
|
||||
if (false !== $index) {
|
||||
unset($this->values[$index]);
|
||||
if ($this->values->contains($value)) {
|
||||
$this->values->detach($value);
|
||||
}
|
||||
|
||||
return $this;
|
||||
|
@ -2,23 +2,30 @@
|
||||
|
||||
namespace DesignPatterns\More\EAV;
|
||||
|
||||
use SplObjectStorage;
|
||||
|
||||
/**
|
||||
* Class Entity
|
||||
*/
|
||||
class Entity implements ValueAccessInterface
|
||||
{
|
||||
/**
|
||||
* @var ValueInterface[]
|
||||
* @var SplObjectStorage
|
||||
*/
|
||||
private $values = array();
|
||||
private $values;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $name;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->values = new SplObjectStorage();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ValueInterface[]
|
||||
* @return SplObjectStorage
|
||||
*/
|
||||
public function getValues()
|
||||
{
|
||||
@ -31,8 +38,9 @@ class Entity implements ValueAccessInterface
|
||||
*/
|
||||
public function addValue(ValueInterface $value)
|
||||
{
|
||||
// @TODO I think the $value should be checked for uniqueness first to avoid duplication in array.
|
||||
$this->values[] = $value;
|
||||
if (!$this->values->contains($value)) {
|
||||
$this->values->attach($value);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -43,10 +51,8 @@ class Entity implements ValueAccessInterface
|
||||
*/
|
||||
public function removeValue(ValueInterface $value)
|
||||
{
|
||||
$index = array_search($value, $this->values, true);
|
||||
|
||||
if (false !== $index) {
|
||||
unset($this->values[$index]);
|
||||
if ($this->values->contains($value)) {
|
||||
$this->values->detach($value);
|
||||
}
|
||||
|
||||
return $this;
|
||||
|
@ -38,12 +38,11 @@ class AttributeTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
$colorSilver = new Value($attribute);
|
||||
$colorSilver->setName('Silver');
|
||||
$values[] = $colorSilver;
|
||||
$colorGold = new Value($attribute);
|
||||
$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()
|
||||
{
|
||||
$values = array();
|
||||
|
||||
$attribute = new Attribute();
|
||||
$attribute->setName('Color');
|
||||
|
||||
$colorSilver = new Value($attribute);
|
||||
$colorSilver->setName('Silver');
|
||||
$values[] = $colorSilver;
|
||||
$colorGold = new Value($attribute);
|
||||
$colorGold->setName('Gold');
|
||||
$values[] = $colorGold;
|
||||
|
||||
$attribute->removeValue($values[0]);
|
||||
unset($values[0]);
|
||||
$attribute->removeValue($colorSilver);
|
||||
|
||||
$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) {
|
||||
$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) {
|
||||
$macBook->addValue($value);
|
||||
}
|
||||
|
||||
$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
|
||||
{
|
||||
/**
|
||||
* @return ValueInterface[]
|
||||
* @return \SplObjectStorage
|
||||
*/
|
||||
public function getValues();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user