dev-master
9999999-dev https://github.com/JoeBengalen/PayloadDomain payload library
MIT
The Requires
- php >=5.4
The Development Requires
by Martijn Wennink
domain payload
Wallogit.com
2017 © Pedro Peláez
Domain payload library
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., (*2)
Via Composer, (*3)
bash
$ composer require joebengalen/payload, (*4)
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., (*5)
The Payload status can be set and read with the following methods:, (*6)
setStatus($status)getStatus()The following Payload statuses are available., (*7)
PayloadStatus::FOUND Requested data foundPayloadStatus::NOT_FOUND Requested data not foundPayloadStatus::SUCCESS Action succeededPayloadStatus::INVALID Invalid inputPayloadStatus::ERROR Error during executionWARNING: Do not rely on the constant values as they may change over time! Always use the constants by their name., (*8)
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., (*9)
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 errorApart 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., (*10)
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., (*11)
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., (*12)
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., (*13)
Fetch methods can return the following payload statuses
- PayloadStatus::FOUND
- PayloadStatus::NOT_FOUND
- PayloadStatus::INVALID
- PayloadStatus::ERROR, (*14)
Command methods can return the following payload statuses
- PayloadStatus::SUCCESS
- PayloadStatus::INVALID
- PayloadStatus::ERROR, (*15)
For each payload status the following input/output data should be provided, (*16)
PayloadStatus::FOUND, (*17)
null or named array of fetch parameters (if any)PayloadStatus::NOT_FOUND, (*18)
null
PayloadStatus::SUCCESS, (*19)
null
PayloadStatus::INVALID, (*20)
PayloadStatus::ERROR, (*21)
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., (*22)
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., (*23)
Examples: - Could not find user - User was successfully updated - Message successfully marked as read - Invalid comment data provided - Could not add project, (*24)
Please see the CHANGELOG for more information what has changed recently., (*25)
This project uses PHPSpec for testing., (*26)
Domain payload library
MIT
domain payload