In some cases, where Enums are not suitable, ValueObjects are used. All value objects should extend AbstractValueObject and override two constants:
public const DEFAULT_VALUE = '';
public const AVAILABLE_VALUES = [];
Here is the example of implementation for user state:
App\Model\ValueObject\UserState
:
<?php
declare(strict_types=1);
namespace App\Model\ValueObject;
use AnzuSystems\Contracts\Model\ValueObject\AbstractValueObject;
final class UserState extends AbstractValueObject
{
public const ACTIVE = 'active';
public const GDPR_DELETED = 'gdpr_deleted';
public const BANNED = 'banned';
public const AVAILABLE_VALUES = [
self::ACTIVE,
self::GDPR_DELETED,
self::BANNED,
];
public const DEFAULT_VALUE = self::ACTIVE;
}
App\Model\ValueObject\UserState
:
<?php
declare(strict_types=1);
namespace App\Doctrine\Type;
use App\Model\ValueObject\UserState;
use Doctrine\DBAL\Platforms\AbstractPlatform;
final class UserStateType extends AbstractValueObjectType
{
public function convertToPHPValue($value, AbstractPlatform $platform): UserState
{
return new UserState((string) $value);
}
}
config/packages/doctrine.yaml
:
doctrine:
dbal:
types:
UserStateType: App\Doctrine\Type\UserStateType
App\Entity\User
:
<?php
declare(strict_types=1);
namespace App\Entity;
use AnzuSystems\CommonBundle\Entity\AnzuUser;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
class User extends AnzuUser
{
#[ORM\Column(type: 'UserStateType')]
private UserState $state;
}