Skip to content

Commit

Permalink
Update library for PHP 8.2
Browse files Browse the repository at this point in the history
  • Loading branch information
OzzyCzech committed Aug 1, 2024
1 parent 8b7587f commit 75f4ea8
Show file tree
Hide file tree
Showing 11 changed files with 219 additions and 162 deletions.
50 changes: 25 additions & 25 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
{
"name": "om/from-array",
"description": "fromArray trait allow create objects instances loaded with initial data array",
"type": "library",
"license": "BSD-3-Clause",
"authors": [
{
"name": "Roman Ožana",
"homepage": "https://ozana.cz/",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"DataLoader\\": "src/"
}
},
"scripts": {
"test": "tester tests"
},
"require": {
"php": ">=5.6.0"
},
"require-dev": {
"nette/tester": "*"
}
"name": "om/from-array",
"description": "fromArray trait allow create objects instances loaded with initial data array",
"type": "library",
"license": "BSD-3-Clause",
"authors": [
{
"name": "Roman Ožana",
"homepage": "https://ozana.cz/",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"DataLoader\\": "src/"
}
},
"scripts": {
"test": "tester tests"
},
"require": {
"php": ">=8.2"
},
"require-dev": {
"nette/tester": "*"
}
}
8 changes: 6 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# FromArray data loader

`fromArray` trait allow create objects instances loaded with initial data array:
`fromArray` trait allow to create objects instances loaded with initial data array:

