-
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.
- Loading branch information
1 parent
0684eee
commit 34836d9
Showing
74 changed files
with
2,880 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Dependencies | ||
composer.lock | ||
vendor/* | ||
|
||
# Test | ||
.phpunit* | ||
test/coverage | ||
|
||
# Dev | ||
.DS_Store | ||
.nova/* |
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,2 +1,76 @@ | ||
# data-structure | ||
Data structure | ||
# Data structure | ||
|
||
## Requirments | ||
|
||
PHP >= 8.0 | ||
|
||
|
||
## Install | ||
|
||
`composer require phant/data-structure` | ||
|
||
|
||
## Abstract | ||
|
||
- Aggregate | ||
- Collection | ||
- Entity | ||
- Enum | ||
- Value | ||
- Boolean | ||
- Decimal | ||
- Integer | ||
- Varchar | ||
|
||
|
||
## Geography | ||
|
||
- GpsCoordinates | ||
|
||
|
||
## Id | ||
|
||
- Uuid (based on [ramsey/uuid](https://github.com/ramsey/uuid)) | ||
|
||
|
||
## Money | ||
|
||
- Currency | ||
- Price | ||
|
||
|
||
## Number | ||
|
||
- Rate | ||
|
||
|
||
## Person | ||
|
||
- Firstname | ||
- Gender | ||
- Lastname | ||
- Person | ||
|
||
|
||
## Time | ||
|
||
- Date | ||
- DateIntervale | ||
- DateTime | ||
- DateTimeIntervale | ||
- Duration | ||
|
||
|
||
## Token | ||
|
||
- Jwt (based on [Firebase PHP-JWT](https://github.com/firebase/php-jwt)) | ||
|
||
|
||
## Web | ||
|
||
- DomainName | ||
- EmailAddress | ||
- EmailAddressAndName | ||
- Url | ||
- UserName |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Phant\DataStructure\Abstract; | ||
|
||
abstract class Aggregate implements \Phant\DataStructure\Abstract\Interface\DataStructure | ||
{ | ||
abstract public function serialize(): array; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Phant\DataStructure\Abstract; | ||
|
||
abstract class Collection implements \Phant\DataStructure\Abstract\Interface\DataStructure | ||
{ | ||
protected array $items; | ||
|
||
public function __construct(array $items = []) | ||
{ | ||
$this->items = $items; | ||
} | ||
|
||
protected function addItem(mixed $item) | ||
{ | ||
if (array_search($item, $this->items) == false) { | ||
$this->items[] = $item; | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
protected function removeItem(mixed $item) | ||
{ | ||
if (($key = array_search($item, $this->items)) !== false) { | ||
unset($this->items[ $key ]); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
public function itemsIterator() | ||
{ | ||
foreach ($this->items as $item) { | ||
yield $item; | ||
} | ||
} | ||
|
||
public function isEmpty(): bool | ||
{ | ||
return empty($this->items); | ||
} | ||
|
||
public function getNbItems(): int | ||
{ | ||
return count($this->items); | ||
} | ||
|
||
public function getByKey(int $key): mixed | ||
{ | ||
return $this->items[ $key ] ?? null; | ||
} | ||
|
||
public function serialize(): ?array | ||
{ | ||
$items = []; | ||
|
||
foreach ($this->items as $item) { | ||
$items[] = method_exists($item, 'serialize') ? $item->serialize() : $item; | ||
} | ||
|
||
return $items; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Phant\DataStructure\Abstract; | ||
|
||
abstract class Entity implements \Phant\DataStructure\Abstract\Interface\DataStructure | ||
{ | ||
abstract public function serialize(): array; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Phant\DataStructure\Abstract; | ||
|
||
use Phant\Error\NotCompliant; | ||
|
||
abstract class Enum implements \Phant\DataStructure\Abstract\Interface\DataStructure | ||
{ | ||
public const VALUE_KEY = 'value'; | ||
public const LABEL_KEY = 'label'; | ||
|
||
public const VALUES = []; | ||
|
||
protected mixed $value; | ||
protected string $label; | ||
|
||
public function __construct(mixed $value) | ||
{ | ||
if (!isset(static::VALUES[$value])) { | ||
throw new NotCompliant(); | ||
} | ||
$this->value = $value; | ||
$this->label = static::VALUES[$value]; | ||
} | ||
|
||
public function __toString() | ||
{ | ||
return (string) $this->label; | ||
} | ||
|
||
public function getValue(): mixed | ||
{ | ||
return $this->value; | ||
} | ||
|
||
public function getLabel(): mixed | ||
{ | ||
return $this->label; | ||
} | ||
|
||
public function serialize(): array | ||
{ | ||
return [ | ||
static::VALUE_KEY => $this->value, | ||
static::LABEL_KEY => (string) $this->label, | ||
]; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Phant\DataStructure\Abstract\Interface; | ||
|
||
interface DataStructure | ||
{ | ||
public function serialize(); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Phant\DataStructure\Abstract; | ||
|
||
abstract class Value implements \Phant\DataStructure\Abstract\Interface\DataStructure | ||
{ | ||
//protected $value; | ||
|
||
public function __construct($value) | ||
{ | ||
if (property_exists($this, 'value')) { | ||
$this->value = $value; | ||
} | ||
} | ||
|
||
public function __toString() | ||
{ | ||
return (string) $this->get(); | ||
} | ||
|
||
public function get() | ||
{ | ||
return property_exists($this, 'value') ? $this->value : null; | ||
} | ||
|
||
public function serialize() | ||
{ | ||
return $this->get(); | ||
} | ||
|
||
protected static function addNonBreakingSpace(string $value): string | ||
{ | ||
return str_replace(' ', "\xC2\xA0", $value); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Phant\DataStructure\Abstract\Value; | ||
|
||
abstract class Boolean extends \Phant\DataStructure\Abstract\Value | ||
{ | ||
protected bool $value; | ||
|
||
public function __construct(bool $value) | ||
{ | ||
parent::__construct($value); | ||
} | ||
|
||
public function get(): bool | ||
{ | ||
return parent::get(); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Phant\DataStructure\Abstract\Value; | ||
|
||
abstract class Decimal extends \Phant\DataStructure\Abstract\Value | ||
{ | ||
protected float $value; | ||
|
||
public function __construct(float $value) | ||
{ | ||
parent::__construct($value); | ||
} | ||
|
||
public function get(): float | ||
{ | ||
return parent::get(); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Phant\DataStructure\Abstract\Value; | ||
|
||
abstract class Integer extends \Phant\DataStructure\Abstract\Value | ||
{ | ||
protected int $value; | ||
|
||
public function __construct(int $value) | ||
{ | ||
parent::__construct($value); | ||
} | ||
|
||
public function get(): int | ||
{ | ||
return parent::get(); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Phant\DataStructure\Abstract\Value; | ||
|
||
use Phant\Error\NotCompliant; | ||
|
||
abstract class Varchar extends \Phant\DataStructure\Abstract\Value | ||
{ | ||
const PATTERN = null; | ||
|
||
protected string $value; | ||
|
||
public function __construct(string $value) | ||
{ | ||
if (defined(get_class($this) . '::PATTERN') && static::PATTERN && !preg_match(static::PATTERN, $value)) { | ||
throw new NotCompliant('Value : ' . $value); | ||
} | ||
|
||
parent::__construct($value); | ||
} | ||
|
||
public function get(): string | ||
{ | ||
return parent::get(); | ||
} | ||
} |
Oops, something went wrong.