Skip to content

Commit

Permalink
Add CustomAddress and RegionData to Customer
Browse files Browse the repository at this point in the history
  • Loading branch information
nei committed Jun 11, 2018
1 parent 80ada97 commit 385dc7d
Show file tree
Hide file tree
Showing 10 changed files with 1,022 additions and 41 deletions.
40 changes: 40 additions & 0 deletions src/AddressSet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace SnowIO\Magento2DataModel;

final class AddressSet implements \IteratorAggregate, ValueObject
{
use SetTrait;

public function withAddress(CustomerAddress $address): self
{
$result = clone $this;
$key = self::getKey($address);
$result->items[$key] = $address;
return $result;
}

public function get(string $key): ?CustomerAddress
{
return $this->items[$key] ?? null;
}

private static function getKey(CustomerAddress $address): ?int
{
return $address->getId();
}

public function toJson(): array
{
return array_map(function (CustomerAddress $address) {
return $address->toJson();
}, array_values($this->items));
}

private static function itemsAreEqual(
CustomerAddress $address,
CustomerAddress $otherAddress
) : bool {
return $address->equals($otherAddress);
}
}
9 changes: 9 additions & 0 deletions src/CustomAttributeSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ public function toJson(): array
}, array_values($this->items));
}

public static function fromJson(array $json): self
{
$items = [];
foreach ($json as $code => $value) {
$items[] = CustomAttribute::of($value['attribute_code'], $value['value']);
}
return self::of($items);
}

private static function itemsAreEqual(CustomAttribute $customAttribute, CustomAttribute $otherCustomAttribute): bool
{
return $customAttribute->equals($otherCustomAttribute);
Expand Down

0 comments on commit 385dc7d

Please sign in to comment.