1: <?php
2:
3: namespace Racoon\Api\Schema;
4:
5:
6: use Racoon\Api\Exception\InvalidArgumentException;
7: use TomWright\Validator\Constraint\ConstraintGroup;
8: use TomWright\Validator\Validator;
9:
10: class Item
11: {
12:
13: 14: 15:
16: protected $propertyName;
17:
18: 19: 20:
21: protected $propertyValue;
22:
23: 24: 25:
26: protected $readableName;
27:
28: 29: 30:
31: protected $validator;
32:
33: 34: 35:
36: protected $required;
37:
38: 39: 40:
41: protected $optionalConstraintGroup;
42:
43:
44: 45: 46: 47: 48: 49: 50: 51:
52: public function __construct($propertyName, $readableName, array $constraintGroups = [], $required = true, ConstraintGroup $optionalConstraintGroup = null)
53: {
54: $this->validator = new Validator(null, null);
55: $this->setPropertyName($propertyName);
56: $this->setReadableName($readableName);
57: $this->setRequired($required);
58: if (is_array($constraintGroups) && count($constraintGroups) > 0) {
59: foreach ($constraintGroups as $constraintGroup) {
60: $this->validator->addConstraintGroup($constraintGroup);
61: }
62: }
63: $this->setOptionalConstraintGroup($optionalConstraintGroup);
64: }
65:
66:
67: 68: 69: 70: 71: 72: 73: 74:
75: public static function create($propertyName, $readableName, array $constraintGroups = [], $required = true, ConstraintGroup $optionalConstraintGroup = null)
76: {
77: $item = new static($propertyName, $readableName, $constraintGroups, $required, $optionalConstraintGroup);
78: return $item;
79: }
80:
81:
82: 83: 84: 85: 86: 87:
88: public function validate($request)
89: {
90: $valueExists = (is_object($request) && property_exists($request, $this->getPropertyName()));
91: if ($this->isRequired() && ! $valueExists) {
92: throw new InvalidArgumentException(null, "Missing required field: {$this->getPropertyName()}");
93: } elseif (! $this->isRequired()) {
94: if (! $valueExists) {
95: $this->validator->addConstraintGroup(
96: ConstraintGroup::create([
97: NullConstraint::create(),
98: ])
99: );
100: } elseif (is_object($this->getOptionalConstraintGroup())) {
101: $this->validator->addConstraintGroup($this->getOptionalConstraintGroup());
102: }
103: }
104:
105: $value = null;
106: if ($valueExists) {
107: $value = $request->{$this->getPropertyName()};
108: }
109: $this->setPropertyValue($value);
110:
111: return $this->validator->validate()->hasPassed();
112: }
113:
114:
115: 116: 117:
118: public function getPropertyName()
119: {
120: return $this->propertyName;
121: }
122:
123:
124: 125: 126: 127:
128: public function setPropertyName($propertyName)
129: {
130: $this->propertyName = $propertyName;
131: return $this;
132: }
133:
134:
135: 136: 137:
138: public function getPropertyValue()
139: {
140: return $this->propertyValue;
141: }
142:
143:
144: 145: 146: 147:
148: public function setPropertyValue($propertyValue)
149: {
150: $this->propertyValue = $propertyValue;
151: $this->validator->setValue($propertyValue);
152: return $this;
153: }
154:
155:
156: 157: 158:
159: public function getReadableName()
160: {
161: return $this->readableName;
162: }
163:
164:
165: 166: 167: 168:
169: public function setReadableName($readableName)
170: {
171: $this->readableName = $readableName;
172: $this->validator->setName($readableName);
173: return $this;
174: }
175:
176:
177: 178: 179:
180: public function getValidator()
181: {
182: return $this->validator;
183: }
184:
185:
186: 187: 188:
189: public function getRequirements()
190: {
191: return $this->getValidator()->getRequirementsString();
192: }
193:
194:
195: 196: 197:
198: public function isRequired()
199: {
200: return $this->required;
201: }
202:
203:
204: 205: 206:
207: public function setRequired($required)
208: {
209: $this->required = $required;
210: }
211:
212:
213: 214: 215:
216: public function getOptionalConstraintGroup()
217: {
218: return $this->optionalConstraintGroup;
219: }
220:
221:
222: 223: 224:
225: public function setOptionalConstraintGroup($optionalConstraintGroup)
226: {
227: $this->optionalConstraintGroup = $optionalConstraintGroup;
228: }
229:
230: }