Skip to content

Commit 12c2888

Browse files
committed
Improve keyed validator interface and handling.
1 parent ae6c1c5 commit 12c2888

File tree

10 files changed

+318
-170
lines changed

10 files changed

+318
-170
lines changed

src/Contracts/Validator/KeyedValidatorInterface.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,44 @@
2525
interface KeyedValidatorInterface extends ValidatorInterface
2626
{
2727

28+
/**
29+
* @param $key
30+
* @param ValidatorInterface $validator
31+
* @return $this
32+
*/
33+
public function setValidator($key, ValidatorInterface $validator);
34+
35+
/**
36+
* @param $keyOrKeys
37+
* @return $this
38+
*/
39+
public function setRequiredKeys($keyOrKeys);
40+
41+
/**
42+
* @param $keyOrKeys
43+
* @return $this
44+
*/
45+
public function addRequiredKeys($keyOrKeys);
46+
2847
/**
2948
* @param $key
3049
* @return ValidatorInterface
3150
*/
3251
public function getValidator($key);
3352

53+
/**
54+
* @param $key
55+
* @return bool
56+
*/
57+
public function hasValidator($key);
58+
59+
/**
60+
* Get a list of the keys for which there are validators.
61+
*
62+
* @return array
63+
*/
64+
public function keys();
65+
3466
/**
3567
* @param $key
3668
* @return bool
@@ -41,4 +73,21 @@ public function isRequiredKey($key);
4173
* @return bool
4274
*/
4375
public function hasRequiredKeys();
76+
77+
/**
78+
* @param $keyOrKeys
79+
* @return $this
80+
*/
81+
public function setAllowedKeys($keyOrKeys);
82+
83+
/**
84+
* @param $keyOrKeys
85+
* @return $this
86+
*/
87+
public function addAllowedKeys($keyOrKeys);
88+
89+
/**
90+
* @return bool
91+
*/
92+
public function isAllowedKey($key);
4493
}

src/Validator/Attributes/AttributesValidator.php

Lines changed: 6 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -104,36 +104,20 @@ public function getValidator($key)
104104
}
105105

106106
/**
107-
* Helper method to add a type validator for the specified key.
108-
*
109107
* @param $key
110-
* @param string|ValidatorInterface $type
111-
* @param array $options
112-
* @return $this
108+
* @return bool
113109
*/
114-
public function attr($key, $type = null, array $options = [])
110+
public function hasValidator($key)
115111
{
116-
$validator = $this->parseType($type);
117-
118-
if ($validator instanceof ConfigurableInterface) {
119-
$validator->configure($options);
120-
}
121-
122-
$this->setValidator($key, $validator);
123-
124-
return $this;
112+
return isset($this->_validators[$key]);
125113
}
126114

127115
/**
128-
* Only allow keys that have validators.
129-
*
130-
* @return $this
116+
* @return array
131117
*/
132-
public function setRestricted()
118+
public function keys()
133119
{
134-
$this->setAllowedKeys(array_keys($this->_validators));
135-
136-
return $this;
120+
return array_keys($this->_validators);
137121
}
138122

139123
/**
@@ -221,37 +205,4 @@ protected function checkValue($key, $value)
221205
return $this;
222206
}
223207

224-
/**
225-
* @param string|null|ValidatorInterface
226-
* @return ValidatorInterface
227-
*/
228-
protected function parseType($type)
229-
{
230-
if ($type instanceof ValidatorInterface) {
231-
return $type;
232-
} elseif (is_null($type)) {
233-
return new TypeValidator();
234-
} elseif (!is_string($type)) {
235-
throw new \InvalidArgumentException('Expecting a string, ValidatorInterface or null.');
236-
}
237-
238-
if (class_exists($type)) {
239-
$class = $type;
240-
} else {
241-
$class = sprintf('CloudCreativity\JsonApi\Validator\Type\%sValidator', ucfirst($type));
242-
243-
if (!class_exists($class)) {
244-
throw new \InvalidArgumentException(sprintf('Unrecognised attribute type: %s.', $type));
245-
}
246-
}
247-
248-
$validator = new $class();
249-
250-
if (!$validator instanceof ValidatorInterface) {
251-
throw new \InvalidArgumentException(sprintf('Type %s does not resolve to a %s instance.', $type, ValidatorInterface::class));
252-
}
253-
254-
return $validator;
255-
}
256-
257208
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2015 Cloud Creativity Limited
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
namespace CloudCreativity\JsonApi\Validator\Helper;
20+
21+
use CloudCreativity\JsonApi\Contracts\Stdlib\ConfigurableInterface;
22+
use CloudCreativity\JsonApi\Contracts\Validator\KeyedValidatorInterface;
23+
use CloudCreativity\JsonApi\Contracts\Validator\ValidatorInterface;
24+
use CloudCreativity\JsonApi\Validator\Type\TypeValidator;
25+
26+
/**
27+
* Class AttrTrait
28+
* @package CloudCreativity\JsonApi
29+
*/
30+
trait AttributeTrait
31+
{
32+
33+
/**
34+
* @return KeyedValidatorInterface
35+
*/
36+
abstract public function getKeyedAttributes();
37+
38+
/**
39+
* Helper method to add a type validator for the specified key.
40+
*
41+
* @param $key
42+
* @param string|ValidatorInterface $type
43+
* @param array $options
44+
* @return $this
45+
*/
46+
public function attribute($key, $type = null, array $options = [])
47+
{
48+
$attributes = $this->getKeyedAttributes();
49+
$validator = $this->parseType($type);
50+
51+
if ($validator instanceof ConfigurableInterface) {
52+
$validator->configure($options);
53+
}
54+
55+
$attributes->setValidator($key, $validator);
56+
57+
return $this;
58+
}
59+
60+
/**
61+
* Short-hand method for `static::attribute`.
62+
*
63+
* @param $key
64+
* @param string|ValidatorInterface $type
65+
* @param array $options
66+
* @return $this
67+
*/
68+
public function attr($key, $type = null, array $options = [])
69+
{
70+
return $this->attribute($key, $type, $options);
71+
}
72+
73+
74+
/**
75+
* @param string|null|ValidatorInterface
76+
* @return ValidatorInterface
77+
*/
78+
protected function parseType($type)
79+
{
80+
if ($type instanceof ValidatorInterface) {
81+
return $type;
82+
} elseif (is_null($type)) {
83+
return new TypeValidator();
84+
} elseif (!is_string($type)) {
85+
throw new \InvalidArgumentException('Expecting a string, ValidatorInterface or null.');
86+
}
87+
88+
if (class_exists($type)) {
89+
$class = $type;
90+
} else {
91+
$class = sprintf('CloudCreativity\JsonApi\Validator\Type\%sValidator', ucfirst($type));
92+
93+
if (!class_exists($class)) {
94+
throw new \InvalidArgumentException(sprintf('Unrecognised attribute type: %s.', $type));
95+
}
96+
}
97+
98+
$validator = new $class();
99+
100+
if (!$validator instanceof ValidatorInterface) {
101+
throw new \InvalidArgumentException(sprintf('Type %s does not resolve to a %s instance.', $type, ValidatorInterface::class));
102+
}
103+
104+
return $validator;
105+
}
106+
}

