CommandResult.php 1.72 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
<?php
namespace Consolidation\AnnotatedCommand;

/**
 * Return a CommandResult as the result of a command to pass both an exit
 * code and result data from a command.
 *
 * Usage:
 *
 *      return CommandResult::dataWithExitCode(new RowsOfFields($rows), 1);
 *
 * The CommandResult can also be used to unambiguously return just
 * an exit code or just output data.
 *
 * Exit code only:
 *
 *      return CommandResult::dataWithExitCode(1);
 *
 * Data only:
 *
 *      return CommandResult::data(new RowsOfFields($rows));
 *
 * Historically, it has always been possible to return an integer to indicate
 * that the result is an exit code, and other return types (typically array
 * / ArrayObjects) indicating actual data with an implicit exit code of 0.
 * Using a CommandResult is preferred, though, as it allows the result of the
 * function to be unambiguously specified without type-based interpretation.
 *
 * @package Consolidation\AnnotatedCommand
 */
class CommandResult implements ExitCodeInterface, OutputDataInterface
{
    protected $data;
    protected $exitCode;

    protected function __construct($data = null, $exitCode = 0)
    {
        $this->data = $data;
        $this->exitCode = $exitCode;
    }

    public static function exitCode($exitCode)
    {
        return new self(null, $exitCode);
    }

    public static function data($data)
    {
        return new self($data);
    }

    public static function dataWithExitCode($data, $exitCode)
    {
        return new self($data, $exitCode);
    }

    public function getExitCode()
    {
        return $this->exitCode;
    }

    public function getOutputData()
    {
        return $this->data;
    }

    public function setOutputData($data)
    {
        $this->data = $data;
    }
}