1 <?php namespace CupOfTea\Chain;
2
3 use CupOfTea\Chain\Contracts\ResultAccess;
4
5 class Results implements ResultAccess
6 {
7
8 /**
9 * Wether or not Results can be added to the Results object
10 *
11 * @protected bool
12 */
13 protected $open = true;
14
15 /**
16 * The Results contained in the Results object
17 *
18 * @protected Array
19 */
20 protected $results = [];
21
22 /**
23 * Add a result to the Results object.
24 *
25 * @param string $result Name of the result
26 * @param mixed $value Value of the result
27 */
28 public function addResult($result, $value)
29 {
30 if (!$this->open) {
31 return;
32 }
33
34 if ($value !== null) {
35 $this->results[$result] = $value;
36 }
37 }
38
39 /**
40 * @inheritdoc
41 */
42 public function getResult($result)
43 {
44 return isset($this->results[$result]) ? $this->results[$result] : null;
45 }
46
47 /**
48 * @inheritdoc
49 */
50 public function getResults()
51 {
52 return $this->results;
53 }
54
55 /**
56 * Close result set, preventing additional results to be added.
57 *
58 * @return CupOfTea\Chain\Results Results object
59 */
60 public function done()
61 {
62 $this->open = false;
63
64 return $this;
65 }
66
67 /**
68 * @inheritdoc
69 */
70 public function toArray()
71 {
72 return $this->getResults();
73 }
74
75 /**
76 * Return the result with the specified name.
77 *
78 * @param string $result Name of the result
79 * @return mixed value of the result
80 */
81 public function __get($result)
82 {
83 return $this->getResult($result);
84 }
85
86 /**
87 * Wether or not a result for the name exists.
88 *
89 * @param string $result Name of the result
90 * @return bool Wether or not a result for the name exists
91 */
92 public function __isset($result)
93 {
94 return isset($this->results[$result]);
95 }
96
97 }
98