Skip to content

Commit c8e5e32

Browse files
committed
Improve resource validator interface and bug fixes.
1 parent 68d6a44 commit c8e5e32

File tree

13 files changed

+383
-283
lines changed

13 files changed

+383
-283
lines changed

src/Error/ErrorException.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ class ErrorException extends \RuntimeException implements ErrorsAwareInterface
3939
*/
4040
public function __construct(ErrorInterface $error, \Exception $previous = null)
4141
{
42-
parent::__construct($error->getTitle(), $error->getCode(), $previous);
42+
$code = is_numeric($error->getCode()) ? (int) $error->getCode() : null;
43+
44+
parent::__construct($error->getTitle(), $code, $previous);
4345

4446
$this->_error = $error;
4547
}

src/Object/Document/Document.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,18 @@ class Document extends StandardObject
3636
*
3737
* @return ResourceObject
3838
*/
39-
public function getResource()
39+
public function getResourceObject()
4040
{
4141
return new ResourceObject($this->get(static::DATA));
4242
}
4343

4444
/**
45-
* Get the primary data as a relationship object.
45+
* Get the document as a relationship object.
4646
*
4747
* @return Relationship
4848
*/
4949
public function getRelationship()
5050
{
51-
return new Relationship($this->get(static::DATA));
51+
return new Relationship($this->getProxy());
5252
}
5353
}

src/Validator/Attributes/AttributesValidator.php

Lines changed: 21 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
use CloudCreativity\JsonApi\Error\ErrorObject;
2323
use CloudCreativity\JsonApi\Error\SourceObject;
2424
use CloudCreativity\JsonApi\Validator\AbstractValidator;
25-
use CloudCreativity\JsonApi\Validator\Type\StringValidator;
25+
use CloudCreativity\JsonApi\Validator\Helper\AllowedKeysTrait;
26+
use CloudCreativity\JsonApi\Validator\Helper\RequiredKeysTrait;
2627
use CloudCreativity\JsonApi\Validator\Type\TypeValidator;
2728
use CloudCreativity\JsonApi\Contracts\Validator\ValidatorInterface;
2829

@@ -33,6 +34,9 @@
3334
class AttributesValidator extends AbstractValidator implements ConfigurableInterface
3435
{
3536

37+
use RequiredKeysTrait,
38+
AllowedKeysTrait;
39+
3640
// Config constants
3741
const ALLOWED = 'allowed';
3842
const REQUIRED = 'required';
@@ -66,62 +70,13 @@ class AttributesValidator extends AbstractValidator implements ConfigurableInter
6670
],
6771
];
6872

69-
/**
70-
* @var array|null
71-
* null means any allowed, array means only the supplied keys are allowed.
72-
*/
73-
protected $_allowed;
74-
75-
/**
76-
* @var array|null
77-
*/
78-
protected $_required;
79-
8073
/**
8174
* Validators for use with keys within the attributes.
8275
*
8376
* @var array
8477
*/
8578
protected $_validators = [];
8679

87-
/**
88-
* @param array $keys
89-
* @return $this
90-
*/
91-
public function setAllowed(array $keys)
92-
{
93-
$this->_allowed = $keys;
94-
95-
return $this;
96-
}
97-
98-
/**
99-
* @return bool
100-
*/
101-
public function isAllowed($key)
102-
{
103-
return is_array($this->_allowed) ? in_array($key, $this->_allowed) : true;
104-
}
105-
106-
/**
107-
* @param array $keys
108-
* @return $this
109-
*/
110-
public function setRequired(array $keys)
111-
{
112-
$this->_required = $keys;
113-
114-
return $this;
115-
}
116-
117-
/**
118-
* @return array
119-
*/
120-
public function getRequired()
121-
{
122-
return (array) $this->_required;
123-
}
124-
12580
/**
12681
* @param $key
12782
* @param ValidatorInterface $validator
@@ -179,18 +134,30 @@ public function attr($key, $type = null, array $options = [])
179134
return $this;
180135
}
181136

137+
/**
138+
* Only allow keys that have validators.
139+
*
140+
* @return $this
141+
*/
142+
public function setRestricted()
143+
{
144+
$this->setAllowedKeys(array_keys($this->_validators));
145+
146+
return $this;
147+
}
148+
182149
/**
183150
* @param array $config
184151
* @return $this
185152
*/
186153
public function configure(array $config)
187154
{
188155
if (isset($config[static::ALLOWED]) && is_array($config[static::ALLOWED])) {
189-
$this->setAllowed($config[static::ALLOWED]);
156+
$this->setAllowedKeys($config[static::ALLOWED]);
190157
}
191158

192159
if (isset($config[static::REQUIRED]) && is_array($config[static::REQUIRED])) {
193-
$this->setRequired($config[static::REQUIRED]);
160+
$this->setRequiredKeys($config[static::REQUIRED]);
194161
}
195162

196163
return $this;
@@ -213,7 +180,7 @@ protected function validate($value)
213180
}
214181

