Skip to content

Commit

Permalink
Merge pull request #8 from sunrise-php/release/v2.0.0
Browse files Browse the repository at this point in the history
v2.0.0
  • Loading branch information
fenric authored Oct 22, 2021
2 parents 5d02b0d + d9aee29 commit 8e9bd14
Show file tree
Hide file tree
Showing 63 changed files with 1,655 additions and 1,752 deletions.
8 changes: 4 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ jobs:
steps:
- checkout
- run: php -v
- run: composer install --no-interaction --prefer-source
- run: php vendor/bin/phpunit --colors=always
- run: composer install --no-interaction --no-suggest --prefer-source
- run: XDEBUG_MODE=coverage php vendor/bin/phpunit --coverage-text
php80:
docker:
- image: circleci/php:8.0-cli-node-browsers
steps:
- checkout
- run: php -v
- run: composer install --no-interaction --prefer-source
- run: php vendor/bin/phpunit --colors=always
- run: composer install --no-interaction --no-suggest --prefer-source
- run: XDEBUG_MODE=coverage php vendor/bin/phpunit --coverage-text
workflows:
version: 2
build:
Expand Down
3 changes: 0 additions & 3 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,5 @@ indent_size = 4
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false

[*.yml]
indent_size = 2
16 changes: 8 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
.php_cs.cache
.phpunit.result.cache
composer.lock
coverage.xml
phpbench.json
phpcs.xml
phpunit.xml
vendor/
/.php_cs.cache
/.phpunit.result.cache
/composer.lock
/coverage.xml
/phpbench.json
/phpcs.xml
/phpunit.xml
/vendor/
13 changes: 4 additions & 9 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
build:
environment:
php:
version: '8.0'
nodes:
analysis:
environment:
php:
version: '8.0'
tests:
override:
- php-scrutinizer-run
coverage:
environment:
php:
version: '8.0'
ini:
'xdebug.mode': 'coverage'
tests:
override:
- command: php vendor/bin/phpunit --coverage-clover coverage.xml
- command: XDEBUG_MODE=coverage php vendor/bin/phpunit --coverage-clover coverage.xml
coverage:
file: coverage.xml
format: clover
138 changes: 66 additions & 72 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,104 +14,98 @@
## Installation

```bash
composer require 'sunrise/hydrator:^1.2'
composer require 'sunrise/hydrator:^2.0'
```

## How to use?

```php
$payload = [
'nullable' => null,
'bool' => true,
'int' => 1,
'float' => 1.1,
'string' => 'foo',
'array' => [],
'dateTime' => '2005-08-15T15:52:01.000+00:00',
'barDto' => [
'value' => 'foo',
],
'barDtoCollection' => [
[
'value' => 'foo',
],
[
'value' => 'bar',
],
],
'enumerableValue' => 'FOO',
];
```
// hydrate an object with array:
$object = (new \Sunrise\Hydrator\Hydrator)->hydrate(Foo::class, $data);

```php
use Sunrise\Hydrator\HydrableObjectInterface;
use ArrayIterator;
use DateTimeImmutable;
// or you can hydrate the object with JSON:
$object = (new \Sunrise\Hydrator\Hydrator)->hydrate(Foo::class, $json);

final class FooDto implements HydrableObjectInterface
{
public string $optional = 'default value';
public ?string $nullable;
public bool $bool;
public int $int;
public float $float;
public string $string;
public ArrayIterator $array;
public DateTimeImmutable $dateTime;
public BarDto $barDto;
public BarDtoCollection $barDtoCollection;
public TestEnumerableObject $enumerableValue;
}
// output the result:
var_dump($object);
```

```php
use Sunrise\Hydrator\HydrableObjectInterface;

final class BarDto implements HydrableObjectInterface
final class Foo
{
public string $value;
}
```
// statical properties will ignored
public static string $statical = '50f4e382-2858-4991-b045-a121004cec80';

```php
use Sunrise\Hydrator\HydrableObjectCollection;
private ?string $nullable = 'ed0110a9-01ac-4f75-a205-223c98d2d2b5';
private string $valuable = '5bf11aa0-08b3-4429-a6d7-4ebf6d70919c';
private string $required;

final class BarDtoCollection extends HydrableObjectCollection
{
public const T = BarDto::class;
private bool $boolean; // also accepts strings (1, on, yes, etc.)
private int $integer; // also accepts string numbers
private float $number; // also accepts string numbers
private string $string;
private array $array;
private object $object;

private \DateTime $dateTime; // accepts timestamps and string date-time
private \DateTimeImmutable $dateTimeImmutable; // accepts timestamps and string date-time

private Bar $bar; // see bellow...
private BarCollection $barCollection; // see bellow...

/**
* @Alias("non-normalized")
*/
#[Alias('non-normalized')]
private string $normalized;

// getters...
}
```

