PHP code example of lukasz93p / doctrine-domain-id-types

1. Go to this page and download the library: Download lukasz93p/doctrine-domain-id-types 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/ */

    

lukasz93p / doctrine-domain-id-types example snippets


 
// module.config.php
use Ramsey\Uuid\Doctrine\UuidType;

return [
    'doctrine' => [
        'configuration' => [
            'orm_default' => [
                'types' => [
                    UuidType::NAME => UuidType::class,

 


namespace Lukasz93P\DoctrineDomainIdTypes\domainId;


interface AggregateId
{
    /**
     * @return AggregateId
     */
    public static function generate();

    /**
     * @param string $string
     * @return AggregateId
     */
    public static function fromString(string $string);

    public function toString(): string;

    public function equals(AggregateId $otherAggregateId): bool;

}




namespace App\domain\id;


use Lukasz93P\DoctrineDomainIdTypes\domainId\AggregateId;

interface ProductId extends AggregateId
{
    // some additional method specific for ProductId if needed
}




namespace App\infrastructure\persistence\testPackage\id;


use App\domain\id\ProductId;
use Lukasz93P\DoctrineDomainIdTypes\domainId\BaseAggregateId;

class ProductDoctrineId extends BaseAggregateId implements ProductId
{
    // BaseAggregateId class provides implementation for all 
    // methods declared in Lukasz93P\DoctrineDomainIdTypes\domainId\AggregateId    

    // ! IMPORTANT ! do not create constructor for this class because constructor
    // of base class Lukasz93P\DoctrineDomainIdTypes\domainId\BaseAggregateId is private final
}




namespace App\infrastructure\persistence\product\id;


use Lukasz93P\DoctrineDomainIdTypes\doctrineId\AggregateIdDoctrineFieldType;

class ProductDoctrineIdType extends AggregateIdDoctrineFieldType
{
    // implement abstract method which should return class name 
    // of Your's id interface implementation
    protected function mappedAggregateIdImplementationClassName(): string
    {
        return ProductDoctrineId::class;
    }

    // implement abstract method which return name of custom Doctrine field
    public function getName()
    {
        return 'product_id';
    }

}

    /**
     * @var ProductId
     *
     * @ORM\Id
     * @ORM\Column(type="product_id", unique=true)
     * @ORM\GeneratedValue(strategy="CUSTOM")
     * @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidGenerator")
     */
    protected $id;

    /**
     * @var ProductId
     *
     * @ORM\Id
     * @ORM\Column(type="product_id", unique=true)
     */
    protected $id;

    public function __construct(ProductId $id)
    {
        $this->id = $id;
    }
 php
\Doctrine\DBAL\Types\Type::addType('uuid', 'Ramsey\Uuid\Doctrine\UuidType');
 php
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="products")
 */
class Product
{
    /**
     * @var \Ramsey\Uuid\UuidInterface
     *
     * @ORM\Id
     * @ORM\Column(type="uuid", unique=true)
     * @ORM\GeneratedValue(strategy="CUSTOM")
     * @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidGenerator")
     */
    protected $id;

    public function getId()
    {
        return $this->id;
    }
}