FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Refactor with SplObjectStorage · JavaDevl/DesignPatternsPHP@a2d372d · GitHub

Commit a2d372d

Browse files
committed
Refactor with SplObjectStorage
1 parent 47c323d commit a2d372d

5 files changed

Lines changed: 41 additions & 33 deletions

File tree

‎More/EAV/Attribute.php‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,30 @@
22

33
namespace DesignPatterns\More\EAV;
44

5+
use SplObjectStorage;
6+
57
/**
68
* Class Attribute
79
*/
810
class Attribute implements ValueAccessInterface
911
{
1012
/**
11-
* @var ValueInterface[]
13+
* @var SplObjectStorage
1214
*/
13-
private $values = array();
15+
private $values;
1416

1517
/**
1618
* @var string
1719
*/
1820
private $name;
1921

22+
public function __construct()
23+
{
24+
$this->values = new SplObjectStorage();
25+
}
26+
2027
/**
21-
* @return ValueInterface[]
28+
* @return SplObjectStorage
2229
*/
2330
public function getValues()
2431
{
@@ -31,8 +38,9 @@ public function getValues()
3138
*/
3239
public function addValue(ValueInterface $value)
3340
{
34-
// @TODO I think the $value should be checked for uniqueness first to avoid duplication in array.
35-
$this->values[] = $value;
41+
if (!$this->values->contains($value)) {
42+
$this->values->attach($value);
43+
}
3644

3745
return $this;
3846
}
@@ -43,10 +51,8 @@ public function addValue(ValueInterface $value)
4351
*/
4452
public function removeValue(ValueInterface $value)
4553
{
46-
$index = array_search($value, $this->values, true);
47-
48-
if (false !== $index) {
49-
unset($this->values[$index]);
54+
if ($this->values->contains($value)) {
55+
$this->values->detach($value);
5056
}
5157

5258
return $this;

‎More/EAV/Entity.php‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,30 @@
22

33
namespace DesignPatterns\More\EAV;
44

5+
use SplObjectStorage;
6+
57
/**
68
* Class Entity
79
*/
810
class Entity implements ValueAccessInterface
911
{
1012
/**
11-
* @var ValueInterface[]
13+
* @var SplObjectStorage
1214
*/
13-
private $values = array();
15+
private $values;
1416

1517
/**
1618
* @var string
1719
*/
1820
private $name;
1921

22+
public function __construct()
23+
{
24+
$this->values = new SplObjectStorage();
25+
}
26+
2027
/**
21-
* @return ValueInterface[]
28+
* @return SplObjectStorage
2229
*/
2330
public function getValues()
2431
{
@@ -31,8 +38,9 @@ public function getValues()
3138
*/
3239
public function addValue(ValueInterface $value)
3340
{
34-
// @TODO I think the $value should be checked for uniqueness first to avoid duplication in array.
35-
$this->values[] = $value;
41+
if (!$this->values->contains($value)) {
42+
$this->values->attach($value);
43+
}
3644

3745
return $this;
3846
}
@@ -43,10 +51,8 @@ public function addValue(ValueInterface $value)
4351
*/
4452
public function removeValue(ValueInterface $value)
4553
{
46-
$index = array_search($value, $this->values, true);
47-
48-
if (false !== $index) {
49-
unset($this->values[$index]);
54+
if ($this->values->contains($value)) {
55+
$this->values->detach($value);
5056
}
5157

5258
return $this;

‎More/EAV/Tests/AttributeTest.php‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,34 +38,29 @@ public function testAddValue()
3838

3939
$colorSilver = new Value($attribute);
4040
$colorSilver->setName('Silver');
41-
$values[] = $colorSilver;
4241
$colorGold = new Value($attribute);
4342
$colorGold->setName('Gold');
44-
$values[] = $colorGold;
4543

46-
$this->assertEquals($values, $attribute->getValues());
44+
$this->assertTrue($attribute->getValues()->contains($colorSilver));
45+
$this->assertTrue($attribute->getValues()->contains($colorGold));
4746
}
4847

4948
/**
5049
* @depends testAddValue
5150
*/
5251
public function testRemoveValue()
5352
{
54-
$values = array();
55-
5653
$attribute = new Attribute();
5754
$attribute->setName('Color');
5855

5956
$colorSilver = new Value($attribute);
6057
$colorSilver->setName('Silver');
61-
$values[] = $colorSilver;
6258
$colorGold = new Value($attribute);
6359
$colorGold->setName('Gold');
64-
$values[] = $colorGold;
6560

66-
$attribute->removeValue($values[0]);
67-
unset($values[0]);
61+
$attribute->removeValue($colorSilver);
6862

69-
$this->assertEquals($values, $attribute->getValues());
63+
$this->assertFalse($attribute->getValues()->contains($colorSilver));
64+
$this->assertTrue($attribute->getValues()->contains($colorGold));
7065
}
7166
}

‎More/EAV/Tests/EntityTest.php‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ public function testAddValue($name, array $values)
3737

3838
foreach ($values as $value) {
3939
$macBook->addValue($value);
40+
$this->assertTrue($macBook->getValues()->contains($value));
4041
}
4142

42-
$this->assertEquals($values, $macBook->getValues());
43+
$this->assertCount(count($values), $macBook->getValues());
4344
}
4445

4546
/**
@@ -57,11 +58,11 @@ public function testRemoveValue($name, array $values)
5758
foreach ($values as $value) {
5859
$macBook->addValue($value);
5960
}
60-
6161
$macBook->removeValue($values[0]);
62-
unset($values[0]);
6362

64-
$this->assertEquals($values, $macBook->getValues());
63+
$this->assertFalse($macBook->getValues()->contains($values[0]));
64+
unset($values[0]);
65+
$this->assertCount(count($values), $macBook->getValues());
6566
}
6667

6768
/**

‎More/EAV/ValueAccessInterface.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
interface ValueAccessInterface
99
{
1010
/**
11-
* @return ValueInterface[]
11+
* @return \SplObjectStorage
1212
*/
1313
public function getValues();
1414

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL