-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from snowio/cleanup
Minor improvements
- Loading branch information
Showing
37 changed files
with
1,009 additions
and
315 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,27 @@ | ||
<?php | ||
namespace SnowIO\FredhopperDataModel; | ||
|
||
class AttributeValueSet implements \IteratorAggregate | ||
use SnowIO\FredhopperDataModel\Internal\SetTrait; | ||
|
||
final class AttributeValueSet implements \IteratorAggregate | ||
{ | ||
public static function of(array $attributeValues): self | ||
{ | ||
$attributeValuesWithKeys = []; | ||
foreach ($attributeValues as $attributeValue) { | ||
$key = self::getKey($attributeValue); | ||
if (isset($attributeValuesWithKeys[$key])) { | ||
throw new \Error; | ||
} | ||
$attributeValuesWithKeys[$key] = $attributeValue; | ||
} | ||
return new self($attributeValuesWithKeys); | ||
} | ||
use SetTrait; | ||
|
||
public function withAttributeValue(AttributeValue $attributeValue): self | ||
public function with(AttributeValue $attributeValue): self | ||
{ | ||
$result = clone $this; | ||
$key = self::getKey($attributeValue); | ||
$result->attributeValues[$key] = $attributeValue; | ||
$result->items[$key] = $attributeValue; | ||
return $result; | ||
} | ||
|
||
public function add(self $otherSet): self | ||
{ | ||
if ($otherSet->overlaps($this)) { | ||
throw new \Error; | ||
} | ||
$mergedValues = \array_merge($this->attributeValues, $otherSet->attributeValues); | ||
return new self($mergedValues); | ||
} | ||
|
||
public function overlaps(AttributeValueSet $otherSet): bool | ||
{ | ||
return !empty(\array_intersect_key($this->attributeValues, $otherSet->attributeValues)) | ||
|| !empty(\array_intersect_key($otherSet->attributeValues, $this->attributeValues)); | ||
} | ||
|
||
public function equals($object): bool | ||
{ | ||
if (!$object instanceof self || \count($object->attributeValues) != \count($this->attributeValues)) { | ||
return false; | ||
} | ||
|
||
foreach ($this->attributeValues as $attributeValueId => $attributeValue) { | ||
$otherAttributeValue = $object->attributeValues[$attributeValueId] ?? null; | ||
if (!$attributeValue->equals($otherAttributeValue)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return \array_values($this->attributeValues); | ||
} | ||
|
||
public function getIterator(): \Iterator | ||
{ | ||
foreach ($this->attributeValues as $attributeValue) { | ||
yield $attributeValue; | ||
} | ||
} | ||
|
||
/** @var AttributeValue[] */ | ||
private $attributeValues; | ||
|
||
private function __construct(array $attributeValues) | ||
private static function getKey(AttributeValue $attributeValue): string | ||
{ | ||
$this->attributeValues = $attributeValues; | ||
return "{$attributeValue->getAttributeId()}-{$attributeValue->getLocale()}"; | ||
} | ||
|
||
private static function getKey(AttributeValue $attributeValue): string | ||
private static function itemsAreEqual(AttributeValue $item1, AttributeValue $item2): bool | ||
{ | ||
return "{$attributeValue->getAttributeId()}-{$attributeValue->getLocale()}"; | ||
return $item1->equals($item2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.