cubicmushroom/valueobjects
A small PHP package providing lightweight Value Object classes to model immutable domain values (e.g., IDs, money, email, dates) with validation and type-safety. Useful for cleaner Laravel apps and DDD-style codebases.
Installation
composer require cubicmushroom/valueobjects
Add to composer.json if not auto-loaded:
"autoload": {
"psr-4": {
"App\\": "app/",
"CubicMushroom\\ValueObjects\\": "vendor/cubicmushroom/valueobjects/src/"
}
}
Run composer dump-autoload.
First Use Case
Define a simple value object (e.g., Email):
use CubicMushroom\ValueObjects\AbstractValueObject;
class Email extends AbstractValueObject
{
protected $value;
public function __construct(string $value)
{
$this->value = $value;
}
public function getValue(): string
{
return $this->value;
}
public function __toString(): string
{
return $this->value;
}
}
Key Features to Explore
value for comparison, not object identity.Domain Modeling Use value objects for:
Money, Email, Username).OrderId, ProductSku).
Example:class Money extends AbstractValueObject
{
public function __construct(
private float $amount,
private string $currency
) {}
public function add(Money $other): self
{
if ($this->currency !== $other->currency) {
throw new \InvalidArgumentException("Currencies must match");
}
return new self($this->amount + $other->amount, $this->currency);
}
}
Validation
Override isValid() for custom rules:
class Email extends AbstractValueObject
{
public function isValid(): bool
{
return filter_var($this->value, FILTER_VALIDATE_EMAIL) !== false;
}
}
Integration with Eloquent Store value objects as JSON in a database column:
use Illuminate\Database\Eloquent\Casts\Attribute;
class User extends Model
{
protected function email(): Attribute
{
return Attribute::make(
get: fn ($value) => new Email($value),
set: fn ($value) => $value->getValue(),
);
}
}
Collections
Use ValueObjectCollection for grouped value objects:
use CubicMushroom\ValueObjects\ValueObjectCollection;
$emails = new ValueObjectCollection([
new Email('user1@example.com'),
new Email('user2@example.com'),
]);
Email, Money).Serialization Issues
__serialize()/__unserialize() or use JsonSerializable.class Email implements JsonSerializable
{
public function jsonSerialize(): string
{
return $this->value;
}
}
Database Storage
Attribute casting or serialize manually.Performance Overhead
Magic Methods
__get()/__set() to maintain immutability. Use explicit getters/setters.$email1 = new Email('test@example.com');
$email2 = new Email('test@example.com');
var_dump($email1 == $email2); // true (values compared)
var_dump($email1 === $email2); // false (object identity)
isValid() returns false and inspect the value object’s logic.Custom Comparison
Override equals() for complex logic:
public function equals(AbstractValueObject $other): bool
{
return $this->value === $other->value;
}
Value Object Factories Create static factory methods for complex creation:
class Money
{
public static function fromString(string $value): self
{
[$amount, $currency] = explode(' ', $value);
return new self((float)$amount, $currency);
}
}
Laravel Integration
public function rules(): array
{
return [
'email' => ['required', 'string'],
];
}
public function prepareForValidation()
{
$this->merge([
'email' => new Email($this->email),
]);
}
toArray():
public function toArray(): array
{
return [
'email' => $this->email->getValue(),
];
}
Testing
$this->partialMock(Email::class, ['isValid'])
->shouldReceive('isValid')
->once()
->andReturn(true);
How can I help you explore Laravel packages today?