PHP code example of eboreum / caster-doctrine-entity-formatter

1. Go to this page and download the library: Download eboreum/caster-doctrine-entity-formatter 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/ */

    

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";

json
"php": "^8.1",
"ext-mbstring": "*",
"doctrine/annotations": "^1.0",
"doctrine/orm": "^2.0",
"eboreum/caster": "^1.0",
"eboreum/exceptional": "^1.0"

\SomeCustomNamespace_9c95fb43\User {$id = (null) null, $name = (string(3)) "foo"}

\SomeCustomNamespace_9c95fb43\User {$id = (int) 42, $name = (string(3)) "bar"}


\SomeCustomNamespace_fd813f94\User {$id = (uninitialized), $name = (string(3)) "foo"}

\SomeCustomNamespace_fd813f94\User {$id = (null) null, $name = (string(3)) "foo"}

\SomeCustomNamespace_fd813f94\User {$id = (int) 42}