一键导入
laravel-value-objects
Immutable value objects for domain values. Use when creating or modifying value objects like money, coordinates, or other domain primitives.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Immutable value objects for domain values. Use when creating or modifying value objects like money, coordinates, or other domain primitives.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Form request validation and comprehensive validation testing. Use when creating or modifying form requests, validation rules, or validation tests.
Feature module pattern organizing domain logic into queries, mutations, and actions. Use when implementing data fetching with filters, API mutations with loading states, business logic with UI feedback, or organizing domain-specific code.
File-based routing with page patterns for lists, details, and navigation. Use when creating pages, defining page meta (permissions, layouts), implementing list/detail patterns, or setting up breadcrumbs and headers.
Foundational architecture for Nuxt 4 + Vue 3 + Nuxt UI applications. Use when starting new projects, understanding project structure, or making architectural decisions about directory organization, technology choices, and pattern selection.
Vue component patterns with Composition API and script setup. Use when creating components, understanding script setup order convention, organizing component directories, or implementing component patterns like slideovers, modals, and tables.
Creating custom Vue composables with proper patterns. Use when building reusable stateful logic, shared state management, or encapsulating feature-specific behavior.
| name | laravel-value-objects |
| description | Immutable value objects for domain values. Use when creating or modifying value objects like money, coordinates, or other domain primitives. |
Value objects are simple, immutable objects representing domain concepts.
Related guides:
Use value objects when:
Use DTOs when:
<?php
declare(strict_types=1);
namespace App\Values;
use App\Enums\ProcessResult as ProcessResultEnum;
class ProcessResult
{
public function __construct(
public readonly ProcessResultEnum $result,
public readonly ?string $message = null,
) {}
public static function success(?string $message = null): self
{
return new self(ProcessResultEnum::Success, $message);
}
public static function skip(?string $message = null): self
{
return new self(ProcessResultEnum::Skip, $message);
}
public static function fail(?string $message = null): self
{
return new self(ProcessResultEnum::Fail, $message);
}
public function isSuccess(): bool
{
return $this->result === ProcessResultEnum::Success;
}
public function isFail(): bool
{
return $this->result === ProcessResultEnum::Fail;
}
}
// In actions
return ProcessResult::success('Order processed successfully');
return ProcessResult::skip('Order already processed');
return ProcessResult::fail('Payment declined');
// Checking results
if ($result->isSuccess()) {
// Handle success
}
if ($result->isFail()) {
// Handle failure
}
// Creating money values
$price = Money::of(29.99); // From major units (£29.99)
$shipping = Money::ofMinor(500); // From minor units (£5.00)
$usdPrice = Money::of(19.99, 'USD'); // Explicit currency
// Arithmetic (returns new instances)
$total = $price->plus($shipping);
$discounted = $total->minus(Money::of(5));
$refund = $total->negated();
// Comparison
$total->isZero();
$total->isGreaterThan($price);
$total->isEqualTo($other);
// Display
echo $total->format(); // "£34.99"
echo $refund->format(showNegativeInParentheses: true); // "(£34.99)"
// Storage (minor units as int)
$total->getMinorAmount(); // 3499
$total->getCurrencyCode(); // "GBP"
→ Full implementation: Money.php
Use final readonly class — all properties immutable, class cannot be extended:
final readonly class Money implements JsonSerializable, Stringable
{
private function __construct(private BrickMoney $money) {}
}
Force controlled instantiation:
private function __construct(/* ... */) {}
public static function of(BigNumber|int|float|string $amount, string $currency = 'GBP'): self
public static function ofMinor(BigNumber|int|float|string $minorAmount, string $currency = 'GBP'): self
public static function success(?string $message = null): self
Wrap third-party libraries behind your own API using __call() delegation:
public function __call(string $name, array $arguments): mixed
{
// Delegate to wrapped library, wrapping results as needed
}
Operations always return new instances (immutability):
$discounted = $price->minus(Money::of(5)); // $price unchanged
$refund = $price->negated(); // $price unchanged
Value objects typically implement JsonSerializable, Stringable, and Wireable (Livewire) for integration with framework features and storage.
app/ValueObjects/
├── Money.php
├── ProcessResult.php
├── Coordinate.php
└── EmailAddress.php
Value objects:
readonly)Use for domain concepts with behavior, not simple data transfer.