Skip to content

Commit 92e78ec

Browse files
committed
More validation work.
1 parent cf1c2d1 commit 92e78ec

26 files changed

+1217
-21
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace CloudCreativity\JsonApi\Contracts\Stdlib;
4+
5+
interface ConfigurableInterface
6+
{
7+
8+
/**
9+
* @param array $config
10+
* @return $this
11+
*/
12+
public function configure(array $config);
13+
}

src/Object/Relationships/Relationships.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ public function __get($key)
1818

1919
/**
2020
* @param $key
21+
* @param $default
2122
* @return Relationship
2223
*/
23-
public function get($key)
24+
public function get($key, $default = null)
2425
{
25-
return new Relationship(parent::get($key));
26+
return new Relationship(parent::get($key, $default));
2627
}
2728

2829
/**

src/Validator/AbstractValidator.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,18 @@ abstract protected function validate($value);
7878

7979
/**
8080
* @param $key
81+
* @param $pointer
8182
* @return ErrorObject
8283
*/
83-
protected function error($key)
84+
protected function error($key, $pointer = null)
8485
{
8586
$error = ErrorObject::create($this->template($key));
8687
$this->getErrors()->add($error);
8788

89+
if (!is_null($pointer)) {
90+
$error->source()->setPointer($pointer);
91+
}
92+
8893
return $error;
8994
}
9095

src/Validator/Attributes/AttributesValidator.php

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,22 @@
22

33
namespace CloudCreativity\JsonApi\Validator\Attributes;
44

5+
use CloudCreativity\JsonApi\Contracts\Stdlib\ConfigurableInterface;
56
use CloudCreativity\JsonApi\Error\ErrorObject;
67
use CloudCreativity\JsonApi\Error\SourceObject;
78
use CloudCreativity\JsonApi\Validator\AbstractValidator;
9+
use CloudCreativity\JsonApi\Validator\Type\StringValidator;
810
use CloudCreativity\JsonApi\Validator\Type\TypeValidator;
911
use CloudCreativity\JsonApi\Contracts\Validator\ValidatorInterface;
1012

11-
class AttributesValidator extends AbstractValidator
13+
class AttributesValidator extends AbstractValidator implements ConfigurableInterface
1214
{
1315

16+
// Config constants
17+
const ALLOWED = 'allowed';
18+
const REQUIRED = 'required';
19+
20+
// Error constants
1421
const ERROR_INVALID_VALUE = 'invalid-value';
1522
const ERROR_UNRECOGNISED_ATTRIBUTE = 'not-recognised';
1623
const ERROR_REQUIRED_ATTRIBUTE = 'required';
@@ -120,6 +127,55 @@ public function getValidator($key)
120127
return $this->_validators[$key];
121128
}
122129

130+
/**
131+
* Helper method to add a type validator for the specified key.
132+
*
133+
* @param $key
134+
* @param null $type
135+
* @param array $options
136+
* @return $this
137+
*/
138+
public function attr($key, $type = null, array $options = [])
139+
{
140+
if (is_null($type)) {
141+
$type = 'type';
142+
}
143+
144+
$class = sprintf('CloudCreativity\JsonApi\Validator\Type\%sValidator', ucfirst($type));
145+
146+
if (!class_exists($class)) {
147+
throw new \InvalidArgumentException(sprintf('Unrecognised attribute type: %s.', $type));
148+
}
149+
150+
/** @var ValidatorInterface $validator */
151+
$validator = new $class();
152+
153+
if ($validator instanceof ConfigurableInterface) {
154+
$validator->configure($options);
155+
}
156+
157+
$this->setValidator($key, $validator);
158+
159+
return $this;
160+
}
161+
162+
/**
163+
* @param array $config
164+
* @return $this
165+
*/
166+
public function configure(array $config)
167+
{
168+
if (isset($config[static::ALLOWED]) && is_array($config[static::ALLOWED])) {
169+
$this->setAllowed($config[static::ALLOWED]);
170+
}
171+
172+
if (isset($config[static::REQUIRED]) && is_array($config[static::REQUIRED])) {
173+
$this->setRequired($config[static::REQUIRED]);
174+
}
175+
176+
return $this;
177+
}
178+
123179
/**
124180
* @param $value
125181
*/

src/Validator/Relationships/BelongsToValidator.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,22 @@
22

33
namespace CloudCreativity\JsonApi\Validator\Relationships;
44

5+
use CloudCreativity\JsonApi\Contracts\Stdlib\ConfigurableInterface;
56
use CloudCreativity\JsonApi\Error\ErrorObject;
67
use CloudCreativity\JsonApi\Object\Relationships\Relationship;
78
use CloudCreativity\JsonApi\Object\ResourceIdentifier\ResourceIdentifier;
89
use CloudCreativity\JsonApi\Validator\AbstractValidator;
910

