Domain


Domain - 图1

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.

  1. <?php
  2. use Phalcon\Domain\Payload;
  3. $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:

  • extras: Extra information
  • input: Input
  • messages: Messages
  • status: Status
  • output: OutputThe component offers getters and setters for the above properties.

All the setters return back a Phalcon\Domain\Payload object, which allows you to chain calls for a more fluent syntax.

Factory

Phalcon\Domain\PayloadFactory is also available, offering an easy way to generate new Payload objects.

  1. <?php
  2. use Phalcon\Domain\PayloadFactory;
  3. $payloadFactory = new PayloadFactory();
  4. $payload = $payloadFactory->newInstance();
  5. ?>

Interfaces

There are three interfaces that you can take advantage of if you wish to extend the object.

  • ReadableInterface: contains only read methods
  • WritableInterface: contains only write methods
  • PayloadInterface: contains both read and write methods

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.

  • ACCEPTED
  • AUTHENTICATED
  • AUTHORIZED
  • CREATED
  • DELETED
  • ERROR
  • FAILURE
  • FOUND
  • NOT_ACCEPTED
  • NOT_AUTHENTICATED
  • NOT_AUTHORIZED
  • NOT_CREATED
  • NOT_DELETED
  • NOT_FOUND
  • NOT_UPDATED
  • NOT_VALID
  • PROCESSING
  • SUCCESS
  • UPDATED
  • VALIDThese statuses can be used at the display/view layer of your application to process domain objects retrieved via Payload::getOutput().

Example

  1. <?php
  2. use Application\Models\Reports;
  3. use Phalcon\Domain\PayloadFactory;
  4. use Phalcon\Domain\Payload\Status;
  5. use Phalcon\Mvc\Controller;
  6. class ReportsController extends Controller
  7. {
  8. public function viewAction(int $reportId)
  9. {
  10. $factory = new PayloadFactory();
  11. $payload = $factory->newInstance();
  12. $report = Reports::find(
  13. [
  14. 'conditions' => 'reportId = :reportId:',
  15. 'bind' => [
  16. 'reportId' => $reportId,
  17. ],
  18. ]
  19. );
  20. if (false === $report) {
  21. $payload
  22. ->setStatus(Status::NOT_FOUND)
  23. ->setInput(func_get_args())
  24. ;
  25. } else {
  26. $payload
  27. ->setStatus(Status::FOUND)
  28. ->setOutput($report)
  29. ;
  30. }
  31. return $payload;
  32. }
  33. }