Skip to content

Commit a6b130f

Browse files
committed
Split the constraint class & change the signature for the public API
This allows the Validator class to use a different definition than ConstraintInterface::check(), which in turn means that $config doesn't need to be passed all over the stack.
1 parent 06048eb commit a6b130f

File tree

5 files changed

+103
-77
lines changed

5 files changed

+103
-77
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the JsonSchema package.
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace JsonSchema\Constraints;
11+
12+
use JsonSchema\Entity\JsonPointer;
13+
14+
/**
15+
* A more basic constraint definition - used for the public
16+
* interface to avoid exposing library internals.
17+
*/
18+
class BaseConstraint
19+
{
20+
/**
21+
* @var array Errors
22+
*/
23+
protected $errors = array();
24+
25+
/**
26+
* @var Factory
27+
*/
28+
protected $factory;
29+
30+
/**
31+
* @param Factory $factory
32+
*/
33+
public function __construct(Factory $factory = null)
34+
{
35+
$this->factory = $factory ? : new Factory();
36+
}
37+
38+
/**
39+
* {@inheritDoc}
40+
*/
41+
public function addError(JsonPointer $path = null, $message, $constraint = '', array $more = null)
42+
{
43+
$error = array(
44+
'property' => $this->convertJsonPointerIntoPropertyPath($path ?: new JsonPointer('')),
45+
'pointer' => ltrim(strval($path ?: new JsonPointer('')), '#'),
46+
'message' => $message,
47+
'constraint' => $constraint,
48+
);
49+
50+
if (is_array($more) && count($more) > 0)
51+
{
52+
$error += $more;
53+
}
54+
55+
$this->errors[] = $error;
56+
}
57+
58+
/**
59+
* {@inheritDoc}
60+
*/
61+
public function addErrors(array $errors)
62+
{
63+
if ($errors) {
64+
$this->errors = array_merge($this->errors, $errors);
65+
}
66+
}
67+
68+
/**
69+
* {@inheritDoc}
70+
*/
71+
public function getErrors()
72+
{
73+
return $this->errors;
74+
}
75+
76+
/**
77+
* {@inheritDoc}
78+
*/
79+
public function isValid()
80+
{
81+
return !$this->getErrors();
82+
}
83+
84+
/**
85+
* Clears any reported errors. Should be used between
86+
* multiple validation checks.
87+
*/
88+
public function reset()
89+
{
90+
$this->errors = array();
91+
}
92+
}

src/JsonSchema/Constraints/Constraint.php

Lines changed: 1 addition & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -20,82 +20,13 @@
2020
* @author Robert Schönthal <[email protected]>
2121
* @author Bruno Prieto Reis <[email protected]>
2222
*/
23-
abstract class Constraint implements ConstraintInterface
23+
abstract class Constraint extends BaseConstraint implements ConstraintInterface
2424
{
25-
protected $errors = array();
2625
protected $inlineSchemaProperty = '$schema';
2726

2827
const CHECK_MODE_NORMAL = 0x00000001;
2928
const CHECK_MODE_TYPE_CAST = 0x00000002;
3029

31-
/**
32-
* @var Factory
33-
*/
34-
protected $factory;
35-
36-
/**
37-
* @param Factory $factory
38-
*/
39-
public function __construct(Factory $factory = null)
40-
{
41-
$this->factory = $factory ? : new Factory();
42-
}
43-
44-
/**
45-
* {@inheritDoc}
46-
*/
47-
public function addError(JsonPointer $path = null, $message, $constraint='', array $more=null)
48-
{
49-
$error = array(
50-
'property' => $this->convertJsonPointerIntoPropertyPath($path ?: new JsonPointer('')),
51-
'pointer' => ltrim(strval($path ?: new JsonPointer('')), '#'),
52-
'message' => $message,
53-
'constraint' => $constraint,
54-
);
55-
56-
if (is_array($more) && count($more) > 0)
57-
{
58-
$error += $more;
59-
}
60-
61-
$this->errors[] = $error;
62-
}
63-
64-
/**
65-
* {@inheritDoc}
66-
*/
67-
public function addErrors(array $errors)
68-
{
69-
if ($errors) {
70-
$this->errors = array_merge($this->errors, $errors);
71-
}
72-
}
73-
74-
/**
75-
* {@inheritDoc}
76-
*/
77-
public function getErrors()
78-
{
79-
return $this->errors;
80-
}
81-
82-
/**
83-
* {@inheritDoc}
84-
*/
85-
public function isValid()
86-
{
87-
return !$this->getErrors();
88-
}
89-
90-
/**
91-
* Clears any reported errors. Should be used between
92-
* multiple validation checks.
93-
*/
94-
public function reset()
95-
{
96-
$this->errors = array();
97-
}
98-
9930
/**
10031
* Bubble down the path
10132
*

src/JsonSchema/Constraints/ConstraintInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function addErrors(array $errors);
4040
* @param string $constraint the constraint/rule that is broken, e.g.: 'minLength'
4141
* @param array $more more array elements to add to the error
4242
*/
43-
public function addError(JsonPointer $path = null, $message, $constraint='', array $more=null);
43+
public function addError(JsonPointer $path = null, $message, $constraint='', array $more = null);
4444

4545
/**
4646
* checks if the validator has not raised errors

src/JsonSchema/Validator.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace JsonSchema;
1111

12-
use JsonSchema\Constraints\Constraint;
12+
use JsonSchema\Constraints\BaseConstraint;
1313
use JsonSchema\Entity\JsonPointer;
1414

1515
/**
@@ -19,7 +19,7 @@
1919
* @author Bruno Prieto Reis <[email protected]>
2020
* @see README.md
2121
*/
22-
class Validator extends Constraint
22+
class Validator extends BaseConstraint
2323
{
2424
const SCHEMA_MEDIA_TYPE = 'application/schema+json';
2525

@@ -30,11 +30,16 @@ class Validator extends Constraint
3030
*
3131
* {@inheritDoc}
3232
*/
33-
public function check(&$value, $schema = null, JsonPointer $path = null, $i = null)
33+
public function check(&$value, $schema = null, array $config = array())
3434
{
35+
$initialConfig = $this->factory->getConfig();
36+
$this->factory->setConfig($config);
37+
3538
$validator = $this->factory->createInstanceFor('schema');
3639
$validator->check($value, $schema);
3740

41+
$this->factory->restoreConfig($initialConfig);
42+
3843
$this->addErrors(array_unique($validator->getErrors(), SORT_REGULAR));
3944
}
4045

@@ -47,7 +52,6 @@ public function check(&$value, $schema = null, JsonPointer $path = null, $i = nu
4752
*/
4853
public function coerce(&$value, $schema = null)
4954
{
50-
$this->factory->setConfig(array('coerce-types' => true));
51-
return $this->check($value, $schema);
55+
return $this->check($value, $schema, array('coerce-types' => true));
5256
}
5357
}

tests/Constraints/FactoryTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ public function constraintNameProvider()
6969
array('enum', 'JsonSchema\Constraints\EnumConstraint'),
7070
array('format', 'JsonSchema\Constraints\FormatConstraint'),
7171
array('schema', 'JsonSchema\Constraints\SchemaConstraint'),
72-
array('validator', 'JsonSchema\Validator'),
7372
);
7473
}
7574

0 commit comments

Comments
 (0)