Skip to content

Domain


WARNING

In future versions of Phalcon, this component will be reworked to follow the Payload Interop interface.

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.

NOTE

All the setters return 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.

<?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.

  • 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
  • VALID

These statuses can be used at the display/view layer of your application to process domain objects retrieved via Payload::getOutput().

NOTE

ERROR and FAILURE carry distinct meanings. ERROR means an exception was raised while the domain layer was running; by convention, Payload::setException() pairs with the ERROR status. FAILURE means the domain layer ran to completion but declined the request (for example, a business rule was not satisfied), with no exception raised.

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;
    }
}