Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
lennyrouanet committed Apr 15, 2022
1 parent 0684eee commit 34836d9
Show file tree
Hide file tree
Showing 74 changed files with 2,880 additions and 2 deletions.
11 changes: 11 additions & 0 deletions .gitignore
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/*
78 changes: 76 additions & 2 deletions README.md
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
- Email
- EmailAddress
- EmailAddressAndName
- Url
- UserName
9 changes: 9 additions & 0 deletions component/Abstract/Aggregate.php
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;
}
65 changes: 65 additions & 0 deletions component/Abstract/Collection.php
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;
}
}
9 changes: 9 additions & 0 deletions component/Abstract/Entity.php
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;
}
49 changes: 49 additions & 0 deletions component/Abstract/Enum.php
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,
];
}
}
9 changes: 9 additions & 0 deletions component/Abstract/Interface/DataStructure.php
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();
}
36 changes: 36 additions & 0 deletions component/Abstract/Value.php
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);
}
}
19 changes: 19 additions & 0 deletions component/Abstract/Value/Boolean.php
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();
}
}
19 changes: 19 additions & 0 deletions component/Abstract/Value/Decimal.php
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();
}
}
19 changes: 19 additions & 0 deletions component/Abstract/Value/Integer.php
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();
}
}
27 changes: 27 additions & 0 deletions component/Abstract/Value/Varchar.php
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();
}
}
Loading

0 comments on commit 34836d9

Please sign in to comment.