Value Object
Value Object is an immutable, validated domain primitive with value semantics.
Generated facts
- Layer
- domain
- Generated path
app/Pulsar/Domain/{Domain}/ValueObjects/{Name}.php- Generator command
make:value-object- Workflow method
- None
- Stability
- Current manifest surface (Pulsar 0.4.1)
Canonical example
Generated src/stubs/value-object.stub — canonical synchronized stub.
<?php
namespace {{namespace}};
use DomainException;
final readonly class {{name}}
{
public function __construct(
public string $value,
) {
if (trim($value) === '') {
throw new DomainException('{{name}} cannot be empty.');
}
}
public static function fromString(string $value): self
{
return new self($value);
}
public function equals(self $other): bool
{
return $this->value === $other->value;
}
public function __toString(): string
{
return $this->value;
}
}
Responsibility
Value Object owns the responsibility stated above; it must not absorb delivery, transaction, or unrelated cross-layer behavior.
Placement and dependencies
Keep this type in its generated Domain path. Domain is independent of delivery concerns; Infrastructure implements Domain-owned Contracts at its boundary.
Workflow and tests
Value Object is consumed by the owning Domain or Service workflow and returns a delivery-neutral result. Test its direct contract, its rejected boundary cases, and the caller or callee that proves the rule in application flow.
Three pitfalls
- ❌ Prohibited: move this responsibility into a neighboring type merely because it is nearby.
- ✅ Correct: keep the generated placement and depend only on the documented layer direction.
- ✅ Correct: test the boundary through the caller and the return or side effect visible to its callee.
Related reading
Read architecture placement for shared rationale and follow this page’s related concept links for the adjacent responsibility.
Boundaries
❌ Prohibited: Do not use a Value Object for a closed owned set of states.
✅ Correct: Use an Enum for a closed set and a Value Object for validated value semantics.