PHP Classes

File: docs/VALUE_OBJECTS.md

Recommend this page to a friend!
  Packages of ?????? ???????   Adaptive Entity Manager   docs/VALUE_OBJECTS.md   Download  
File: docs/VALUE_OBJECTS.md
Role: Auxiliary data
Content type: text/markdown
Description: Auxiliary data
Class: Adaptive Entity Manager
Entity manager to work with multiple data sources
Author: By
Last change:
Date: 3 months ago
Size: 9,954 bytes
 

 

Contents

Class file image Download

Value Objects Support

The Adaptive Entity Manager supports Value Objects - immutable objects that are defined by their values rather than identity. This feature provides better type safety, validation, and domain modeling capabilities.

What are Value Objects?

Value Objects are small objects that represent a simple entity whose equality is not based on identity but on the values of its attributes. Examples include:

  • Money (amount + currency)
  • Email addresses
  • Phone numbers
  • Postal codes
  • User IDs

Key Characteristics

  1. Immutable: Once created, they cannot be changed
  2. Value-based equality: Two objects are equal if their values are equal
  3. Self-validating: They validate their data upon creation
  4. No identity: They don't have an ID or primary key

Usage

1. Creating Value Objects

The default ValueObjectConverterRegistry path expects types to implement ValueObjectInterface (see below). Alternatively, use metadata from / to on value_object fields so domain classes need not reference AEM ? see Domain classes without ValueObjectInterface.

To use the registry and fromPrimitive / toPrimitive:

use Kabiroman\AEM\ValueObject\ValueObjectInterface;

class Email implements ValueObjectInterface
{
    public function __construct(private readonly string $email)
    {
        $this->validate($email);
    }

    public static function fromPrimitive(mixed $value): static
    {
        return new self($value);
    }

    public function toPrimitive(): string
    {
        return $this->email;
    }

    public function equals(ValueObjectInterface $other): bool
    {
        return $other instanceof self && $this->email === $other->email;
    }

    public function __toString(): string
    {
        return $this->email;
    }

    private function validate(string $email): void
    {
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            throw new \InvalidArgumentException('Invalid email address');
        }
    }
}

2. Using Abstract Base Class

For convenience, you can extend AbstractValueObject:

use Kabiroman\AEM\ValueObject\AbstractValueObject;

class UserId extends AbstractValueObject
{
    public function __construct(private readonly int $id)
    {
        if ($id <= 0) {
            throw new \InvalidArgumentException('User ID must be positive');
        }
    }

    public static function fromPrimitive(mixed $value): static
    {
        return new self((int) $value);
    }

    public function toPrimitive(): int
    {
        return $this->id;
    }
}

3. Entity with Value Objects

use Kabiroman\AEM\ValueObject\Common\Email;
use Kabiroman\AEM\ValueObject\Common\Money;

class Product
{
    private int $id;
    private string $name;
    private Email $contactEmail;
    private Money $price;

    // ... getters and setters
}

4. Metadata Configuration

Configure your entity metadata to use Value Objects:

class ProductMetadata extends AbstractClassMetadata
{
    protected array $metadata = [
        Product::class => [
            'id' => [
                'id' => ['type' => 'int'],
            ],
            'fields' => [
                'name' => ['type' => 'string'],
                'contactEmail' => [
                    'type' => 'value_object',
                    'valueObjectClass' => Email::class,
                ],
                'price' => [
                    'type' => 'value_object', 
                    'valueObjectClass' => Money::class,
                    'column' => 'price_json', // Custom column name
                ],
            ],
        ]
    ];
}

Domain classes without ValueObjectInterface

For DDD-style types that must not depend on AEM, declare type => value_object and point AEM at a static factory and an instance extractor:

  • class (preferred) or valueObjectClass: must match the entity property?s PHP type. If both keys are set, they must specify the same FQCN or configuration is rejected. If only one is set, it must still match the property type when provided.
  • from: name of a static method on that class; one required parameter (the raw storage value). It must return an instance of the value object class.
  • to (optional for `Stringable` types): name of an instance method with no required parameters (e.g. `__toString`). If omitted, the value must implement `Stringable` and the field must remain `value_object` with a matching declared type.

Unsupported in this version: union and intersection property types on value_object fields (a clear error is thrown).

Example:

'contactEmail' => [
    'type' => 'value_object',
    'class' => \My\Domain\Email::class,
    'from' => 'fromString',
    'to' => '__toString',
],

