Skip to content

Commit 4571246

Browse files
committed
Additions to attr method.
Allow attribute validator to be set using a class string or a `ValidatorInterface` object in addition to the shorthand string.
1 parent cf7226a commit 4571246

File tree

3 files changed

+61
-18
lines changed

3 files changed

+61
-18
lines changed

src/Validator/Attributes/AttributesValidator.php

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
namespace CloudCreativity\JsonApi\Validator\Attributes;
2020

2121
use CloudCreativity\JsonApi\Contracts\Stdlib\ConfigurableInterface;
22+
use CloudCreativity\JsonApi\Contracts\Validator\ValidatorInterface;
2223
use CloudCreativity\JsonApi\Error\ErrorObject;
2324
use CloudCreativity\JsonApi\Error\SourceObject;
2425
use CloudCreativity\JsonApi\Validator\AbstractValidator;
2526
use CloudCreativity\JsonApi\Validator\Helper\AllowedKeysTrait;
2627
use CloudCreativity\JsonApi\Validator\Helper\RequiredKeysTrait;
2728
use CloudCreativity\JsonApi\Validator\Type\TypeValidator;
28-
use CloudCreativity\JsonApi\Contracts\Validator\ValidatorInterface;
2929

3030
/**
3131
* Class AttributesValidator
@@ -106,24 +106,13 @@ public function getValidator($key)
106106
* Helper method to add a type validator for the specified key.
107107
*
108108
* @param $key
109-
* @param null $type
109+
* @param string|ValidatorInterface $type
110110
* @param array $options
111111
* @return $this
112112
*/
113113
public function attr($key, $type = null, array $options = [])
114114
{
115-
if (is_null($type)) {
116-
$type = 'type';
117-
}
118-
119-
$class = sprintf('CloudCreativity\JsonApi\Validator\Type\%sValidator', ucfirst($type));
120-
121-
if (!class_exists($class)) {
122-
throw new \InvalidArgumentException(sprintf('Unrecognised attribute type: %s.', $type));
123-
}
124-
125-
/** @var ValidatorInterface $validator */
126-
$validator = new $class();
115+
$validator = $this->parseType($type);
127116

128117
if ($validator instanceof ConfigurableInterface) {
129118
$validator->configure($options);
@@ -231,4 +220,37 @@ protected function checkValue($key, $value)
231220
return $this;
232221
}
233222

223+
/**
224+
* @param string|null|ValidatorInterface
225+
* @return ValidatorInterface
226+
*/
227+
protected function parseType($type)
228+
{
229+
if ($type instanceof ValidatorInterface) {
230+
return $type;
231+
} elseif (is_null($type)) {
232+
return new TypeValidator();
233+
} elseif (!is_string($type)) {
234+
throw new \InvalidArgumentException('Expecting a string, ValidatorInterface or null.');
235+
}
236+
237+
if (class_exists($type)) {
238+
$class = $type;
239+
} else {
240+
$class = sprintf('CloudCreativity\JsonApi\Validator\Type\%sValidator', ucfirst($type));
241+
242+
if (!class_exists($class)) {
243+
throw new \InvalidArgumentException(sprintf('Unrecognised attribute type: %s.', $type));
244+
}
245+
}
246+
247+
$validator = new $class();
248+
249+
if (!$validator instanceof ValidatorInterface) {
250+
throw new \InvalidArgumentException(sprintf('Type %s does not resolve to a %s instance.', $type, ValidatorInterface::class));
251+
}
252+
253+
return $validator;
254+
}
255+
234256
}

src/Validator/Resource/ResourceObjectValidator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function id($id)
8080
* Set an attribute of the resource object.
8181
*
8282
* @param $key
83-
* @param null $type
83+
* @param mixed $type
8484
* @param array $options
8585
* @return $this
8686
*/
@@ -204,4 +204,4 @@ public function getRelated($key)
204204
{
205205
return $this->getRelationships()->getValidator($key);
206206
}
207-
}
207+
}

test/Validator/Resource/ResourceObjectValidatorTest.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
namespace CloudCreativity\JsonApi\Validator\Resource;
2020

21+
use CloudCreativity\JsonApi\Contracts\Validator\ValidatorInterface;
2122
use CloudCreativity\JsonApi\Validator\Relationships\BelongsToValidator;
2223
use CloudCreativity\JsonApi\Validator\Relationships\HasManyValidator;
2324
use CloudCreativity\JsonApi\Validator\ResourceIdentifier\ExpectedIdValidator;
@@ -46,7 +47,7 @@ public function testId()
4647
$this->assertSame($validator, $validator->id($id));
4748
$this->assertEquals($expected, $validator->getIdValidator());
4849
}
49-
50+
5051
public function testAttr()
5152
{
5253
$key = 'foo';
@@ -63,6 +64,26 @@ public function testAttr()
6364
$this->assertEquals($expected, $validator->getAttributes()->getValidator($key));
6465
}
6566

67+
public function testAttrWithClass()
68+
{
69+
$key = 'foo';
70+
$expected = new StringValidator();
71+
$validator = new ResourceObjectValidator();
72+
73+
$validator->attr($key, get_class($expected));
74+
$this->assertEquals($expected, $validator->getAttributes()->getValidator($key));
75+
}
76+
77+
public function testAttrWithValidator()
78+
{
79+
$key = 'foo';
80+
$mock = $this->getMock(ValidatorInterface::class);
81+
$validator = new ResourceObjectValidator();
82+
83+
$validator->attr($key, $mock);
84+
$this->assertSame($mock, $validator->getAttributes()->getValidator($key));
85+
}
86+
6687
public function testBelongsTo()
6788
{
6889
$key = 'foo';
@@ -164,4 +185,4 @@ public function testAcceptsEmptyAttributesAndRelationships()
164185

165186
$this->assertTrue($validator->isValid($input));
166187
}
167-
}
188+
}

0 commit comments

Comments
 (0)