215182
// Check that required keys exist.
216-
foreach ($this->getRequired() as $key) {
183+
foreach ($this->getRequiredKeys() as $key) {
217184

218185
if (!isset($value->{$key})) {
219186
$err = $this->error(static::ERROR_REQUIRED_ATTRIBUTE);
@@ -230,7 +197,7 @@ protected function checkKey($key)
230197
{
231198
$pointer = '/' . $key;
232199

233-
if (!$this->isAllowed($key)) {
200+
if (!$this->isAllowedKey($key)) {
234201
$this->error(static::ERROR_UNRECOGNISED_ATTRIBUTE)
235202
->setSource([
236203
SourceObject::POINTER => $pointer,
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
/**
22+
* Class AllowedKeysTrait
23+
* @package CloudCreativity\JsonApi
24+
*/
25+
trait AllowedKeysTrait
26+
{
27+
28+
/**
29+
* @var array|null
30+
* null means any allowed, array means only the contained keys are allowed.
31+
*/
32+
protected $_allowedKeys;
33+
34+
/**
35+
* @param $keyOrKeys
36+
* @return $this
37+
*/
38+
public function setAllowedKeys($keyOrKeys)
39+
{
40+
$this->_allowedKeys = is_array($keyOrKeys) ? $keyOrKeys : [$keyOrKeys];
41+
42+
return $this;
43+
}
44+
45+
/**
46+
* @param $keyOrKeys
47+
* @return $this
48+
*/
49+
public function addAllowedKeys($keyOrKeys)
50+
{
51+
$keys = is_array($keyOrKeys) ? $keyOrKeys : [$keyOrKeys];
52+
53+
if (!is_array($this->_allowedKeys)) {
54+
$this->_allowedKeys = [];
55+
}
56+
57+
$this->_allowedKeys = array_merge($this->_allowedKeys, $keys);
58+
59+
return $this;
60+
}
61+
62+
/**
63+
* @param $key
64+
* @return bool
65+
*/
66+
public function isAllowedKey($key)
67+
{
68+
return is_array($this->_allowedKeys) ? in_array($key, $this->_allowedKeys) : true;
69+
}
70+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
/**
22+
* Class RequiredKeysTrait
23+
* @package CloudCreativity\JsonApi
24+
*/
25+
trait RequiredKeysTrait
26+
{
27+
28+
/**
29+
* @var array|null
30+
*/
31+
protected $_requiredKeys;
32+
33+
/**
34+
* @param $keyOrKeys
35+
* @return $this
36+
*/
37+
public function setRequiredKeys($keyOrKeys)
38+
{
39+
$this->_requiredKeys = is_array($keyOrKeys) ? $keyOrKeys : [$keyOrKeys];
40+
41+
return $this;
42+
}
43+
44+
/**
45+
* @param $keyOrKeys
46+
* @return $this
47+
*/
48+
public function addRequiredKeys($keyOrKeys)
49+
{
50+
$keys = is_array($keyOrKeys) ? $keyOrKeys : [$keyOrKeys];
51+
52+
if (!is_array($this->_requiredKeys)) {
53+
$this->_requiredKeys = [];
54+
}
55+
56+
$this->_requiredKeys = array_merge($this->_requiredKeys, $keys);
57+
58+
return $this;
59+
}
60+
61+
/**
62+
* @return array
63+
*/
64+
public function getRequiredKeys()
65+
{
66+
return (array) $this->_requiredKeys;
67+
}
68+
69+
/**
70+
* @return $this
71+
*/
72+
public function clearRequiredKeys()
73+
{
74+
$this->_requiredKeys = null;
75+
76+
return $this;
77+
}
78+
}

src/Validator/Relationships/RelationshipsValidator.php

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use CloudCreativity\JsonApi\Validator\AbstractValidator;
2222
use CloudCreativity\JsonApi\Contracts\Validator\ValidatorInterface;
2323
use CloudCreativity\JsonApi\Error\ErrorObject;
24+
use CloudCreativity\JsonApi\Validator\Helper\RequiredKeysTrait;
2425

2526
/**
2627
* Class RelationshipsValidator
@@ -29,6 +30,8 @@
2930
class RelationshipsValidator extends AbstractValidator
3031
{
3132

33+
use RequiredKeysTrait;
34+
3235
const ERROR_INVALID_VALUE = 'invalid-value';
3336
const ERROR_UNRECOGNISED_RELATIONSHIP = 'not-recognised';
3437
const ERROR_REQUIRED_RELATIONSHIP = 'required';
@@ -62,30 +65,6 @@ class RelationshipsValidator extends AbstractValidator
6265
*/
6366
protected $_validators = [];
6467

65-
/**
66-
* @var array|null
67-
*/
68-
protected $_required;
69-
70-
/**
71-
* @param array $keys
72-
* @return $this
73-
*/
74-
public function setRequired(array $keys)
75-
{
76-
$this->_required = $keys;
77-
78-
return $this;
79-
}
80-
81-
/**
82-
* @return array
83-
*/
84-
public function getRequired()
85-
{
86-
return (array) $this->_required;
87-
}
88-
8968
/**
9069
* @param ValidatorInterface[] $validators
9170
* @return $this
@@ -146,7 +125,7 @@ public function hasValidator($key)
146125
* @param array $options
147126
* @return $this
148127
*/
149-
public function belongsTo($key, $typeOrTypes = null, array $options = [])
128+
public function belongsTo($key, $typeOrTypes, array $options = [])
150129
{
151130
$validator = new BelongsToValidator($typeOrTypes);
152131
$validator->configure($options);
@@ -164,7 +143,7 @@ public function belongsTo($key, $typeOrTypes = null, array $options = [])
164143
* @param array $options
165144
* @return $this
166145
*/
167-
public function hasMany($key, $typeOrTypes = null, array $options = [])
146+
public function hasMany($key, $typeOrTypes, array $options = [])
168147
{
169148
$validator = new HasManyValidator($typeOrTypes);
170149
$validator->configure($options);
@@ -191,7 +170,7 @@ protected function validate($value)
191170
}
192171

193172
// Check required relationships
194-
foreach ($this->getRequired() as $key) {
173+
foreach ($this->getRequiredKeys() as $key) {
195174

196175
if (!isset($value->{$key})) {
197176
$error = $this->error(static::ERROR_REQUIRED_RELATIONSHIP);

0 commit comments

Comments
 (0)