```php
use Sunrise\Hydrator\EnumerableObject;

final class TestEnumerableObject extends EnumerableObject
final class Bar
{
public const FOO = 'foo';
public string $value;
}
```

```php
use Sunrise\Hydrator\Hydrator;

$dto = new FooDto();

(new Hydrator)->hydrate($dto, $payload);
use Sunrise\Hydrator\ObjectCollection;

var_dump($dto);
final class BarCollection extends ObjectCollection
{
// the collection will contain only the specified objects
public const T = Bar::class;
}
```

### Property aliases

```php
final class FooDto implements HydrableObjectInterface
{

/**
* @Alias("snake_case")
*/
public string $camelCase;
}
$data = [
'statical' => '813ea72c-6763-4596-a4d6-b478efed61bb',
'nullable' => null,
'required' => '9f5c273e-1dca-4c2d-ac81-7d6b03b169f4',
'boolean' => true,
'integer' => 42,
'number' => 123.45,
'string' => 'db7614d4-0a81-437b-b2cf-c536ad229c97',
'array' => ['foo' => 'bar'],
'object' => (object) ['foo' => 'bar'],
'dateTime' => '2038-01-19 03:14:08',
'dateTimeImmutable' => '2038-01-19 03:14:08',
'bar' => [
'value' => '9898fb3b-ffb0-406c-bda6-b516423abde7',
],
'barCollection' => [
[
'value' => 'd85c17b6-6e2c-4e2d-9eba-e1dd59b75fe3',
],
[
'value' => '5a8019aa-1c15-4c7c-8beb-1783c3d8996b',
],
],
'non-normalized' => 'f76c4656-431a-4337-9ba9-5440611b37f1',
];
```

---
Expand Down
17 changes: 11 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"fenric",
"sunrise",
"hydrator",
"data-mapper",
"mapper",
"DTO",
"php7",
Expand All @@ -23,24 +24,28 @@
"php": "^7.4|^8.0"
},
"require-dev": {
"phpunit/phpunit": "9.5.3",
"sunrise/coding-standard": "1.0.0",
"doctrine/annotations": "^1.12.1"
"phpunit/phpunit": "~9.5.0",
"sunrise/coding-standard": "~1.0.0",
"doctrine/annotations": "^1.6.0"
},
"autoload": {
"psr-4": {
"Sunrise\\Hydrator\\": "src/Hydrator"
"Sunrise\\Hydrator\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Sunrise\\Hydrator\\Tests\\": "tests/Hydrator"
"Sunrise\\Hydrator\\Tests\\Fixtures\\": "tests/fixtures/"
}
},
"scripts": {
"test": [
"phpcs",
"XDEBUG_MODE=coverage phpunit --colors=always --coverage-text"
"XDEBUG_MODE=coverage phpunit --coverage-text --colors=always"
],
"build": [
"phpdoc -d src/ -t phpdoc/",
"XDEBUG_MODE=coverage phpunit --coverage-html coverage/"
]
}
}
7 changes: 2 additions & 5 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
<?xml version="1.0"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
colors="true">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory>./src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Sunrise Hydrator Test Suite">
<testsuite name="sunrise.hydrator.testSuite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
Expand Down
26 changes: 23 additions & 3 deletions src/Hydrator/Annotation/Alias.php → src/Annotation/Alias.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,39 @@
namespace Sunrise\Hydrator\Annotation;

/**
* Alias
*
* Import classes
*/
use Attribute;

/**
* @Annotation
*
* @Target({"PROPERTY"})
*
* @NamedArgumentConstructor
*
* @Attributes({
* @Attribute("value", type="string", required=true),
* })
*/
#[Attribute(Attribute::TARGET_PROPERTY)]
final class Alias
{

/**
* @Required
* The attribute value
*
* @var string
*/
public $value;

/**
* Constructor of the class
*
* @param string $value
*/
public function __construct(string $value)
{
$this->value = $value;
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 8e9bd14

Please sign in to comment.