Data.php 3.38 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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
<?php
namespace Robo\State;

/**
 * A State\Data object contains a "message" (the primary result) and a
 * data array (the persistent state). The message is transient, and does
 * not move into the persistent state unless explicitly copied there.
 */
class Data extends \ArrayObject
{
    /**
     * @var string
     */
    protected $message;

    /**
     * @param string $message
     * @param array $data
     */
    public function __construct($message = '', $data = [])
    {
        $this->message = $message;
        parent::__construct($data);
    }

    /**
     * @return array
     */
    public function getData()
    {
        return $this->getArrayCopy();
    }

    /**
     * @return string
     */
    public function getMessage()
    {
        return $this->message;
    }

    /**
     * @param string $message
     */
    public function setMessage($message)
    {
        $this->message = $message;
    }

    /**
     * Merge another result into this result.  Data already
     * existing in this result takes precedence over the
     * data in the Result being merged.
     *
     * @param \Robo\State\Data $result
     *
     * @return $this
     */
    public function merge(Data $result)
    {
        $mergedData = $this->getArrayCopy() + $result->getArrayCopy();
        $this->exchangeArray($mergedData);
        return $this;
    }

    /**
     * Update the current data with the data provided in the parameter.
     * Provided data takes precedence.
     *
     * @param \ArrayObject $update
     *
     * @return $this
     */
    public function update(\ArrayObject $update)
    {
        $iterator = $update->getIterator();

        while ($iterator->valid()) {
            $this[$iterator->key()] = $iterator->current();
            $iterator->next();
        }

        return $this;
    }

    /**
     * Merge another result into this result.  Data already
     * existing in this result takes precedence over the
     * data in the Result being merged.
     *
     * $data['message'] is handled specially, and is appended
     * to $this->message if set.
     *
     * @param array $data
     *
     * @return array
     */
    public function mergeData(array $data)
    {
        $mergedData = $this->getArrayCopy() + $data;
        $this->exchangeArray($mergedData);
        return $mergedData;
    }

    /**
     * @return bool
     */
    public function hasExecutionTime()
    {
        return isset($this['time']);
    }

    /**
     * @return null|float
     */
    public function getExecutionTime()
    {
        if (!$this->hasExecutionTime()) {
            return null;
        }
        return $this['time'];
    }

    /**
     * Accumulate execution time
     *
     * @param array|float $duration
     *
     * @return null|float
     */
    public function accumulateExecutionTime($duration)
    {
        // Convert data arrays to scalar
        if (is_array($duration)) {
            $duration = isset($duration['time']) ? $duration['time'] : 0;
        }
        $this['time'] = $this->getExecutionTime() + $duration;
        return $this->getExecutionTime();
    }

    /**
     * Accumulate the message.
     *
     * @param string $message
     *
     * @return string
     */
    public function accumulateMessage($message)
    {
        if (!empty($this->message)) {
            $this->message .= "\n";
        }
        $this->message .= $message;
        return $this->getMessage();
    }
}