PHP code example of hradigital / php-datatypes-laravel-doctrine

1. Go to this page and download the library: Download hradigital/php-datatypes-laravel-doctrine 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/ */

    

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

/**
 * @ORM\Column(type="DatetimeType", name="date_of_birth", nullable=true)
 */
private ?Datetime $dateOfBirth = null;

/**
 * @ORM\Column(type="NativeDateTimeType", name="last_login_at", nullable=true)
 */
private ?DateTime $lastLoginAt = null;

// 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(),
];

#[ORM\Column(type: 'UrlType')]
private Url $url;

#[ORM\Column(type: 'string', length: 32, unique: true)]
private string $urlHash;   // populated from $this->url->getHash()

use Doctrine\DBAL\Types\Type;
use HraDigital\Components\DatatypesDoctrine\DoctrineTypes;

foreach (DoctrineTypes::defaults() as $name => $class) {
    if (! Type::hasType($name)) {
        Type::addType($name, $class);
    }
}
bash
php artisan vendor:publish --tag=datatypes-doctrine-config