Skip to content

Commit

Permalink
Merge pull request #1 from PhantPHP/unserialiser
Browse files Browse the repository at this point in the history
Version 2
  • Loading branch information
lennyrouanet authored May 5, 2022
2 parents 08b7c58 + 6666546 commit 4a04583
Show file tree
Hide file tree
Showing 56 changed files with 726 additions and 113 deletions.
1 change: 1 addition & 0 deletions component/Abstract/Aggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
abstract class Aggregate implements \Phant\DataStructure\Abstract\Interface\DataStructure
{
abstract public function serialize(): array;
abstract public static function unserialize(array $array): self;
}
3 changes: 3 additions & 0 deletions component/Abstract/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ protected function removeItem(mixed $item): self
{
if (($key = array_search($item, $this->items)) !== false) {
unset($this->items[ $key ]);
$this->items = array_values($this->items);
}

return $this;
Expand Down Expand Up @@ -62,4 +63,6 @@ public function serialize(): ?array

return $items;
}

abstract public static function unserialize(array $array): self;
}
1 change: 1 addition & 0 deletions component/Abstract/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
abstract class Entity implements \Phant\DataStructure\Abstract\Interface\DataStructure
{
abstract public function serialize(): array;
abstract public static function unserialize(array $array): self;
}
9 changes: 9 additions & 0 deletions component/Abstract/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,13 @@ public function serialize(): array
static::LABEL_KEY => (string) $this->label,
];
}

public static function unserialize(array $array): self
{
if (!isset($array[ static::VALUE_KEY ])) {
throw new NotCompliant();
}

return new static($array[ static::VALUE_KEY ]);
}
}
3 changes: 2 additions & 1 deletion component/Abstract/Interface/DataStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@

interface DataStructure
{
public function serialize();
public function serialize(): mixed;
public static function unserialize(array $array): self;
}
11 changes: 8 additions & 3 deletions component/Abstract/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,26 @@ public function __construct($value)
}
}

public function __toString()
public function __toString(): string
{
return (string) $this->get();
}

public function get()
public function get(): mixed
{
return property_exists($this, 'value') ? $this->value : null;
}

public function serialize()
public function serialize(): mixed
{
return $this->get();
}

public static function unserialize(mixed $value): self
{
return new static($value);
}

protected static function addNonBreakingSpace(string $value): string
{
return str_replace(' ', "\xC2\xA0", $value);
Expand Down
12 changes: 12 additions & 0 deletions component/Geography/GpsCoordinates.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ public function serialize(): array
];
}

public static function unserialize(array $array): self
{
if (!isset(
$array[ 'latitude' ],
$array[ 'longitude' ]
)) {
throw new NotCompliant();
}

return new self($array[ 'latitude' ], $array[ 'longitude' ]);
}

public static function createFromLambert93(float $x, float $y): static
{
$x = number_format($x, 10, '.', '');
Expand Down
24 changes: 23 additions & 1 deletion component/Money/Price.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace Phant\DataStructure\Money;

use Phant\Error\NotCompliant;

class Price extends \Phant\DataStructure\Abstract\Aggregate
{
protected float $price;
Expand All @@ -11,10 +13,14 @@ class Price extends \Phant\DataStructure\Abstract\Aggregate

public function __construct(
float $price,
?Currency $currency = null,
null|string|Currency $currency = null,
?string $unit = null
)
{
if (is_string($currency)) {
$currency = new Currency($currency);
}

$this->price = $price;
$this->currency = $currency;
$this->unit = $unit;
Expand Down Expand Up @@ -72,4 +78,20 @@ public function serialize(): array

return $array;
}

public static function unserialize(array $array): self
{
if (!isset(
$array[ 'price' ],
$array[ 'currency' ]
)) {
throw new NotCompliant();
}

return new self(
$array[ 'price' ],
!is_null($array[ 'currency' ]) ? Currency::unserialize($array[ 'currency' ]) : null,
$array[ 'unit' ] ?? null
);
}
}
12 changes: 12 additions & 0 deletions component/Number/Note.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,16 @@ public function serialize(): array
'unit' => $this->unit,
];
}

public static function unserialize(array $array): self
{
if (!isset(
$array[ 'note' ],
$array[ 'unit' ]
)) {
throw new NotCompliant();
}

return new self($array[ 'note' ], $array[ 'unit' ]);
}
}
45 changes: 41 additions & 4 deletions component/Person/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
DateTime,
};

use Phant\Error\NotCompliant;

class Person extends \Phant\DataStructure\Abstract\Entity
{
public ?Lastname $lastname;
Expand All @@ -22,12 +24,28 @@ class Person extends \Phant\DataStructure\Abstract\Entity
public ?Date $birthday;

public function __construct(
?Lastname $lastname,
?Firstname $firstname,
?Gender $gender = null,
?Date $birthday = null
null|string|Lastname $lastname,
null|string|Firstname $firstname,
null|string|Gender $gender = null,
null|string|Date $birthday = null
)
{
if (is_string($lastname)) {
$lastname = new Lastname($lastname);
}

if (is_string($firstname)) {
$firstname = new Firstname($firstname);
}

if (is_string($gender)) {
$gender = new Gender($gender);
}

if (is_string($birthday)) {
$birthday = new Date($birthday);
}

$this->lastname = $lastname;
$this->firstname = $firstname;
$this->gender = $gender;
Expand All @@ -43,4 +61,23 @@ public function serialize(): array
'birthday' => $this->birthday ? $this->birthday->serialize() : null,
];
}