```php
class Example {
Expand Down Expand Up @@ -46,7 +46,11 @@ function alwaysFalse() { return false; }

class Example {
use \DataLoader\FromArray;
const SCHEME = ['id' => 'intval', 'isFalse' => 'alwaysFalse', 'date' => DateTime::class];
const SCHEME = [
'id' => 'intval',
'isFalse' => 'alwaysFalse',
'date' => DateTime::class
];
public $id;
public $date;
public $isFalse = true;
Expand Down
20 changes: 6 additions & 14 deletions src/FromArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,15 @@
*/
trait FromArray {

/**
* @param array $data
* @param callable|null $filter
* @param array $scheme
* @param array $mapping
* @return static
*/
public static function fromArray(array $data = [], callable $filter = null, array $scheme = [], array $mapping = []) {
// Late Static Binding class

public static function fromArray(array $data = [], ?callable $filter = null, array $scheme = [], array $mapping = []): static {
$class = get_called_class();

// merge $scheme with default $class::SCHEME
if (defined($class . '::SCHEME')) {
if (defined(constant_name: "$class::SCHEME")) {
$scheme = array_merge($class::SCHEME, $scheme);
}

// merge $mapping with default $class::MAPPING
if (defined($class . '::MAPPING')) {
if (defined(constant_name: "$class::MAPPING")) {
$mapping = array_flip(array_merge($class::MAPPING, $mapping));
}

Expand All @@ -51,7 +42,7 @@ public static function fromArray(array $data = [], callable $filter = null, arra

if (method_exists($scheme[$property], __FUNCTION__)) {
// 1.1 fromArray() method exists
$obj->{$property} = call_user_func([$scheme[$property], __FUNCTION__], (array)$value, $filter);
$obj->{$property} = call_user_func([$scheme[$property], __FUNCTION__], (array) $value, $filter);
} else {
// 1.2 Create object with constructor
$obj->{$property} = new $scheme[$property]($value);
Expand All @@ -67,6 +58,7 @@ public static function fromArray(array $data = [], callable $filter = null, arra
$obj->{$property} = $value; // assign value to object
}
}

return $obj;
}
}
55 changes: 33 additions & 22 deletions tests/FromArray.basic.phpt
Original file line number Diff line number Diff line change
@@ -1,30 +1,41 @@
<?php

use DataLoader\FromArray;
use Tester\Assert;

require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/bootstrap.php';


class A {
use \DataLoader\FromArray;
public $value;
public $default = 'default value';
}

// Loader test

$a = A::fromArray($data = ['value' => 'this was loaded']);
class Basic {
use FromArray;

Assert::same('this was loaded', $a->value);
Assert::same('default value', $a->default);


$a = A::fromArray(
$data = ['value' => 'NOT THIS', 'default' => 'NOT THIS'],
function ($value) {
return 'CALLBACK';
}
);
public ?string $value = null;
public ?string $default = 'default value';
}

Assert::same('CALLBACK', $a->value);
Assert::same('CALLBACK', $a->default);
test('Check default values', function () {
$basic = Basic::fromArray([]);
Assert::same(null, $basic->value);
Assert::same('default value', $basic->default);
});

test('Value loading', function () {
$basic = Basic::fromArray($data = ['value' => 'this was loaded']);
Assert::same('this was loaded', $basic->value);
Assert::same('default value', $basic->default);
});

test('Change value with callback', function () {
$basic = Basic::fromArray(
$data = [
'value' => 'something else',
'default' => 'something default'
],
function ($value) {
return sprintf('CHANGING TO %s', $value);
}
);

Assert::same('CHANGING TO something else', $basic->value);
Assert::same('CHANGING TO something default', $basic->default);
});
15 changes: 8 additions & 7 deletions tests/FromArray.dateTime.phpt
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
<?php

use DataLoader\FromArray;
use Tester\Assert;

require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/bootstrap.php';

class DateExample {

use \DataLoader\FromArray;
use FromArray;

const SCHEME = ['date' => DateTime::class];

/** @var DateTime */
public $date = DateTime::class;
public string|DateTime $date = DateTime::class;
}

$dateExample = DateExample::fromArray(['date' => '2020-01-01']);
Assert::true($dateExample->date instanceof DateTime);
test('DateTime from array', function () {
$dateExample = DateExample::fromArray(['date' => '2020-01-01']);
Assert::true($dateExample->date instanceof DateTime);
});
21 changes: 10 additions & 11 deletions tests/FromArray.filter.phpt
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
<?php

use DataLoader\FromArray;
use Tester\Assert;

require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/bootstrap.php';

test('Filter callback', function () {
class FilterCallback {
use FromArray;

function changeToFalse() {
return false;
}
public bool $value = true;
}

class FilterCallback {
use \DataLoader\FromArray;
public $value = true;
}

$clb = FilterCallback::fromArray(['value' => true], 'changeToFalse');
Assert::false($clb->value);
$results = FilterCallback::fromArray(['value' => true], fn() => false);
Assert::false($results->value);
});
25 changes: 15 additions & 10 deletions tests/FromArray.latestatic.phpt
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
<?php

use DataLoader\FromArray;
use Tester\Assert;

require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/bootstrap.php';

class A {
use \DataLoader\FromArray;
public $value;
class Base {
use FromArray;

public ?string $value = null;
}

class C extends A {
// inheritance test
class Custom extends Base {

}

// the "Late Static Binding" class name test

$c = C::fromArray(['value' => 'abc']);
test('LateStaticBinding class name test',
function () {
$c = Custom::fromArray(['value' => 'abc']);

Assert::true($c instanceof C);
Assert::same('abc', $c->value);
Assert::type(Custom::class, $c);
Assert::same('abc', $c->value);
}
);
27 changes: 18 additions & 9 deletions tests/FromArray.mapping.phpt
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
<?php

use DataLoader\FromArray;
use Tester\Assert;

require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/bootstrap.php';

class MappingExample {

use \DataLoader\FromArray;
use FromArray;

const MAPPING = [
'anotherId' => 'id',
'exampleNumber' => 'isNumber',
'anotherNumber' => 'number',
];

const SCHEME = [
'isNumber' => 'is_integer',
'number' => 'intval', // convert to integer
];

public $id;
public $isNumber;
public ?int $id = 0;
public ?int $number = 0;
}

$values = MappingExample::fromArray(['anotherId' => 123, 'exampleNumber' => 123]);
Assert::true(is_integer($values->id));
Assert::true($values->isNumber);
test('Mapping properties to another keys', function () {
$values = MappingExample::fromArray(
[
'anotherId' => 123,
'anotherNumber' => '345'
]
);

Assert::same(123, $values->id);
Assert::same(345, $values->number);
});
Loading

0 comments on commit 75f4ea8

Please sign in to comment.