<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
eboreum / caster-doctrine-entity-formatter example snippets
declare(strict_types=1);
namespace SomeCustomNamespace_9c95fb43;
use Doctrine\ORM\Mapping as ORM;
use Eboreum\Caster\Attribute\DebugIdentifier;
use Eboreum\Caster\Caster;
use Eboreum\Caster\Collection\Formatter\ObjectFormatterCollection;
use Eboreum\Caster\Contract\DebugIdentifierAttributeInterface;
use Eboreum\CasterDoctrineEntityFormatter\EntityFormatter;
#[ORM\Entity]
class User implements DebugIdentifierAttributeInterface
{
#[ORM\Id]
public ?int $id = null;
#[DebugIdentifier]
public string $name = 'foo';
}
$user = new User();
$caster = Caster::create()->withCustomObjectFormatterCollection(new ObjectFormatterCollection([
new EntityFormatter(),
]));
echo $caster->cast($user) . "\n";
$user->id = 42;
$user->name = 'bar';
echo "\n";
echo $caster->cast($user) . "\n";
declare(strict_types=1);
namespace SomeCustomNamespace_fd813f94;
use Doctrine\ORM\Mapping as ORM;
use Eboreum\Caster\Attribute\DebugIdentifier;
use Eboreum\Caster\Caster;
use Eboreum\Caster\Collection\Formatter\ObjectFormatterCollection;
use Eboreum\Caster\Contract\DebugIdentifierAttributeInterface;
use Eboreum\CasterDoctrineEntityFormatter\EntityFormatter;
#[ORM\Entity]
class User implements DebugIdentifierAttributeInterface
{
#[ORM\Id]
public ?int $id;
#[DebugIdentifier]
public string $name = 'foo';
}
$user = new User();
$entityFormatter = new EntityFormatter();
$entityFormatter = $entityFormatter->withIsRenderingDebugIdentifierOnlyWhenIdHasNotBeenSet(true);
$caster = Caster::create()->withCustomObjectFormatterCollection(new ObjectFormatterCollection([
$entityFormatter,
]));
echo $caster->cast($user) . "\n";
$user->id = null;
echo "\n";
echo $caster->cast($user) . "\n";
$user->id = 42;
echo "\n";
echo $caster->cast($user) . "\n";