-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to simplify the public API and make adding features easier (#…
…351) This commit makes the following changes: * Split the Constraint class to make Validator independent from it * Add Validator::validate() as the main entry point * Turn Validator::coerce() and Validator::check() into aliases * Add Factory::setConfig(), getConfig(), addConfig() & removeConfig() * Make type-coercion a checkMode option, don't pass $coerce everywhere * Add some extra tests
- Loading branch information
1 parent
325a0f8
commit 9b6ebfe
Showing
25 changed files
with
348 additions
and
272 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the JsonSchema package. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace JsonSchema\Constraints; | ||
|
||
use JsonSchema\Entity\JsonPointer; | ||
|
||
/** | ||
* A more basic constraint definition - used for the public | ||
* interface to avoid exposing library internals. | ||
*/ | ||
class BaseConstraint | ||
{ | ||
/** | ||
* @var array Errors | ||
*/ | ||
protected $errors = array(); | ||
|
||
/** | ||
* @var Factory | ||
*/ | ||
protected $factory; | ||
|
||
/** | ||
* @param Factory $factory | ||
*/ | ||
public function __construct(Factory $factory = null) | ||
{ | ||
$this->factory = $factory ? : new Factory(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function addError(JsonPointer $path = null, $message, $constraint = '', array $more = null) | ||
{ | ||
$error = array( | ||
'property' => $this->convertJsonPointerIntoPropertyPath($path ?: new JsonPointer('')), | ||
'pointer' => ltrim(strval($path ?: new JsonPointer('')), '#'), | ||
'message' => $message, | ||
'constraint' => $constraint, | ||
); | ||
|
||
if (is_array($more) && count($more) > 0) | ||
{ | ||
$error += $more; | ||
} | ||
|
||
$this->errors[] = $error; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function addErrors(array $errors) | ||
{ | ||
if ($errors) { | ||
$this->errors = array_merge($this->errors, $errors); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getErrors() | ||
{ | ||
return $this->errors; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function isValid() | ||
{ | ||
return !$this->getErrors(); | ||
} | ||
|
||
/** | ||
* Clears any reported errors. Should be used between | ||
* multiple validation checks. | ||
*/ | ||
public function reset() | ||
{ | ||
$this->errors = array(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.