1. Go to this page and download the library: Download fab2s/dt0 library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
fab2s / dt0 example snippets
use fab2s\Dt0\Dt0;
// works if all public props have defaults
$dt0 = new SomeDt0;
// set at least props without default
$dt0 = new SomeDt0(readOnlyProp: $someValue /*, ... */); // <= argument order does not matter
// unless SomeDt0 has a constructor
// same as
$dt0 = SomeDt0::make(readOnlyProp: $someValue /*, ... */); // <= argument order never matter
$value = $dt0->readOnlyProp; // $someValue
/** @var array|string|SomeDt0|Dt0|null|mixed $wannaBeDt0 */
$dt0 = SomeDt0::tryFrom($wannaBeDt0); // return null when nothing works
/** @var Dt0 $dt0 */
$dto = SomeDt0::from($wannaBeDt0); // throws a Dt0Exception when nothing matched or more Throwable when something is too wrong
// keeps objects as such
$array = $dt0->toArray();
// toArray with call to jsonSerialize on implementing members
$jsonArray = $dt0->toJsonArray();
// same as
$jsonArray = $dt0->jsonSerialize();
// toJson
$json = $dt0->toJson();
// same as
$json = json_encode($dt0);
// will work if Dt0 has consistent in/out casters
// that is if caster out type is valid for input
$fromJson = SomeDt0::fromJson($json);
// same as
$fromJson = SomeDt0::fromString($json);
$fromJson->equals($dt0); // true
// always true
$dto->equals(SomeDt0::fromArray($dto->toArray()));
// serializable
$serialized = serialize($dt0);
$unserialized = unserialize($serialized);
$unserialized->equal($dt0); // true
// Immutability with ...
$anotherInstance = $dto->clone();
$anotherInstance->equals($dto); // true
// ... updates :o
$updated = $dto->update(readOnlyProp: $anotherValue);
// or
$updated = $dto->update(...['readOnlyProp' => $anotherValue]);
$updated->->equals($dto); // false
$updated->readOnlyProp; // $anotherValue
use fab2s\Dt0\Attribute\Casts;
use fab2s\Dt0\Attribute\Cast;
use fab2s\Dt0\Dt0;
#[Casts(
new Cast(default: 'defaultFromCast', propName: 'prop1'),
// same as
prop1: new Cast(default: 'defaultFromCast'),
// ...
)]
class MyDt0 extends Dt0 {
public readonly string $prop1;
}
use fab2s\Dt0\Attribute\Casts;
use fab2s\Dt0\Attribute\Cast;
use fab2s\Dt0\Dt0;
class MyDt0 extends Dt0 {
#[Cast(default: 'defaultFromCast')]
public readonly string $prop1;
}
use fab2s\Dt0\Attribute\Cast;
use fab2s\Dt0\Caster\DateTimeCaster;
use fab2s\Dt0\Caster\DateTimeFormatCaster;
use fab2s\Dt0\Dt0;
class MyDt0 extends Dt0 {
#[Cast(in: DateTimeCaster::class, out: new DateTimeFormatCaster(DateTimeFormatCaster::ISO))]
public readonly DateTime $date;
}
/** @var Dt0 $dt0 */
$dt0 = MyDt0::make(date:'1337-01-01 00:00:00');
$dt0->toArray();
/*
[
'date' => DateTimeInstance,
]
*/
$dt0->jsonSerialize();
/*
[
'date' => '1337-01-01T00:00:00.000000Z',
]
*/
use fab2s\Dt0\Dt0;
use fab2s\Dt0\Attribute\Cast;
use fab2s\Dt0\Attribute\Casts;
#[Casts(
new Cast(default: 'defaultFromCast', propName: 'propClassCasted'),
// same as
propClassCasted: new Cast(default: 'defaultFromCast'),
)]
class MyDt0 extends Dt0
{
public readonly string $propClassCasted;
#[Cast(default: null)]
public readonly ?string $propCasted;
#[Cast(renameFrom: 'inputName', renameTo: 'outputName', default: 'default')]
public readonly string $propRenamed;
}
$dt0 = MyDt0::make();
$dt0->propClassCasted; // 'defaultFromCast'
$dt0->propCasted; // 'null'
$dt0->propRenamed; // 'default'
$dt0 = MyDt0::make(propCasted: 'Oh Yeah', inputName: "I don't exist"); // <= argument order never matter
$dt0->propRenamed; // "I don't exist"
$dt0->toArray();
/**
[
'propClassCasted' => 'defaultFromCast',
'propCasted' => 'Oh Yeah',
'propRenamed' => "I don't exist",
]
*/
// same as
$dt0 = MyDt0::make(propCasted: 'Oh Yeah', outputName: "I don't exist");
$dt0->propRenamed; // "I don't exist"
// all renameTo are added to renameFrom
$dt0->equal(MyDt0::fromArray($dt0->toArray()); // true
$dt0 = MyDt0::fromArray([
'propCasted' => 'Oh', // <= order never matter
'propClassCasted' => 'Ho',
]);
$dt0->propRenamed; // 'default'
$dt0->toArray();
/**
[
'propClassCasted' => 'Ho',
'propCasted' => 'Oh',
'propRenamed' => 'default',
]
*/
// output renaming only occurs in json format
$dt0->toJsonArray();
/**
[
'propClassCasted' => 'Ho',
'propCasted' => 'Oh',
'outputName' => 'default',
]
*/
#[Cast(renameFrom: ['alias', 'legacy_name'])] // first in wins the race
public readonly string $prop;
class ConstructedDt0 extends Dt0
{
// un-casted
public readonly string $stringNoCast;
#[Cast(/*...*/)]
public readonly ?string $stringCasted;
public function __construct(
public readonly string $promotedPropNoCast,
#[Cast(/*...*/)]
public readonly string $promotedPropCasted = 'default',
// all constructor parameters, promoted on not, can be casted
#[Cast(/*...*/)]
?string $myCustomVar = null,
// Mandatory, the remaining $args will be used to further
// initialize other public properties in this class
...$args,
) {
// where the magic happens
parent::__construct(...$args);
}
}
// now you can
$dt0 = new ConstructedDt0(
promotedPropNoCast: 'The order',
promotedPropCasted: 'matters',
myCustomVar: 'for constructor parameters',
stringCasted: 'but not',
stringNoCast: 'for regular props',
);
// ::make, ::fromArray, ::fromString, ::from ... don't care about argument orders
$dt0 = ConstructedDt0::make(
stringCasted: 'The Order',
stringNoCast: 'never',
promotedPropNoCast: 'matter',
promotedPropCasted: 'outside',
myCustomVar: 'of the constructor',
);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.