Skip to content

Commit 6250911

Browse files
committed
Required keys sets expectations for input, fixes #3
1 parent 7ec57cb commit 6250911

File tree

10 files changed

+121
-72
lines changed

10 files changed

+121
-72
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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\Contracts\Validator;
20+
21+
/**
22+
* Interface KeyedValidatorInterface
23+
* @package CloudCreativity\JsonApi
24+
*/
25+
interface KeyedValidatorInterface extends ValidatorInterface
26+
{
27+
28+
/**
29+
* @param $key
30+
* @return ValidatorInterface
31+
*/
32+
public function getValidator($key);
33+
34+
/**
35+
* @param $key
36+
* @return bool
37+
*/
38+
public function isRequiredKey($key);
39+
40+
/**
41+
* @return bool
42+
*/
43+
public function hasRequiredKeys();
44+
}

src/Validator/Attributes/AttributesValidator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
namespace CloudCreativity\JsonApi\Validator\Attributes;
2020

2121
use CloudCreativity\JsonApi\Contracts\Stdlib\ConfigurableInterface;
22+
use CloudCreativity\JsonApi\Contracts\Validator\KeyedValidatorInterface;
2223
use CloudCreativity\JsonApi\Contracts\Validator\ValidatorInterface;
2324
use CloudCreativity\JsonApi\Error\ErrorObject;
2425
use CloudCreativity\JsonApi\Error\SourceObject;
@@ -31,7 +32,7 @@
3132
* Class AttributesValidator
3233
* @package CloudCreativity\JsonApi
3334
*/
34-
class AttributesValidator extends AbstractValidator implements ConfigurableInterface
35+
class AttributesValidator extends AbstractValidator implements ConfigurableInterface, KeyedValidatorInterface
3536
{
3637

3738
use RequiredKeysTrait,

src/Validator/Helper/RequiredKeysTrait.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,23 @@ public function getRequiredKeys()
6666
return (array) $this->_requiredKeys;
6767
}
6868

69+
/**
70+
* @return bool
71+
*/
72+
public function hasRequiredKeys()
73+
{
74+
return !empty($this->_requiredKeys);
75+
}
76+
77+
/**
78+
* @param $key
79+
* @return bool
80+
*/
81+
public function isRequiredKey($key)
82+
{
83+
return in_array($key, $this->_requiredKeys, true);
84+
}
85+
6986
/**
7087
* @return $this
7188
*/

src/Validator/Relationships/RelationshipsValidator.php

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

1919
namespace CloudCreativity\JsonApi\Validator\Relationships;
2020

21+
use CloudCreativity\JsonApi\Contracts\Validator\KeyedValidatorInterface;
2122
use CloudCreativity\JsonApi\Validator\AbstractValidator;
2223
use CloudCreativity\JsonApi\Contracts\Validator\ValidatorInterface;
2324
use CloudCreativity\JsonApi\Error\ErrorObject;
@@ -27,7 +28,7 @@
2728
* Class RelationshipsValidator
2829
* @package CloudCreativity\JsonApi
2930
*/
30-
class RelationshipsValidator extends AbstractValidator
31+
class RelationshipsValidator extends AbstractValidator implements KeyedValidatorInterface
3132
{
3233

3334
use RequiredKeysTrait;

src/Validator/Resource/AbstractResourceObjectValidator.php

Lines changed: 32 additions & 17 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\KeyedValidatorInterface;
2122
use CloudCreativity\JsonApi\Contracts\Validator\ValidatorInterface;
2223
use CloudCreativity\JsonApi\Error\ErrorObject;
2324
use CloudCreativity\JsonApi\Object\Resource\ResourceObject;
@@ -100,39 +101,49 @@ abstract public function getTypeValidator();
100101
abstract public function getIdValidator();
101102

102103
/**
103-
* Whether attributes must always be present in the resource object.
104+
* Get the attributes validator or null if the resource object must not have attributes.
104105
*
105-
* @return bool
106+
* @return ValidatorInterface
106107
*/
107-
abstract public function isExpectingAttributes();
108+
abstract public function getAttributesValidator();
108109

109110
/**
110-
* Get the attributes validator or null if the resource object must not have attributes.
111+
* Get the relationships validator or null if the resource object must not have relationships.
111112
*
112113
* @return ValidatorInterface
113114
*/
114-
abstract public function getAttributesValidator();
115+
abstract public function getRelationshipsValidator();
115116

116117
/**
117-
* Whether relationships must always be present in the resource object.
118-
*
119118
* @return bool
120119
*/
121-
abstract public function isExpectingRelationships();
120+
public function hasIdValidator()
121+
{
122+
return $this->getIdValidator() instanceof ValidatorInterface;
123+
}
122124

123125
/**
124-
* Get the relationships validator or null if the resource object must not have relationships.
126+
* Whether the resource object expects to have attributes.
125127
*
126-
* @return ValidatorInterface
128+
* @return bool
127129
*/
128-
abstract public function getRelationshipsValidator();
130+
public function isExpectingAttributes()
131+
{
132+
$attr = $this->getAttributesValidator();
133+
134+
return ($attr instanceof KeyedValidatorInterface && $attr->hasRequiredKeys());
135+
}
129136

130137
/**
138+
* Whether the resource object expects to have relationships.
139+
*
131140
* @return bool
132141
*/
133-
public function hasIdValidator()
142+
public function isExpectingRelationships()
134143
{
135-
return $this->getIdValidator() instanceof ValidatorInterface;
144+
$rel = $this->getRelationshipsValidator();
145+
146+
return ($rel instanceof KeyedValidatorInterface && $rel->hasRequiredKeys());
136147
}
137148

138149
/**
@@ -213,12 +224,14 @@ protected function validateId(StandardObject $object)
213224
*/
214225
protected function validateAttributes(StandardObject $object)
215226
{
227+
$expectation = $this->isExpectingAttributes();
228+
216229
// valid if the object does not have attributes, and attributes are not expected.
217-
if (!$object->has(ResourceObject::ATTRIBUTES) && !$this->isExpectingAttributes()) {
230+
if (!$object->has(ResourceObject::ATTRIBUTES) && !$expectation) {
218231
return $this;
219232
}
220233

221-
if (!$object->has(ResourceObject::ATTRIBUTES) && $this->isExpectingAttributes()) {
234+
if (!$object->has(ResourceObject::ATTRIBUTES) && $expectation) {
222235
$this->error(static::ERROR_MISSING_ATTRIBUTES);
223236
return $this;
224237
}
@@ -243,12 +256,14 @@ protected function validateAttributes(StandardObject $object)
243256
*/
244257
protected function validateRelationships(StandardObject $object)
245258
{
259+
$expectation = $this->isExpectingRelationships();
260+
246261
// valid if no relationships and not expecting relationships
247-
if (!$object->has(ResourceObject::RELATIONSHIPS) && !$this->isExpectingRelationships()) {
262+
if (!$object->has(ResourceObject::RELATIONSHIPS) && !$expectation) {
248263
return $this;
249264
}
250265

251-
if (!$object->has(ResourceObject::RELATIONSHIPS) && $this->isExpectingRelationships()) {
266+
if (!$object->has(ResourceObject::RELATIONSHIPS) && $expectation) {
252267
$this->error(static::ERROR_MISSING_RELATIONSHIPS);
253268
return $this;
254269
}

src/Validator/Resource/AttributesValidatorTrait.php

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ trait AttributesValidatorTrait
3333
*/
3434
protected $_attributesValidator;
3535

36-
/**
37-
* @var bool
38-
*/
39-
protected $_expectingAttributes = true;
40-
4136
/**
4237
* @param ValidatorInterface $validator
4338
* @return $this
@@ -61,25 +56,6 @@ public function getAttributesValidator()
6156
return $this->_attributesValidator;
6257
}
6358

64-
/**
65-
* @param $bool
66-
* @return $this
67-
*/
68-
public function setExpectingAttributes($bool)
69-
{
70-
$this->_expectingAttributes = (bool) $bool;
71-
72-
return $this;
73-
}
74-
75-
/**
76-
* @return bool
77-
*/
78-
public function isExpectingAttributes()
79-
{
80-
return (bool) $this->_expectingAttributes;
81-
}
82-
8359
/**
8460
* @return AttributesValidator
8561
*/

src/Validator/Resource/RelationshipsValidatorTrait.php

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ trait RelationshipsValidatorTrait
3333
*/
3434
protected $_relationshipsValidator;
3535

36-
/**
37-
* @var bool
38-
*/
39-
protected $_expectingRelationships = false;
40-
4136
/**
4237
* @param ValidatorInterface $validator
4338
* @return $this
@@ -61,25 +56,6 @@ public function getRelationshipsValidator()
6156
return $this->_relationshipsValidator;
6257
}
6358

64-
/**
65-
* @param $bool
66-
* @return $this
67-
*/
68-
public function setExpectingRelationships($bool)
69-
{
70-
$this->_expectingRelationships = (bool) $bool;
71-
72-
return $this;
73-
}
74-
75-
/**
76-
* @return bool
77-
*/
78-
public function isExpectingRelationships()
79-
{
80-
return (bool) $this->_expectingRelationships;
81-
}
82-
8359
/**
8460
* @return RelationshipsValidator
8561
*/

test/Validator/Attributes/AttributesValidatorTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,11 @@ public function testNotAllowed()
6969
public function testRequiredKeys()
7070
{
7171
$validator = new AttributesValidator();
72+
73+
$this->assertFalse($validator->hasRequiredKeys());
7274
$this->assertSame($validator, $validator->setRequiredKeys([static::KEY_A, static::KEY_B]));
7375
$this->assertTrue($validator->isValid($this->data));
76+
$this->assertTrue($validator->hasRequiredKeys());
7477
}
7578

7679
public function testMissingRequired()

test/Validator/Relationships/RelationshipsValidatorTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,15 @@ public function testUnrecognisedKey()
130130

131131
public function testRequired()
132132
{
133+
$this->assertFalse($this->validator->hasRequiredKeys());
134+
133135
$this->assertSame($this->validator, $this->validator->setRequiredKeys([
134136
static::KEY_A,
135137
static::KEY_B,
136138
]));
137139

140+
$this->assertTrue($this->validator->hasRequiredKeys());
141+
138142
$this->validator->setValidators([
139143
static::KEY_A => $this->a,
140144
static::KEY_B => $this->b,

test/Validator/Resource/AbstractResourceObjectValidatorTest.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
use CloudCreativity\JsonApi\Error\ErrorCollection;
2323
use CloudCreativity\JsonApi\Error\ErrorObject;
2424
use CloudCreativity\JsonApi\Object\Resource\ResourceObject;
25-
use Neomerx\JsonApi\Document\Error;
25+
use CloudCreativity\JsonApi\Contracts\Validator\KeyedValidatorInterface;
2626

2727
class AbstractResourceObjectValidatorTest extends \PHPUnit_Framework_TestCase
2828
{
@@ -184,10 +184,16 @@ public function testInvalidId()
184184

185185
public function testMissingAttributes()
186186
{
187-
$this->validator
188-
->method('isExpectingAttributes')
187+
$mock = $this->getMock(KeyedValidatorInterface::class);
188+
189+
$mock->expects($this->atLeastOnce())
190+
->method('hasRequiredKeys')
189191
->willReturn(true);
190192

193+
$this->validator
194+
->method('getAttributesValidator')
195+
->willReturn($mock);
196+
191197
$this->assertFalse($this->validator->isValid($this->input));
192198

193199
/** @var ErrorObject $error */
@@ -246,10 +252,16 @@ public function testInvalidAttributes()
246252

247253
public function testMissingRelationships()
248254
{
249-
$this->validator
250-
->method('isExpectingRelationships')
255+
$mock = $this->getMock(KeyedValidatorInterface::class);
256+
257+
$mock->expects($this->atLeastOnce())
258+
->method('hasRequiredKeys')
251259
->willReturn(true);
252260

261+
$this->validator
262+
->method('getRelationshipsValidator')
263+
->willReturn($mock);
264+
253265
$this->assertFalse($this->validator->isValid($this->input));
254266

255267
/** @var ErrorObject $error */

0 commit comments

Comments
 (0)