Payload is a data transfer object between the domain layer and the application layer. The idea is that for every method you call on the domain service/access layer (domain API), you will receive a Payload instance as answer.
Via Composer
$ composer require joebengalen/payload
There are two types of methods in a domain access layer: First you have fetch methods, which asks for data. Second you have command methods, which tell the domain to perform a certain action. The Payload status will tell you if the method was successful or not. Apart from the status the Payload may contain additional information, such as the requested data or an exception if something went wrong.
The Payload status can be set and read with the following methods:
setStatus($status)
getStatus()
The following Payload statuses are available.
PayloadStatus::FOUND
Requested data foundPayloadStatus::NOT_FOUND
Requested data not foundPayloadStatus::SUCCESS
Action succeededPayloadStatus::INVALID
Invalid inputPayloadStatus::ERROR
Error during execution
WARNING: Do not rely on the constant values as they may change over time! Always use the constants by their name.
To make the creation of the Payload objects easier there is a PayloadFactory
available. This factory contains methods to create payload objects with every
available status or one without a status.
createPayload()
Create a new Payload objectcreateSuccessPayload()
Create a new Payload object with status successcreateFoundPayload()
Create a new Payload object with status foundcreateNotFoundPayload()
Create a new Payload object with status not foundcreateInvalidPayload()
Create a new Payload object with status invalidcreateErrorPayload()
Create a new Payload object with status error
Apart from the status the Payload can contain a message, the input and the output of the domain method. To manage that data the following methods are available in the Payload class.
setMessage(string $message)
getMessage()
setInput(mixed $input)
getInput()
setOutput(mixed $input)
getOutput()
Payload objects should only be created and modified within the domain. If the
domain method has PayloadInterface
as return type, only the getter methods
will be visible/available to the application.
Of course you could still modify the payload by methods outside of the interface, but it will at least show you should not use the setter methods.
Explanation of how I intended the Payload objects to be used. You can make up your own set of rules, but it is recommended to use the same rules throughout the whole domain.
Fetch methods can return the following payload statuses
PayloadStatus::FOUND
PayloadStatus::NOT_FOUND
PayloadStatus::INVALID
PayloadStatus::ERROR
Command methods can return the following payload statuses
PayloadStatus::SUCCESS
PayloadStatus::INVALID
PayloadStatus::ERROR
For each payload status the following input/output data should be provided
-
PayloadStatus::FOUND
- input:
null
or named array of fetch parameters (if any) - output: found entity or collection
- input:
-
PayloadStatus::NOT_FOUND
- input: named array of fetch parameters (if any)
- output:
null
-
PayloadStatus::SUCCESS
- input:
null
- output: created/updated/deleted entity or other relevant data if available
- input:
-
PayloadStatus::INVALID
- input: named array of the parameters
- output: list of error messages
-
PayloadStatus::ERROR
- input: named array of the parameters
- output: thrown exception
This way you will always have the information you may need. If anything goes wrong (not found, invalid or error status) you will have the input parameter to see why. Optionally you may choose to always include the input argument in the payload.
Payload messages are optional. They may contain a general message about the status. Such a message could be handy to provide the user with a friendly message in the application. I would recommend to at least be consistent throughout the whole domain whether you return a message or not.
Examples:
- Could not find user
- User was successfully updated
- Message successfully marked as read
- Invalid comment data provided
- Could not add project
Please see the CHANGELOG for more information what has changed recently.
This project uses PHPSpec for testing.