Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] refactor FormatConstraint, use factory approach #280

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions src/JsonSchema/Constraints/FormatConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
*/

namespace JsonSchema\Constraints;

use JsonSchema\Rfc3339;
use InvalidArgumentException;

/**
* Validates against the "format" property
Expand All @@ -18,6 +20,9 @@
*/
class FormatConstraint extends Constraint
{
protected $map = array(
'phone' => 'JsonSchema\Constraints\Validators\PhoneValidator'
);
/**
* {@inheritDoc}
*/
Expand All @@ -27,6 +32,15 @@ public function check($element, $schema = null, $path = null, $i = null)
return;
}

if(array_key_exists($schema->format, $this->map)) {
try {
(new $this->map[$schema->format])($element);
} catch(InvalidArgumentException $e) {
$this->addError($path, $e->getMessage(), 'format', array('format' => $schema->format,));
}
return;
}

switch ($schema->format) {
case 'date':
if (!$date = $this->validateDateTime($element, 'Y-m-d')) {
Expand Down Expand Up @@ -70,12 +84,6 @@ public function check($element, $schema = null, $path = null, $i = null)
}
break;

case 'phone':
if (!$this->validatePhone($element)) {
$this->addError($path, "Invalid phone number", 'format', array('format' => $schema->format,));
}
break;

case 'uri':
if (null === filter_var($element, FILTER_VALIDATE_URL, FILTER_NULL_ON_FAILURE)) {
$this->addError($path, "Invalid URL format", 'format', array('format' => $schema->format,));
Expand Down Expand Up @@ -154,11 +162,6 @@ protected function validateStyle($style)
return empty($invalidEntries);
}

protected function validatePhone($phone)
{
return preg_match('/^\+?(\(\d{3}\)|\d{3}) \d{3} \d{4}$/', $phone);
}

protected function validateHostname($host)
{
return preg_match('/^[_a-z]+\.([_a-z]+\.?)+$/i', $host);
Expand Down
22 changes: 22 additions & 0 deletions src/JsonSchema/Constraints/Validators/PhoneValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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\Validators;

use InvalidArgumentException;

class PhoneValidator
{
public function __invoke($element)
{
if(!preg_match('/^\+?(\(\d{3}\)|\d{3}) \d{3} \d{4}$/', $element)) {
throw new InvalidArgumentException('Invalid phone number');
}
}
}