1. Go to this page and download the library: Download xthiago/id-value-object 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/ */
namespace YourApp;
use Xthiago\ValueObject\Id\Id;
// Generate a new ID (UUID v4):
$generatedId = Id::generate();
echo $generatedId; // prints something like `b18c7bbe-da70-4c86-8b8f-145abb21a7c7`.
// Create an ID from string:
$parsedId = Id::fromString('Foo');
echo $parsedId; // prints 'Foo'.
// Comparing two instances of Id:
var_dump($generatedId->isEqualTo($parsedId)); // prints: `false`
var_dump($parsedId->isEqualTo(Id::fromString('Foo'))); // prints: `true`
namespace YourApp;
use Doctrine\ORM\Mapping as ORM;
use Xthiago\ValueObject\Id\Id;
/**
* @ORM\Entity()
*/
class Product
{
/**
* @ORM\Id
* @ORM\Column(type="xthiago_id", name="id")
*
* @var Id
* @psalm-var Id<Product>
*/
private $id;
// other attributes goes here.
public function __construct(Id $id)
{
$this->id = $id;
}
/** @psalm-return Id<Product> */
public function id(): Id
{
return $this->id();
}
}
class ProductId extends Id
{
}
class ProductIdDbalType extends DoctrineDbalType
{
public const NAME = 'product_id';
public function getConcreteIdClass(): string
{
return ProductId::class;
}
}
namespace YourApp;
use Doctrine\ORM\Mapping as ORM;
#[
ORM\Entity,
ORM\Table('product')
]
class Product
{
public function __construct(
#[
ORM\Column(name: 'id', type: ProductIdDbalType::NAME),
ORM\Id
]
private ProductId $id,
)
{}
}
$serializer = new Serializer(
normalizers: [
new IdInterfaceNormalizer(), // <---
// others normalizers...
],
encoders: [new JsonEncoder()]
);
class MyAwesomeTest extends Testcase
{
use FreezesUuidTrait;
protected function setUp(): void
{
// we could freeze the uuids here :)
}
protected function tearDown(): void
{
$this->unfreezeUuid(); // <-- This is important to unfreeze the uuid generation.
}
public function test_fixed_uuid(): void
{
// Fixing the uuid value (this can also be set on `setUp()` method):
$this->freezeUuidV4WithFixedValue('866cc948-b6de-4cdc-8f5e-3b53a58a9f63');
// All generated Id will have the same uuid value.
$this->assertSame('866cc948-b6de-4cdc-8f5e-3b53a58a9f63', (string) Id::generate());
$this->assertSame('866cc948-b6de-4cdc-8f5e-3b53a58a9f63', (string) Id::generate());
$this->assertSame('866cc948-b6de-4cdc-8f5e-3b53a58a9f63', (string) Id::generate());
}
public function test_fixed_uuid_sequence(): void
{
// Fixing the uuid value with a known sequence:
$this->freezeUuidV4WithKnownSequence(
'e2d5d0fd-a719-4da7-976d-b5cd184fa615'
'4c98d212-19ed-4c41-9ea2-4b2d48d7410d'
'2acfaf05-25c3-4db9-9fea-5d71a0c3f909'
);
// Each new Id instance will assume one uuid from the known sequence:
$this->assertSame('e2d5d0fd-a719-4da7-976d-b5cd184fa615', (string) Id::generate());
$this->assertSame('4c98d212-19ed-4c41-9ea2-4b2d48d7410d', (string) Id::generate());
$this->assertSame('2acfaf05-25c3-4db9-9fea-5d71a0c3f909', (string) Id::generate());
// If we call again Id::generate(), it will throw RuntimeException because there is no remaining uuid in the
// available list.
$this->assertException(RunTimeException::class);
Id::generate();
$this->fail('The expected exception was not thrown.');
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.