public static function unserialize(array $array): self
{
if (!isset(
$array[ 'lastname' ],
$array[ 'firstname' ],
$array[ 'gender' ],
$array[ 'birthday' ]
)) {
throw new NotCompliant();
}

return new self(
!is_null($array[ 'lastname' ]) ? Lastname::unserialize($array[ 'lastname' ]) : null,
!is_null($array[ 'firstname' ]) ? Firstname::unserialize($array[ 'firstname' ]) : null,
!is_null($array[ 'gender' ]) ? Gender::unserialize($array[ 'gender' ]) : null,
!is_null($array[ 'birthday' ]) ? Date::unserialize($array[ 'birthday' ]) : null
);
}
}
25 changes: 24 additions & 1 deletion component/Time/DateInterval.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,23 @@ class DateInterval extends \Phant\DataStructure\Abstract\Aggregate
protected ?Date $to;
protected ?Duration $duration;

public function __construct(?Date $from, ?Date $to)
public function __construct(
null|string|Date $from,
null|string|Date $to
)
{
if (!$from && !$to) {
throw new NotCompliant('Date intervals: from ' . $from . ' to' . $to);
}

if (is_string($from)) {
$from = new Date($from);
}

if (is_string($to)) {
$to = new Date($to);
}

$this->from = $from;
$this->to = $to;
$this->duration = ($this->from && $this->to) ? new Duration($this->to->getTime() - $this->from->getTime()) : null;
Expand Down Expand Up @@ -54,4 +65,16 @@ public function serialize(): array
static::DURATION_KEY => $this->duration ? $this->duration->serialize() : null,
];
}

public static function unserialize(array $array): self
{
if (!isset(
$array[static::FROM_KEY],
$array[static::TO_KEY]
)) {
throw new NotCompliant();
}

return new self($array[ static::FROM_KEY ], $array[ static::TO_KEY ]);
}
}
25 changes: 24 additions & 1 deletion component/Time/DateTimeInterval.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,23 @@ class DateTimeInterval extends \Phant\DataStructure\Abstract\Aggregate
protected ?DateTime $to;
protected ?Duration $duration;

public function __construct(?DateTime $from, ?DateTime $to)
public function __construct(
null|string|DateTime $from,
null|string|DateTime $to
)
{
if (!$from && !$to) {
throw new NotCompliant('Date time intervals: from ' . $from . ' to' . $to);
}

if (is_string($from)) {
$from = new DateTime($from);
}

if (is_string($to)) {
$to = new DateTime($to);
}

$this->from = $from;
$this->to = $to;
$this->duration = ($this->from && $this->to) ? new Duration($this->to->getTime() - $this->from->getTime()) : null;
Expand Down Expand Up @@ -54,4 +65,16 @@ public function serialize(): array
static::DURATION_KEY => $this->duration ? $this->duration->serialize() : null,
];
}

public static function unserialize(array $array): self
{
if (!isset(
$array[ static::FROM_KEY ],
$array[ static::TO_KEY ]
)) {
throw new NotCompliant();
}

return new self($array[ static::FROM_KEY ], $array[ static::TO_KEY ]);
}
}
27 changes: 22 additions & 5 deletions component/Time/Duration.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

namespace Phant\DataStructure\Time;

class Duration extends \Phant\DataStructure\Abstract\Value\Integer
use Phant\Error\NotCompliant;

class Duration extends \Phant\DataStructure\Abstract\Aggregate
{
// Duration in secondes
const MINUTE = 60;
Expand All @@ -25,11 +27,12 @@ class Duration extends \Phant\DataStructure\Abstract\Value\Integer
const YEAR_LABEL = 'year';
const YEAR_LABEL_PLURAL = 'years';

protected int $time;
protected string $label;

public function __construct(int $duration)
public function __construct(int $time)
{
parent::__construct($duration);
$this->time = $time;
$this->label = $this->buildLabel();
}

Expand All @@ -38,14 +41,19 @@ public function __toString()
return (string) $this->getLabel();
}

public function get(): int
{
return $this->time;
}

public function getLabel(): string
{
return $this->label;
}

protected function buildLabel(): string
{
$remainingTime = $this->value;
$remainingTime = $this->time;

$labels = [];

Expand Down Expand Up @@ -102,8 +110,17 @@ protected function buildLabel(): string
public function serialize(): array
{
return [
'value' => $this->value,
'value' => $this->time,
'label' => $this->label
];
}

public static function unserialize(array $array): self
{
if (!isset($array[ 'value' ])) {
throw new NotCompliant();
}

return new self($array[ 'value' ]);
}
}
Loading

0 comments on commit 4a04583

Please sign in to comment.