uuid: - value: 62684ebe-62d0-4999-b4c3-c768e371caac langcode: - value: en type: - target_id: daily_email target_type: node_type target_uuid: 8bde1f2f-eef9-4f2d-ae9c-96921f8193d7 revision_timestamp: - value: '2025-05-11T09:00:44+00:00' revision_uid: - target_type: user target_uuid: b8966985-d4b2-42a7-a319-2e94ccfbb849 revision_log: { } status: - value: true uid: - target_type: user target_uuid: b8966985-d4b2-42a7-a319-2e94ccfbb849 title: - value: | Data transfer objects and value objects created: - value: '2023-04-14T00:00:00+00:00' changed: - value: '2025-05-11T09:00:44+00:00' promote: - value: false sticky: - value: false default_langcode: - value: true revision_translation_affected: - value: true path: - alias: /daily/2023/04/14/data-transfer-objects-and-value-objects langcode: en body: - value: |
In the last few emails, we've seen a data transfer object called AccountDetails
that stores the account number and sort code for a bank account.
A data transfer object only stores data but we could also use a value object that can do more tasks, like validating the values.
For example, we could validate that the account number is the correct length and the sort code is the correct format:
class AccountDetails {
public function __construct(
public readonly string $accountNumber,
public readonly string $sortCode,
) {
// Validate the account number.
if (!preg_match('/^\d{8}$/', $accountNumber)) {
throw new \InvalidArgumentException('Account number must be an 8-digit number');
}
// Validate the sort code.
if (!preg_match('/^\d{2}-\d{2}-\d{2}$/', $sortCode)) {
throw new \InvalidArgumentException('Sort code must be in the format 00-00-00');
}
}
}
Helper methods can also be added to a value object as long as they don't modify the state of the object, such as getting the raw sort code without the separating dashes or performing further checks on the input values.
format: full_html processed: |In the last few emails, we've seen a data transfer object called AccountDetails
that stores the account number and sort code for a bank account.
A data transfer object only stores data but we could also use a value object that can do more tasks, like validating the values.
For example, we could validate that the account number is the correct length and the sort code is the correct format:
class AccountDetails {
public function __construct(
public readonly string $accountNumber,
public readonly string $sortCode,
) {
// Validate the account number.
if (!preg_match('/^\d{8}$/', $accountNumber)) {
throw new \InvalidArgumentException('Account number must be an 8-digit number');
}
// Validate the sort code.
if (!preg_match('/^\d{2}-\d{2}-\d{2}$/', $sortCode)) {
throw new \InvalidArgumentException('Sort code must be in the format 00-00-00');
}
}
}
Helper methods can also be added to a value object as long as they don't modify the state of the object, such as getting the raw sort code without the separating dashes or performing further checks on the input values.
summary: null field_daily_email_cta: { }