The domain component incorporates components that are used for the implementation of the Action Domain Responder (ADR) pattern and can also be used when implementing Domain Driven Design.
Payload
The Action Domain Responder requires a data transfer mechanism between the three layers to serve your application. The Phalcon\Domain\Payload is a data transfer object that is used to send data between the three layers of the pattern.
<?php
use Phalcon\Domain\Payload;
$payload = new Payload();When using this object, you can set its status, the input, the output, any messages, or extra information required by each layer of your pattern to be transferred to the next layer that requires it during the application flow. The class itself is a data wrapper that contains the necessary information to be passed between layers.
The properties stored are:
| Property | Description |
|---|---|
extras |
Extra information |
input |
Input |
messages |
Messages |
status |
Status |
output |
Output |
The component offers getters and setters for the above properties.
Factory
Phalcon\Domain\PayloadFactory is also available, offering a way to generate new Payload objects.
<?php
use Phalcon\Domain\PayloadFactory;
$payloadFactory = new PayloadFactory();
$payload = $payloadFactory->newInstance();
?>Interfaces
There are three interfaces that you can take advantage of if you wish to extend the object.
| Interface | Description |
|---|---|
ReadableInterface |
contains only read methods |
WriteableInterface |
contains only write methods |
PayloadInterface |
contains both read and write methods |
The two single-responsibility interfaces describe a narrowing convention for the Action Domain Responder flow:
- The domain layer builds the payload through
WriteableInterface(the setters). - The responder consumes the finished payload through
ReadableInterface(the getters).
PayloadInterface extends both and exposes the full surface. Type-hinting against the narrower interface at each boundary keeps each side to the capability it needs.
Contracts
The three interfaces above extend canonical contracts in the Phalcon\Contracts\Domain\Payload namespace. New code should type-hint the contracts. The *Interface types remain as deprecated aliases and will be removed in a future major version.
| Deprecated interface | Contract |
|---|---|
Phalcon\Domain\Payload\ReadableInterface |
Phalcon\Contracts\Domain\Payload\Readable |
Phalcon\Domain\Payload\WriteableInterface |
Phalcon\Contracts\Domain\Payload\Writeable |
Phalcon\Domain\Payload\PayloadInterface |
Phalcon\Contracts\Domain\Payload\Payload |
Status Values
The Phalcon\Domain\Payload\Status class contains several constants to help with the domain status of your Payload objects. You can always extend the class and introduce your own domain statuses, depending on the needs of your application.
ACCEPTEDAUTHENTICATEDAUTHORIZEDCREATEDDELETEDERRORFAILUREFOUNDNOT_ACCEPTEDNOT_AUTHENTICATEDNOT_AUTHORIZEDNOT_CREATEDNOT_DELETEDNOT_FOUNDNOT_UPDATEDNOT_VALIDPROCESSINGSUCCESSUPDATEDVALID
These statuses can be used at the display/view layer of your application to process domain objects retrieved via Payload::getOutput().
Example
<?php
use Application\Models\Reports;
use Phalcon\Domain\PayloadFactory;
use Phalcon\Domain\Payload\Status;
use Phalcon\Mvc\Controller;
class ReportsController extends Controller
{
public function viewAction(int $reportId)
{
$factory = new PayloadFactory();
$payload = $factory->newInstance();
$report = Reports::find(
[
'conditions' => 'reportId = :reportId:',
'bind' => [
'reportId' => $reportId,
],
]
);
if (false === $report) {
$payload
->setStatus(Status::NOT_FOUND)
->setInput(func_get_args())
;
} else {
$payload
->setStatus(Status::FOUND)
->setOutput($report)
;
}
return $payload;
}
}