src/Validator/Resource/AttributesValidatorTrait.php renamed to src/Validator/Helper/AttributesValidatorTrait.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
* limitations under the License.
1717
*/
1818

19-
namespace CloudCreativity\JsonApi\Validator\Resource;
19+
namespace CloudCreativity\JsonApi\Validator\Helper;
2020

21+
use CloudCreativity\JsonApi\Contracts\Validator\KeyedValidatorInterface;
2122
use CloudCreativity\JsonApi\Contracts\Validator\ValidatorInterface;
2223
use CloudCreativity\JsonApi\Validator\Attributes\AttributesValidator;
2324

@@ -57,16 +58,16 @@ public function getAttributesValidator()
5758
}
5859

5960
/**
60-
* @return AttributesValidator
61+
* @return KeyedValidatorInterface
6162
*/
62-
public function getAttributes()
63+
public function getKeyedAttributes()
6364
{
64-
$validator = $this->getAttributesValidator();
65+
$attributes = $this->getAttributesValidator();
6566

66-
if (!$validator instanceof AttributesValidator) {
67-
throw new \RuntimeException(sprintf('Attributes validator is not a %s instance.', AttributesValidator::class));
67+
if (!$attributes instanceof KeyedValidatorInterface) {
68+
throw new \RuntimeException('%s expects attributes validator to be a %s instance.', static::class, KeyedValidatorInterface::class);
6869
}
6970

70-
return $validator;
71+
return $attributes;
7172
}
7273
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2015 Cloud Creativity Limited
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
namespace CloudCreativity\JsonApi\Validator\Helper;
20+
21+
use CloudCreativity\JsonApi\Contracts\Validator\KeyedValidatorInterface;
22+
use CloudCreativity\JsonApi\Validator\Relationships\BelongsToValidator;
23+
use CloudCreativity\JsonApi\Validator\Relationships\HasManyValidator;
24+
25+
/**
26+
* Class RelationshipTrait
27+
* @package CloudCreativity\JsonApi
28+
*/
29+
trait RelationshipTrait
30+
{
31+
32+
/**
33+
* @return KeyedValidatorInterface
34+
*/
35+
abstract public function getKeyedRelationships();
36+
37+
/**
38+
* Helper method to add a belongs to validator for the specified key.
39+
*
40+
* @param $key
41+
* @param $typeOrTypes
42+
* @param array $options
43+
* @return $this
44+
*/
45+
public function belongsTo($key, $typeOrTypes, array $options = [])
46+
{
47+
$relationships = $this->getKeyedRelationships();
48+
49+
$validator = new BelongsToValidator($typeOrTypes);
50+
$validator->configure($options);
51+
52+
$relationships->setValidator($key, $validator);
53+
54+
return $this;
55+
}
56+
57+
/**
58+
* Helper method to add a has-many validator for the specified key.
59+
*
60+
* @param $key
61+
* @param $typeOrTypes
62+
* @param array $options
63+
* @return $this
64+
*/
65+
public function hasMany($key, $typeOrTypes, array $options = [])
66+
{
67+
$relationships = $this->getKeyedRelationships();
68+
69+
$validator = new HasManyValidator($typeOrTypes);
70+
$validator->configure($options);
71+
72+
$relationships->setValidator($key, $validator);
73+
74+
return $this;
75+
}
76+
}

0 commit comments

Comments
 (0)