10-
class BelongsToValidator extends AbstractValidator
11+
class BelongsToValidator extends AbstractValidator implements ConfigurableInterface
1112
{
1213

14+
// Config constants
15+
const TYPES = 'types';
16+
const TYPE = self::TYPES;
17+
const ALLOW_EMPTY = 'allowEmpty';
18+
const CALLBACK = 'callback';
19+
20+
// Error constants
1321
const ERROR_INVALID_VALUE = 'invalid-value';
1422
const ERROR_INVALID_TYPE = 'invalid-resource-type';
1523
const ERROR_INVALID_ID = 'invalid-resouce-id';
@@ -53,6 +61,16 @@ class BelongsToValidator extends AbstractValidator
5361
],
5462
];
5563

64+
/**
65+
* @param $typeOrTypes
66+
*/
67+
public function __construct($typeOrTypes = null)
68+
{
69+
if (!is_null($typeOrTypes)) {
70+
$this->setTypes($typeOrTypes);
71+
}
72+
}
73+
5674
public function setTypes($typeOrTypes)
5775
{
5876
$this->_types = is_array($typeOrTypes) ? $typeOrTypes : [$typeOrTypes];
@@ -102,6 +120,23 @@ public function hasCallback()
102120
return is_callable($this->_callback);
103121
}
104122

123+
public function configure(array $config)
124+
{
125+
if (isset($config[static::TYPES])) {
126+
$this->setTypes($config[static::TYPES]);
127+
}
128+
129+
if (array_key_exists(static::ALLOW_EMPTY, $config)) {
130+
$this->setAllowEmpty($config[static::ALLOW_EMPTY]);
131+
}
132+
133+
if (isset($config[static::CALLBACK])) {
134+
$this->setCallback($config[static::CALLBACK]);
135+
}
136+
137+
return $this;
138+
}
139+
105140
protected function validate($value)
106141
{
107142
// must be an object

src/Validator/Relationships/HasManyValidator.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,23 @@
22

33
namespace CloudCreativity\JsonApi\Validator\Relationships;
44

5+
use CloudCreativity\JsonApi\Contracts\Stdlib\ConfigurableInterface;
56
use CloudCreativity\JsonApi\Error\ErrorObject;
67
use CloudCreativity\JsonApi\Validator\AbstractValidator;
78
use CloudCreativity\JsonApi\Object\Relationships\Relationship;
89
use CloudCreativity\JsonApi\Object\ResourceIdentifier\ResourceIdentifier;
910
use CloudCreativity\JsonApi\Object\ResourceIdentifier\ResourceIdentifierCollection;
1011

11-
class HasManyValidator extends AbstractValidator
12+
class HasManyValidator extends AbstractValidator implements ConfigurableInterface
1213
{
1314

15+
// Config constants
16+
const TYPES = 'types';
17+
const TYPE = self::TYPES;
18+
const ALLOW_EMPTY = 'allowEmpty';
19+
const CALLBACK = 'callback';
20+
21+
// Error constants
1422
const ERROR_INVALID_VALUE = BelongsToValidator::ERROR_INVALID_VALUE;
1523
const ERROR_INVALID_TYPE = BelongsToValidator::ERROR_INVALID_TYPE;
1624
const ERROR_INVALID_ID = BelongsToValidator::ERROR_INVALID_ID;
@@ -61,6 +69,16 @@ class HasManyValidator extends AbstractValidator
6169
protected $_allowEmpty = true;
6270
protected $_callback;
6371

72+
/**
73+
* @param $typeOrTypes
74+
*/
75+
public function __construct($typeOrTypes = null)
76+
{
77+
if (!is_null($typeOrTypes)) {
78+
$this->setTypes($typeOrTypes);
79+
}
80+
}
81+
6482
public function setTypes($typeOrTypes)
6583
{
6684
$this->_types = is_array($typeOrTypes) ? $typeOrTypes : [$typeOrTypes];
@@ -110,6 +128,23 @@ public function hasCallback()
110128
return is_callable($this->_callback);
111129
}
112130

131+
public function configure(array $config)
132+
{
133+
if (isset($config[static::TYPES])) {
134+
$this->setTypes($config[static::TYPES]);
135+
}
136+
137+
if (array_key_exists(static::ALLOW_EMPTY, $config)) {
138+
$this->setAllowEmpty($config[static::ALLOW_EMPTY]);
139+
}
140+
141+
if (isset($config[static::CALLBACK])) {
142+
$this->setCallback($config[static::CALLBACK]);
143+
}
144+
145+
return $this;
146+
}
147+
113148
protected function validate($value)
114149
{
115150
// must be an object.

0 commit comments

Comments
 (0)