Skip to content

Commit

Permalink
Merge pull request #5 from snowio/cleanup
Browse files Browse the repository at this point in the history
Minor improvements
  • Loading branch information
joshdifabio authored Oct 24, 2017
2 parents 62393d9 + c27bd85 commit 46b84d3
Show file tree
Hide file tree
Showing 37 changed files with 1,009 additions and 315 deletions.
38 changes: 26 additions & 12 deletions src/Attribute.php → src/AttributeData.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
use function SnowIO\FredhopperDataModel\Internal\sanitizeId;
use function SnowIO\FredhopperDataModel\Internal\validateId;

class Attribute extends Entity
final class AttributeData
{
public static function of(string $id, string $type, array $names): self
public static function of(string $id, string $type, InternationalizedString $names): self
{
validateId($id);
self::validateId($id);
AttributeType::validate($type);
foreach ($names as $locale => $name) {
Locale::validate($locale);
if ($names->isEmpty()) {
throw new FredhopperDataException('A name must be provided for at least one locale.');
}
$attribute = new self;
$attribute->id = $id;
Expand All @@ -20,6 +20,11 @@ public static function of(string $id, string $type, array $names): self
return $attribute;
}

public static function validateId(string $id): void
{
validateId($id);
}

public static function sanitizeId(string $id): string
{
return sanitizeId($id);
Expand All @@ -35,27 +40,36 @@ public function getType(): string
return $this->type;
}

public function getNames(): array
public function getNames(): InternationalizedString
{
return $this->names;
}

public function getName(string $locale): ?string
{
return $this->names[$locale] ?? null;
return $this->names->getValue($locale);
}

public function equals($other): bool
{
return $other instanceof AttributeData
&& $this->id === $other->id
&& $this->type === $other->type
&& $this->names->equals($other->names);
}

public function toJson(): array
{
$json = parent::toJson();
$json['attribute_id'] = $this->id;
$json['type'] = $this->type;
$json['names'] = $this->names;
return $json;
return [
'attribute_id' => $this->id,
'type' => $this->type,
'names' => $this->names->toJson(),
];
}

private $id;
private $type;
/** @var InternationalizedString */
private $names;

private function __construct()
Expand Down
54 changes: 35 additions & 19 deletions src/AttributeOption.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
<?php
namespace SnowIO\FredhopperDataModel;

use function SnowIO\FredhopperDataModel\Internal\sanitizeId;
use function SnowIO\FredhopperDataModel\Internal\validateId;

class AttributeOption extends Entity
final class AttributeOption
{
public static function of(string $attributeId, string $valueId): self
{
validateId($attributeId);
validateId($valueId);
self::validateValueId($valueId);
$attributeOption = new self;
$attributeOption->attributeId = $attributeId;
$attributeOption->valueId = $valueId;
$attributeOption->displayValues = InternationalizedString::create();
return $attributeOption;
}

public static function validateValueId(string $valueId): void
{
validateId($valueId);
}

public static function sanitizeValueId(string $valueId): string
{
return sanitizeId($valueId);
}

public function getAttributeId(): string
{
return $this->attributeId;
Expand All @@ -25,47 +37,51 @@ public function getValueId(): string
return $this->valueId;
}

public function withDisplayValues(array $displayValues): self
public function withDisplayValues(InternationalizedString $displayValues): self
{
foreach ($displayValues as $locale => $displayValue) {
Locale::validate($locale);
}
$attributeOption = clone $this;
$attributeOption->displayValues = $displayValues;
return $attributeOption;
}

public function withDisplayValue(string $displayValue, string $locale): self
public function withDisplayValue(LocalizedString $displayValue): self
{
Locale::validate($locale);
$attributeOption = clone $this;
$attributeOption->displayValues[$locale] = $displayValue;
$attributeOption->displayValues = $this->displayValues->with($displayValue);
return $attributeOption;
}

public function getDisplayValues(): array
public function getDisplayValues(): InternationalizedString
{
return $this->displayValues ?? [];
return $this->displayValues;
}

public function getDisplayValue(string $locale): ?string
{
Locale::validate($locale);
return $this->displayValues[$locale] ?? null;
return $this->displayValues->getValue($locale);
}

public function equals($other): bool
{
return $other instanceof AttributeOption
&& $other->valueId === $this->valueId
&& $other->attributeId === $this->attributeId
&& $other->displayValues->equals($this->displayValues);
}

public function toJson(): array
{
$json = parent::toJson();
$json['value_id'] = $this->valueId;
$json['attribute_id'] = $this->attributeId;
$json['display_values'] = $this->displayValues;
return $json;
return [
'value_id' => $this->valueId,
'attribute_id' => $this->attributeId,
'display_values' => $this->displayValues->toJson(),
];
}

private $valueId;
private $attributeId;
private $displayValues = [];
/** @var InternationalizedString */
private $displayValues;

private function __construct()
{
Expand Down
2 changes: 1 addition & 1 deletion src/AttributeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static function isValid(string $type): bool
public static function validate(string $type): void
{
if (!\in_array($type, self::ALL)) {
throw new \Exception('Invalid Type');
throw new FredhopperDataException('Invalid Type');
}
}

Expand Down
9 changes: 1 addition & 8 deletions src/AttributeValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use function SnowIO\FredhopperDataModel\Internal\sanitizeId;
use function SnowIO\FredhopperDataModel\Internal\validateId;

class AttributeValue
final class AttributeValue
{
public static function of(string $attributeId, $value): self
{
Expand Down Expand Up @@ -51,13 +51,6 @@ public function equals($object): bool
&& $object->locale === $this->locale;
}

public function toJson(): array
{
return [
$this->attributeId => $this->value,
];
}

private $attributeId;
private $value;
private $locale;
Expand Down
75 changes: 10 additions & 65 deletions src/AttributeValueSet.php
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);
}
}
34 changes: 21 additions & 13 deletions src/Category.php → src/CategoryData.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php
namespace SnowIO\FredhopperDataModel;

class Category extends Entity
final class CategoryData
{
public static function of(string $id, array $names): self
public static function of(string $id, InternationalizedString $names): self
{
self::validateId($id);
foreach ($names as $locale => $name) {
Locale::validate($locale);
if ($names->isEmpty()) {
throw new FredhopperDataException('A name must be provided for at least one locale.');
}
$category = new self;
$category->id = $id;
Expand All @@ -25,7 +25,7 @@ public static function sanitizeId(string $id): string
public static function validateId(string $id): void
{
if (!\preg_match('{^[a-z][a-z0-9_]+$}', $id)) {
throw new \Exception('Invalid Id');
throw new FredhopperDataException('Invalid Id');
}
}

Expand All @@ -34,15 +34,14 @@ public function getId(): string
return $this->id;
}

public function getNames(): array
public function getNames(): InternationalizedString
{
return $this->names;
}

public function getName(string $locale): ?string
{
Locale::validate($locale);
return $this->names[$locale] ?? null;
return $this->names->getValue($locale);
}

public function getParentId(): ?string
Expand All @@ -58,17 +57,26 @@ public function withParent(string $parentId): self
return $category;
}

public function equals($other): bool
{
return $other instanceof CategoryData
&& $this->id === $other->id
&& $this->parentId === $other->parentId
&& $this->names->equals($other->names);
}

public function toJson(): array
{
$json = parent::toJson();
$json['category_id'] = $this->id;
$json['names'] = $this->names;
$json['parent_id'] = $this->parentId;
return $json;
return [
'category_id' => $this->id,
'parent_id' => $this->parentId,
'names' => $this->names->toJson(),
];
}

private $id;
private $parentId;
/** @var InternationalizedString */
private $names;

private function __construct()
Expand Down
Loading

0 comments on commit 46b84d3

Please sign in to comment.