Skip to content

Commit af32c06

Browse files
committed
Put required back in, but with different logic.
1 parent 74bf9b8 commit af32c06

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

src/Validator/Resource/ResourceObjectValidator.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,28 @@ public function allowed($keyOrKeys)
108108
return $this;
109109
}
110110

111+
/**
112+
* Helper method to set attribute and/or relationship key validators as required.
113+
*
114+
* @param $keyOrKeys
115+
* @return $this
116+
*/
117+
public function required($keyOrKeys)
118+
{
119+
$keys = is_array($keyOrKeys) ? $keyOrKeys : [$keyOrKeys];
120+
121+
foreach ($keys as $key) {
122+
123+
$validator = $this->getKeyValidator($key);
124+
125+
if (method_exists($validator, 'setRequired')) {
126+
$validator->setRequired(true);
127+
}
128+
}
129+
130+
return $this;
131+
}
132+
111133
/**
112134
* Set attributes and/or relationships to only accept keys for which there are validators.
113135
*
@@ -132,6 +154,27 @@ public function restrict($attributes = true, $relationships = true)
132154
return $this;
133155
}
134156

157+
/**
158+
* @param $key
159+
* @return ValidatorInterface
160+
*/
161+
public function getKeyValidator($key)
162+
{
163+
$attributes = $this->getKeyedAttributes();
164+
165+
if ($attributes->hasValidator($key)) {
166+
return $attributes->getValidator($key);
167+
}
168+
169+
$relationships = $this->getKeyedRelationships();
170+
171+
if ($relationships->hasValidator($key)) {
172+
return $relationships->getValidator($key);
173+
}
174+
175+
throw new \OutOfBoundsException(sprintf('Key "%s" does not exist as a validator on attributes or relationships.', $key));
176+
}
177+
135178
/**
136179
* @return ExpectedTypeValidator
137180
*/

0 commit comments

Comments
 (0)