| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 47c323d commit a2d372d
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,23 +2,30 @@ | |||
| 2 | 2 | ||
| 3 | 3 | namespace DesignPatterns\More\EAV; | |
| 4 | 4 | ||
| 5 | + use SplObjectStorage; | ||
| 6 | + | ||
| 5 | 7 | /** | |
| 6 | 8 | * Class Attribute | |
| 7 | 9 | */ | |
| 8 | 10 | class Attribute implements ValueAccessInterface | |
| 9 | 11 | { | |
| 10 | 12 | /** | |
| 11 | - * @var ValueInterface[] | ||
| 13 | + * @var SplObjectStorage | ||
| 12 | 14 | */ | |
| 13 | - private $values = array(); | ||
| 15 | + private $values; | ||
| 14 | 16 | ||
| 15 | 17 | /** | |
| 16 | 18 | * @var string | |
| 17 | 19 | */ | |
| 18 | 20 | private $name; | |
| 19 | 21 | ||
| 22 | + public function __construct() | ||
| 23 | + { | ||
| 24 | + $this->values = new SplObjectStorage(); | ||
| 25 | + } | ||
| 26 | + | ||
| 20 | 27 | /** | |
| 21 | - * @return ValueInterface[] | ||
| 28 | + * @return SplObjectStorage | ||
| 22 | 29 | */ | |
| 23 | 30 | public function getValues() | |
| 24 | 31 | { | |
@@ -31,8 +38,9 @@ public function getValues() | |||
| 31 | 38 | */ | |
| 32 | 39 | public function addValue(ValueInterface $value) | |
| 33 | 40 | { | |
| 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 | + } | ||
| 36 | 44 | ||
| 37 | 45 | return $this; | |
| 38 | 46 | } | |
@@ -43,10 +51,8 @@ public function addValue(ValueInterface $value) | |||
| 43 | 51 | */ | |
| 44 | 52 | public function removeValue(ValueInterface $value) | |
| 45 | 53 | { | |
| 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); | ||
| 50 | 56 | } | |
| 51 | 57 | ||
| 52 | 58 | return $this; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,23 +2,30 @@ | |||
| 2 | 2 | ||
| 3 | 3 | namespace DesignPatterns\More\EAV; | |
| 4 | 4 | ||
| 5 | + use SplObjectStorage; | ||
| 6 | + | ||
| 5 | 7 | /** | |
| 6 | 8 | * Class Entity | |
| 7 | 9 | */ | |
| 8 | 10 | class Entity implements ValueAccessInterface | |
| 9 | 11 | { | |
| 10 | 12 | /** | |
| 11 | - * @var ValueInterface[] | ||
| 13 | + * @var SplObjectStorage | ||
| 12 | 14 | */ | |
| 13 | - private $values = array(); | ||
| 15 | + private $values; | ||
| 14 | 16 | ||
| 15 | 17 | /** | |
| 16 | 18 | * @var string | |
| 17 | 19 | */ | |
| 18 | 20 | private $name; | |
| 19 | 21 | ||
| 22 | + public function __construct() | ||
| 23 | + { | ||
| 24 | + $this->values = new SplObjectStorage(); | ||
| 25 | + } | ||
| 26 | + | ||
| 20 | 27 | /** | |
| 21 | - * @return ValueInterface[] | ||
| 28 | + * @return SplObjectStorage | ||
| 22 | 29 | */ | |
| 23 | 30 | public function getValues() | |
| 24 | 31 | { | |
@@ -31,8 +38,9 @@ public function getValues() | |||
| 31 | 38 | */ | |
| 32 | 39 | public function addValue(ValueInterface $value) | |
| 33 | 40 | { | |
| 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 | + } | ||
| 36 | 44 | ||
| 37 | 45 | return $this; | |
| 38 | 46 | } | |
@@ -43,10 +51,8 @@ public function addValue(ValueInterface $value) | |||
| 43 | 51 | */ | |
| 44 | 52 | public function removeValue(ValueInterface $value) | |
| 45 | 53 | { | |
| 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); | ||
| 50 | 56 | } | |
| 51 | 57 | ||
| 52 | 58 | return $this; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -38,34 +38,29 @@ public function testAddValue() | |||
| 38 | 38 | ||
| 39 | 39 | $colorSilver = new Value($attribute); | |
| 40 | 40 | $colorSilver->setName('Silver'); | |
| 41 | - $values[] = $colorSilver; | ||
| 42 | 41 | $colorGold = new Value($attribute); | |
| 43 | 42 | $colorGold->setName('Gold'); | |
| 44 | - $values[] = $colorGold; | ||
| 45 | 43 | ||
| 46 | - $this->assertEquals($values, $attribute->getValues()); | ||
| 44 | + $this->assertTrue($attribute->getValues()->contains($colorSilver)); | ||
| 45 | + $this->assertTrue($attribute->getValues()->contains($colorGold)); | ||
| 47 | 46 | } | |
| 48 | 47 | ||
| 49 | 48 | /** | |
| 50 | 49 | * @depends testAddValue | |
| 51 | 50 | */ | |
| 52 | 51 | public function testRemoveValue() | |
| 53 | 52 | { | |
| 54 | - $values = array(); | ||
| 55 | - | ||
| 56 | 53 | $attribute = new Attribute(); | |
| 57 | 54 | $attribute->setName('Color'); | |
| 58 | 55 | ||
| 59 | 56 | $colorSilver = new Value($attribute); | |
| 60 | 57 | $colorSilver->setName('Silver'); | |
| 61 | - $values[] = $colorSilver; | ||
| 62 | 58 | $colorGold = new Value($attribute); | |
| 63 | 59 | $colorGold->setName('Gold'); | |
| 64 | - $values[] = $colorGold; | ||
| 65 | 60 | ||
| 66 | - $attribute->removeValue($values[0]); | ||
| 67 | - unset($values[0]); | ||
| 61 | + $attribute->removeValue($colorSilver); | ||
| 68 | 62 | ||
| 69 | - $this->assertEquals($values, $attribute->getValues()); | ||
| 63 | + $this->assertFalse($attribute->getValues()->contains($colorSilver)); | ||
| 64 | + $this->assertTrue($attribute->getValues()->contains($colorGold)); | ||
| 70 | 65 | } | |
| 71 | 66 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -37,9 +37,10 @@ public function testAddValue($name, array $values) | |||
| 37 | 37 | ||
| 38 | 38 | foreach ($values as $value) { | |
| 39 | 39 | $macBook->addValue($value); | |
| 40 | + $this->assertTrue($macBook->getValues()->contains($value)); | ||
| 40 | 41 | } | |
| 41 | 42 | ||
| 42 | - $this->assertEquals($values, $macBook->getValues()); | ||
| 43 | + $this->assertCount(count($values), $macBook->getValues()); | ||
| 43 | 44 | } | |
| 44 | 45 | ||
| 45 | 46 | /** | |
@@ -57,11 +58,11 @@ public function testRemoveValue($name, array $values) | |||
| 57 | 58 | foreach ($values as $value) { | |
| 58 | 59 | $macBook->addValue($value); | |
| 59 | 60 | } | |
| 60 | - | ||
| 61 | 61 | $macBook->removeValue($values[0]); | |
| 62 | - unset($values[0]); | ||
| 63 | 62 | ||
| 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()); | ||
| 65 | 66 | } | |
| 66 | 67 | ||
| 67 | 68 | /** | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,7 +8,7 @@ | |||
| 8 | 8 | interface ValueAccessInterface | |
| 9 | 9 | { | |
| 10 | 10 | /** | |
| 11 | - * @return ValueInterface[] | ||
| 11 | + * @return \SplObjectStorage | ||
| 12 | 12 | */ | |
| 13 | 13 | public function getValues(); | |
| 14 | 14 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments