1. Go to this page and download the library: Download sunrise/hydrator 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/ */
sunrise / hydrator example snippets
enum Status: int
{
case Disabled = 0;
case Enabled = 1;
}
final readonly class CategoryDto
{
public function __construct(
public string $name,
) {
}
}
final readonly class TagDto
{
public function __construct(
public string $name,
) {
}
}
final class TagDtoCollection extends ArrayObject
{
public function __construct(TagDto ...$tags)
{
parent::__construct($tags);
}
}
final readonly class PublicationDto
{
public function __construct(
public string $name,
public CategoryDto $category,
public TagDtoCollection $tags,
public Status $status = Status::Disabled,
#[\Sunrise\Hydrator\Annotation\Format(DateTimeInterface::RFC3339)]
public DateTimeImmutable $createdAt = new DateTimeImmutable(),
) {
}
}
#[\Sunrise\Hydrator\Annotation\DefaultValue('foo')]
public readonly string $value;
public readonly ?string $value;
public readonly bool $value;
public readonly int $value;
public readonly float $value;
public readonly string $value;
public readonly array $value;
#[\Sunrise\Hydrator\Annotation\Subtype(SomeDto::class)]
public readonly array $value;
#[\Sunrise\Hydrator\Annotation\Subtype(SomeDto::class, limit: 100)]
public readonly array $value;
final class TagDto
{
}
final class TagDtoCollection implements \ArrayAccess
{
}
final class CreateProductDto
{
public function __construct(
#[\Sunrise\Hydrator\Annotation\Subtype(TagDto::class, limit: 10)]
public readonly TagDtoCollection $tags,
) {
}
}
final class TagDtoCollection implements \ArrayAccess
{
public function __construct(public TagDto ...$tags)
{
}
}
final class CreateProductDto
{
public function __construct(
public readonly TagDtoCollection $tags,
) {
}
}
#[\Sunrise\Hydrator\Annotation\Subtype(\DateTimeImmutable::class, limit: 100)]
#[\Sunrise\Hydrator\Annotation\Format('Y-m-d H:i:s')]
public readonly array $value;
use Sunrise\Hydrator\Type;
$data = [...];
$collection = $hydrator->castValue($data, Type::fromName(SomeDtoCollection::class));
#[\Sunrise\Hydrator\Annotation\Format('Y-m-d H:i:s')]
public readonly DateTimeImmutable $value;
#[\Sunrise\Hydrator\Annotation\Format('U')]
public readonly DateTimeImmutable $value;
use Sunrise\Hydrator\Dictionary\ContextKey;
use Sunrise\Hydrator\Hydrator;
$hydrator = new Hydrator([
ContextKey::TIMESTAMP_FORMAT => 'Y-m-d H:i:s',
]);
public readonly DateTimeZone $value;
use Sunrise\Hydrator\Dictionary\ContextKey;
use Sunrise\Hydrator\Hydrator;
$hydrator = new Hydrator([
ContextKey::TIMEZONE => 'Europe/Kyiv',
]);
public readonly \Ramsey\Uuid\UuidInterface $value;
public readonly \Symfony\Component\Uid\UuidV4 $value;
public readonly SomeEnum $value;
public readonly SomeEnum $value;
public readonly SomeDto $value;
use Sunrise\Hydrator\Exception\InvalidValueException;
use Sunrise\Hydrator\Type;
use Sunrise\Hydrator\TypeConverterInterface;
use Psr\Message\UriInterface;
use Sunrise\Http\Message\Uri;
final class UriTypeConverter implements TypeConverterInterface
{
public function castValue($value, Type $type, array $path): Generator
{
if ($type->getName() <> UriInterface::class) {
return;
}
if (!\is_string($value)) {
throw InvalidValueException::mustBeString($path, $value);
}
try {
yield new Uri($value);
} catch (\InvalidArgumentException $e) {
throw new InvalidValueException(
'This value is not a valid URI.',
'c66741c6-e3c0-4522-a8e3-97528d7712a3',
$path,
);
}
}
public function getWeight(): int
{
return 0;
}
}