<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
hradigital / php-datatypes-laravel-doctrine example snippets
// Without this package - the Entity holds a primitive, and every caller converts by hand.
#[ORM\Column(type: 'string', length: 255)]
private ?string $email = null;
// With this package - the Entity holds the Value Object, validated and normalized.
#[ORM\Column(type: 'EmailAddressType', nullable: true)]
private ?EmailAddress $email = null;
use Doctrine\ORM\Mapping as ORM;
use HraDigital\Datatypes\Datetime\Datetime;
use HraDigital\Datatypes\Financial\Money;
use HraDigital\Datatypes\ValueObjects\Address;
use HraDigital\Datatypes\Web\EmailAddress;
use HraDigital\Datatypes\Web\Seo\Slug;
#[ORM\Entity]
#[ORM\Table(name: 'customers')]
class Customer
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private int $id;
#[ORM\Column(type: 'EmailAddressType', length: 255, unique: true)]
private EmailAddress $email;
#[ORM\Column(type: 'SlugType', unique: true)]
private Slug $slug;
#[ORM\Column(type: 'AddressType', nullable: true)]
private ?Address $billingAddress = null;
#[ORM\Column(type: 'MoneyType')]
private Money $lifetimeValue;
#[ORM\Column(type: 'DatetimeType')]
private Datetime $registeredAt;
}
// Money as two real columns - sortable, indexable, summable.
#[ORM\Column(type: 'bigint')]
private int $totalAmount;
#[ORM\Column(type: 'CurrencyType')]
private Currency $totalCurrency;
public function getTotal(): Money
{
return Money::fromScalars($this->totalAmount, $this->totalCurrency->value);
}
return [
// Turns the whole package off without uninstalling it.
'enabled' => true,
// Doctrine type name => Type class.
'types' => DoctrineTypes::defaults(),
];
use Doctrine\DBAL\Types\Type;
use HraDigital\Components\DatatypesDoctrine\DoctrineTypes;
foreach (DoctrineTypes::defaults() as $name => $class) {
if (! Type::hasType($name)) {
Type::addType($name, $class);
}
}