Runnable demo in this repository: examples/ddd_domain_vo_demo.php (domain class under examples/ddd_domain_vo/).

Criteria values use the same conversion: you may pass a value object instance and it will be converted to storage before the adapter runs. The instance must be of the field?s declared value object type (the same object you would assign to the entity property).

Implementation note: VO capability is declared on ValueObjectAwareEntityManagerInterface (implemented by AdaptiveEntityManager), not on the base EntityManagerInterface, to avoid breaking custom interface implementations. EntityPersister uses a process-wide static entity factory; multiple independent EntityManager instances in one process are not fully isolated?prefer one logical manager per process.

See RFC-003: Domain-agnostic value object mapping for background.

5. Entity Manager Setup

Enable Value Object support in your Entity Manager:

use Kabiroman\AEM\AdaptiveEntityManager;
use Kabiroman\AEM\ValueObject\Converter\ValueObjectConverterRegistry;

$valueObjectRegistry = new ValueObjectConverterRegistry();

$entityManager = new AdaptiveEntityManager(
    config: $config,
    classMetadataProvider: $metadataProvider,
    entityDataAdapterProvider: $adapterProvider,
    valueObjectRegistry: $valueObjectRegistry // Enable ValueObject support
);

// Now you can work with entities containing Value Objects
$product = new Product();
$product->setContactEmail(Email::fromPrimitive('contact@example.com'));
$product->setPrice(Money::fromPrimitive(['amount' => 12500, 'currency' => 'USD']));

$entityManager->persist($product);
$entityManager->flush();

Built-in Value Objects

The package provides several common Value Objects:

Email

$email = Email::fromPrimitive('user@example.com');
echo $email->getDomain(); // 'example.com'
echo $email->getLocalPart(); // 'user'

Money

$price = Money::fromPrimitive(['amount' => 10000, 'currency' => 'USD']);
echo $price->getFormattedAmount(); // 100.00
echo $price->getCurrency(); // 'USD'

$total = $price->add(Money::fromPrimitive(['amount' => 5000, 'currency' => 'USD']));
echo $total->getFormattedAmount(); // 150.00

UserId

$userId = UserId::fromPrimitive(42);
echo $userId->getValue(); // 42

Custom Converters

You can create custom converters for complex Value Objects:

use Kabiroman\AEM\ValueObject\Converter\ValueObjectConverterInterface;

class CustomValueObjectConverter implements ValueObjectConverterInterface
{
    public function supports(string $className): bool
    {
        return $className === CustomValueObject::class;
    }

    public function convertToPHP(mixed $value, string $className): ?ValueObjectInterface
    {
        // Custom conversion logic
        return CustomValueObject::fromCustomFormat($value);
    }

    public function convertToDatabase(?ValueObjectInterface $valueObject): mixed
    {
        // Custom serialization logic
        return $valueObject?->toCustomFormat();
    }
}

// Register your converter
$valueObjectRegistry->addConverter(new CustomValueObjectConverter());

Database Storage

Value Objects are automatically serialized for database storage:

  • Simple objects (like Email, UserId): Stored as their primitive value
  • Complex objects (like Money): Stored as JSON
  • Custom serialization: Can be handled by custom converters

Validation

Value Objects provide automatic validation:

try {
    $email = Email::fromPrimitive('invalid-email');
} catch (InvalidArgumentException $e) {
    echo $e->getMessage(); // "invalid-email is not a valid email address"
}

try {
    $userId = UserId::fromPrimitive(-1);
} catch (InvalidArgumentException $e) {
    echo $e->getMessage(); // "User ID must be a positive integer"
}

Benefits

  1. Type Safety: PHP's type system ensures you can't accidentally pass wrong types
  2. Validation: Data is validated at creation time
  3. Immutability: Prevents accidental data corruption
  4. Expressiveness: Code becomes more readable and self-documenting
  5. Reusability: Value Objects can be used across different entities
  6. Testing: Easier to test business logic with well-defined value objects

Performance Considerations

  • Value Objects add minimal overhead
  • Serialization/deserialization is handled efficiently
  • Runtime caching minimizes conversion costs
  • Consider using custom converters for performance-critical paths

Migration from Primitives

You can gradually migrate from primitive types to Value Objects:

  1. Add the Value Object class
  2. Update entity properties to use the Value Object type
  3. Update metadata configuration
  4. Enable Value Object support in Entity Manager
  5. Update your application code to work with Value Objects

The system maintains backward compatibility during migration.