1: <?php
2:
3: /**
4: * Copyright 2015, Eduardo Trujillo
5: *
6: * For the full copyright and license information, please view the LICENSE
7: * file that was distributed with this source code.
8: *
9: * This file is part of the Nucleus package
10: */
11:
12: namespace Chromabits\Nucleus\Meditation;
13:
14: use Chromabits\Nucleus\Foundation\BaseObject;
15: use Chromabits\Nucleus\Meditation\Constraints\AbstractConstraint;
16: use Chromabits\Nucleus\Meditation\Interfaces\CheckResultInterface;
17: use Chromabits\Nucleus\Support\Arr;
18:
19: /**
20: * Class SpecResult.
21: *
22: * @author Eduardo Trujillo <ed@chromabits.com>
23: * @package Chromabits\Nucleus\Meditation
24: */
25: class SpecResult extends BaseObject implements CheckResultInterface
26: {
27: const STATUS_PASS = 'pass';
28: const STATUS_FAIL = 'fail';
29:
30: /**
31: * List of field that were missing from the input.
32: *
33: * @var string[]
34: */
35: protected $missing;
36:
37: /**
38: * List of constraints that failed per field.
39: *
40: * @var array[]
41: */
42: protected $failed;
43:
44: /**
45: * Whether the spec check passed or not.
46: *
47: * @var string
48: */
49: protected $status;
50:
51: /**
52: * Construct an instance of a SpecResult.
53: *
54: * @param string[] $missing
55: * @param array[] $failed
56: * @param string $status
57: */
58: public function __construct($missing = [], $failed = [], $status = 'fail')
59: {
60: parent::__construct();
61:
62: $this->missing = $missing;
63: $this->failed = $failed;
64: $this->status = $status;
65: }
66:
67: /**
68: * Get missing fields.
69: *
70: * @return string[]
71: */
72: public function getMissing()
73: {
74: return $this->missing;
75: }
76:
77: /**
78: * Get failed constrains for every field.
79: *
80: * @return array[]
81: */
82: public function getFailed()
83: {
84: return $this->failed;
85: }
86:
87: /**
88: * Get the failed constrains for a specific field.
89: *
90: * Dot notation is supported.
91: *
92: * @param string $fieldName
93: *
94: * @return AbstractConstraint[]
95: */
96: public function getFailedForField($fieldName)
97: {
98: return Arr::dotGet($this->failed, $fieldName);
99: }
100:
101: /**
102: * Get the status of the result.
103: *
104: * @return string
105: */
106: public function getStatus()
107: {
108: return $this->status;
109: }
110:
111: /**
112: * Return true if the check passed.
113: *
114: * @return bool
115: */
116: public function passed()
117: {
118: return $this->status === static::STATUS_PASS;
119: }
120:
121: /**
122: * Return false if the check failed.
123: *
124: * @return bool
125: */
126: public function failed()
127: {
128: return $this->status === static::STATUS_FAIL;
129: }
130: }
131: