-
Notifications
You must be signed in to change notification settings - Fork 5
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
Showing
11 changed files
with
219 additions
and
162 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 |
---|---|---|
@@ -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": "*" | ||
} | ||
} |
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
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
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,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); | ||
}); |
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,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); | ||
}); |
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,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); | ||
}); |
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,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); | ||
} | ||
); |
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,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); | ||
}); |
Oops, something went wrong.