-
Notifications
You must be signed in to change notification settings - Fork 2
Definition File
The Transfer Object definition is a YML file with name matching pattern *.transfer.yml.
Definition file can include one or more Transfer Object definitions:
- Each root level represents a new Transfer Object name.
- The second level represents property names.
- The third level represents the property type and supporting keys:
typecollectionTypeenumTypedateTimeTypenumberTyperequiredprotectedattributes
Customer:
firstName:
type: string
lastName:
type: stringIn order to use schema please add on the top of definition files the following:
# $schema: https://raw.githubusercontent.com/picamator/transfer-object/main/schema/definition.schema.jsonThe type key supports the following PHP built-in types:
booltruefalseintfloatstringarrayArrayObject
Tip
The type key can include the name of the embeded Transfer Object (without the Transfer suffix).
Warning
Union e.g string|bool and Intersection types e.g. CustomerInterface&Customer, are not supported.
Product:
sku:
type: string
details:
type: DetailsWhere Details is a Transfer Object name.
For the types array and ArrayObject DocBlock syntax supported:
array<bool>array<int,bool>array<string,bool>array<int>array<int,int>array<string,int>array<float>array<int,float>array<string,float>array<string>array<int,string>array<string,string>array<mixed>array<int,mixed>array<string,mixed>array<string,array<int,string>>ArrayObject<bool>ArrayObject<int,bool>ArrayObject<string,bool>ArrayObject<int>ArrayObject<int,int>ArrayObject<string,int>ArrayObject<float>ArrayObject<int,float>ArrayObject<string,float>ArrayObject<string>ArrayObject<int,string>ArrayObject<string,string>ArrayObject<mixed>ArrayObject<int,mixed>ArrayObject<string,mixed>ArrayObject<string,array<int,string>>
Tip
Basically any text between <> is allowed.
The collectionType key should include the Transfer Object name without the Transfer suffix.
Agent:
merchants:
collectionType: MerchantThe enumType key supports only BakedEnum and should include fully-qualified class name.
Merchant:
country:
enumType: "Picamator\\Doc\\Samples\\TransferObject\\Enum\\CountryEnum"The enumType key also supports alias.
Merchant:
country:
enumType: "Picamator\\Doc\\Samples\\TransferObject\\Enum\\CountryEnum as CountryEnumAlias"The dateTimeType key supports DateTimeInterface implementation such us DateTime, DateTimeImmutable, or any of the sub-classes with fully-qualified class name.
Merchant:
createdAt:
dateTimeType: "DateTimeImmutable"
updatedAt:
dateTimeType: "DateTime"The dateTimeType key also supports aliases.
The numberType key supports arbitrary precision mathematic class BcMath\Number.
Product:
price:
numberType: "BcMath\\Number"The required key doesn't have any value. It is marking a property as not null.
By default the following types are not nullable:
ArrayObjectarray- Transfer Object collections
Merchant:
merchantReference:
type: string
required:
isActive:
type: bool
required:The protected key doesn't have any value. It is adding asymmetric protected(set) visibility.
Credentials:
login:
type: string
protected:
token:
type: string
protected:The key attributes is an array where each of element is fully-qualified property attribute class name.
Customer:
email:
type: string
attributes:
- "\\Symfony\\Component\\Validator\\Constraints\\NotBlank"
- "\\Symfony\\Component\\Validator\\Constraints\\Email"For Symfony validation shortcut can be used sf-assert:.
Customer:
email:
type: string
attributes:
- "sf-assert:NotBlank"
- "sf-assert:Email"The basic definition file is built to hold all Transfer Objects in one namespace.
In cases where each application module holds its own Transfer Objects and requires cross-module connections, the type and collectionType fields should contain the full namespace, optionally with a namespace alias.
Let's assume there are two Transfer Objects:
MyShop\Cart\Generated\CartTransferMyShop\Customer\Generated\CustomerTransfer
and the task is to create a new Transfer Object CheckoutTransfer in MyShop\Checkout to hold both CartTransfer and CustomerTransfer.
The MyShop\Checkout should contain the following definition file:
Checkout:
customer:
type: "MyShop\\Customer\\Generated\\CustomerTransfer as CustomerAlias"
cart:
type: "MyShop\\Cart\\Generated\\CartTransfer"Important: The namespace should be in double quotes, containing full Transfer Object name with the Transfer suffix.
Running the Transfer Object generation console commands builds CheckoutTransfer with non-nullable properties: customer and cart.
Using the full namespace enforces the use of non-nullable rules.
The alias CustomerAlias is helpful when there are multiple objects with the same name used in the Transfer Object.
Transfer Object supports connecting custom Data Transfer Objects or third-party DTOs such as Spatie Laravel Data or Spryker Transfer.
To integrate a custom Transfer Object, the steps should be followed:
- The custom Transfer Object should implement
Picamator\TransferObject\Transfer\TransferInterfaceinterface. - Optionally, Transfer Object can use one of the available adapters:
Picamator\TransferObject\Transfer\Adapter\DummyTransferAdapterTraitPicamator\TransferObject\Transfer\Adapter\TransferAdapterTrait
Let's consider there is a custom Data Transfer Object:
class CredentialsData
{
public string $login {
get => $this->login;
set => $this->login = $value;
}
public string $token {
get => $this->token;
set => $this->token = $value;
}
}After implementing the interface and dummy trait, it looks like:
use Picamator\TransferObject\Transfer\Adapter\DummyTransferAdapterTrait;
use Picamator\TransferObject\Transfer\TransferInterface;
class CredentialsData implements TransferInterface
{
use DummyTransferAdapterTrait;
public string $login {
get => $this->login;
set => $this->login = $value;
}
public string $token {
get => $this->token;
set => $this->token = $value;
}
}Finally, the custom Data Transfer Object can be registered in the definition file:
Customer:
credentials:
type: "MyShop\\Customer\\TDO\\CredentialsData"