First commit

This commit is contained in:
Frédéric Guillot
2014-01-25 14:56:02 -05:00
commit 9383a15af6
80 changed files with 7519 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
<?php
/*
* This file is part of Simple Validator.
*
* (c) Frédéric Guillot <contact@fredericguillot.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace SimpleValidator\Validators;
use SimpleValidator\Base;
/**
* @author Frédéric Guillot <contact@fredericguillot.com>
*/
class Alpha extends Base
{
public function execute(array $data)
{
if (isset($data[$this->field]) && $data[$this->field] !== '') {
if (! ctype_alpha($data[$this->field])) {
return false;
}
}
return true;
}
}

View File

@@ -0,0 +1,33 @@
<?php
/*
* This file is part of Simple Validator.
*
* (c) Frédéric Guillot <contact@fredericguillot.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace SimpleValidator\Validators;
use SimpleValidator\Base;
/**
* @author Frédéric Guillot <contact@fredericguillot.com>
*/
class AlphaNumeric extends Base
{
public function execute(array $data)
{
if (isset($data[$this->field]) && $data[$this->field] !== '') {
if (! ctype_alnum($data[$this->field])) {
return false;
}
}
return true;
}
}

View File

@@ -0,0 +1,81 @@
<?php
/*
* This file is part of Simple Validator.
*
* (c) Frédéric Guillot <contact@fredericguillot.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace SimpleValidator\Validators;
use SimpleValidator\Base;
/**
* @author Frédéric Guillot <contact@fredericguillot.com>
*/
class Email extends Base
{
public function execute(array $data)
{
if (isset($data[$this->field]) && $data[$this->field] !== '') {
// I use the same validation method as Firefox
// http://hg.mozilla.org/mozilla-central/file/cf5da681d577/content/html/content/src/nsHTMLInputElement.cpp#l3967
$value = $data[$this->field];
$length = strlen($value);
// If the email address begins with a '@' or ends with a '.',
// we know it's invalid.
if ($value[0] === '@' || $value[$length - 1] === '.') {
return false;
}
// Check the username
for ($i = 0; $i < $length && $value[$i] !== '@'; ++$i) {
$c = $value[$i];
if (! (ctype_alnum($c) || $c === '.' || $c === '!' || $c === '#' || $c === '$' ||
$c === '%' || $c === '&' || $c === '\'' || $c === '*' || $c === '+' ||
$c === '-' || $c === '/' || $c === '=' || $c === '?' || $c === '^' ||
$c === '_' || $c === '`' || $c === '{' || $c === '|' || $c === '}' ||
$c === '~')) {
return false;
}
}
// There is no domain name (or it's one-character long),
// that's not a valid email address.
if (++$i >= $length) return false;
if (($i + 1) === $length) return false;
// The domain name can't begin with a dot.
if ($value[$i] === '.') return false;
// Parsing the domain name.
for (; $i < $length; ++$i) {
$c = $value[$i];
if ($c === '.') {
// A dot can't follow a dot.
if ($value[$i - 1] === '.') return false;
}
elseif (! (ctype_alnum($c) || $c === '-')) {
// The domain characters have to be in this list to be valid.
return false;
}
}
}
return true;
}
}

View File

@@ -0,0 +1,43 @@
<?php
/*
* This file is part of Simple Validator.
*
* (c) Frédéric Guillot <contact@fredericguillot.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace SimpleValidator\Validators;
use SimpleValidator\Base;
/**
* @author Frédéric Guillot <contact@fredericguillot.com>
*/
class Equals extends Base
{
private $field2;
public function __construct($field1, $field2, $error_message)
{
parent::__construct($field1, $error_message);
$this->field2 = $field2;
}
public function execute(array $data)
{
if (isset($data[$this->field]) && $data[$this->field] !== '') {
if (! isset($data[$this->field2])) return false;
return $data[$this->field] === $data[$this->field2];
}
return true;
}
}

View File

@@ -0,0 +1,42 @@
<?php
/*
* This file is part of Simple Validator.
*
* (c) Frédéric Guillot <contact@fredericguillot.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace SimpleValidator\Validators;
use SimpleValidator\Base;
/**
* @author Frédéric Guillot <contact@fredericguillot.com>
*/
class Integer extends Base
{
public function execute(array $data)
{
if (isset($data[$this->field]) && $data[$this->field] !== '') {
if (is_string($data[$this->field])) {
if ($data[$this->field][0] === '-') {
return ctype_digit(substr($data[$this->field], 1));
}
return ctype_digit($data[$this->field]);
}
else {
return is_int($data[$this->field]);
}
}
return true;
}
}

View File

@@ -0,0 +1,33 @@
<?php
/*
* This file is part of Simple Validator.
*
* (c) Frédéric Guillot <contact@fredericguillot.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace SimpleValidator\Validators;
use SimpleValidator\Base;
/**
* @author Frédéric Guillot <contact@fredericguillot.com>
*/
class Ip extends Base
{
public function execute(array $data)
{
if (isset($data[$this->field]) && $data[$this->field] !== '') {
if (! filter_var($data[$this->field], FILTER_VALIDATE_IP)) {
return false;
}
}
return true;
}
}

View File

@@ -0,0 +1,48 @@
<?php
/*
* This file is part of Simple Validator.
*
* (c) Frédéric Guillot <contact@fredericguillot.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace SimpleValidator\Validators;
use SimpleValidator\Base;
/**
* @author Frédéric Guillot <contact@fredericguillot.com>
*/
class Length extends Base
{
private $min;
private $max;
public function __construct($field, $error_message, $min, $max)
{
parent::__construct($field, $error_message);
$this->min = $min;
$this->max = $max;
}
public function execute(array $data)
{
if (isset($data[$this->field]) && $data[$this->field] !== '') {
$length = mb_strlen($data[$this->field], 'UTF-8');
if ($length < $this->min || $length > $this->max) {
return false;
}
}
return true;
}
}

View File

@@ -0,0 +1,37 @@
<?php
/*
* This file is part of Simple Validator.
*
* (c) Frédéric Guillot <contact@fredericguillot.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace SimpleValidator\Validators;
use SimpleValidator\Base;
/**
* @author Frédéric Guillot <contact@fredericguillot.com>
*/
class MacAddress extends Base
{
public function execute(array $data)
{
if (isset($data[$this->field]) && $data[$this->field] !== '') {
$groups = explode(':', $data[$this->field]);
if (count($groups) !== 6) return false;
foreach ($groups as $group) {
if (! ctype_xdigit($group)) return false;
}
}
return true;
}
}

View File

@@ -0,0 +1,46 @@
<?php
/*
* This file is part of Simple Validator.
*
* (c) Frédéric Guillot <contact@fredericguillot.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace SimpleValidator\Validators;
use SimpleValidator\Base;
/**
* @author Frédéric Guillot <contact@fredericguillot.com>
*/
class MaxLength extends Base
{
private $max;
public function __construct($field, $error_message, $max)
{
parent::__construct($field, $error_message);
$this->max = $max;
}
public function execute(array $data)
{
if (isset($data[$this->field]) && $data[$this->field] !== '') {
$length = mb_strlen($data[$this->field], 'UTF-8');
if ($length > $this->max) {
return false;
}
}
return true;
}
}

View File

@@ -0,0 +1,46 @@
<?php
/*
* This file is part of Simple Validator.
*
* (c) Frédéric Guillot <contact@fredericguillot.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace SimpleValidator\Validators;
use SimpleValidator\Base;
/**
* @author Frédéric Guillot <contact@fredericguillot.com>
*/
class MinLength extends Base
{
private $min;
public function __construct($field, $error_message, $min)
{
parent::__construct($field, $error_message);
$this->min = $min;
}
public function execute(array $data)
{
if (isset($data[$this->field]) && $data[$this->field] !== '') {
$length = mb_strlen($data[$this->field], 'UTF-8');
if ($length < $this->min) {
return false;
}
}
return true;
}
}

View File

@@ -0,0 +1,33 @@
<?php
/*
* This file is part of Simple Validator.
*
* (c) Frédéric Guillot <contact@fredericguillot.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace SimpleValidator\Validators;
use SimpleValidator\Base;
/**
* @author Frédéric Guillot <contact@fredericguillot.com>
*/
class Numeric extends Base
{
public function execute(array $data)
{
if (isset($data[$this->field]) && $data[$this->field] !== '') {
if (! is_numeric($data[$this->field])) {
return false;
}
}
return true;
}
}

View File

@@ -0,0 +1,51 @@
<?php
/*
* This file is part of Simple Validator.
*
* (c) Frédéric Guillot <contact@fredericguillot.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace SimpleValidator\Validators;
use SimpleValidator\Base;
/**
* @author Frédéric Guillot <contact@fredericguillot.com>
*/
class Range extends Base
{
private $min;
private $max;
public function __construct($field, $error_message, $min, $max)
{
parent::__construct($field, $error_message);
$this->min = $min;
$this->max = $max;
}
public function execute(array $data)
{
if (isset($data[$this->field]) && $data[$this->field] !== '') {
if (! is_numeric($data[$this->field])) {
return false;
}
if ($data[$this->field] < $this->min || $data[$this->field] > $this->max) {
return false;
}
}
return true;
}
}

View File

@@ -0,0 +1,30 @@
<?php
/*
* This file is part of Simple Validator.
*
* (c) Frédéric Guillot <contact@fredericguillot.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace SimpleValidator\Validators;
use SimpleValidator\Base;
/**
* @author Frédéric Guillot <contact@fredericguillot.com>
*/
class Required extends Base
{
public function execute(array $data)
{
if (! isset($data[$this->field]) || $data[$this->field] === '') {
return false;
}
return true;
}
}

View File

@@ -0,0 +1,78 @@
<?php
/*
* This file is part of Simple Validator.
*
* (c) Frédéric Guillot <contact@fredericguillot.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace SimpleValidator\Validators;
use SimpleValidator\Base;
/**
* @author Frédéric Guillot <contact@fredericguillot.com>
*/
class Unique extends Base
{
private $pdo;
private $primary_key;
private $table;
public function __construct($field, $error_message, \PDO $pdo, $table, $primary_key = 'id')
{
parent::__construct($field, $error_message);
$this->pdo = $pdo;
$this->primary_key = $primary_key;
$this->table = $table;
}
public function execute(array $data)
{
if (isset($data[$this->field]) && $data[$this->field] !== '') {
if (! isset($data[$this->primary_key])) {
$rq = $this->pdo->prepare('SELECT COUNT(*) FROM '.$this->table.' WHERE '.$this->field.'=?');
$rq->execute(array(
$data[$this->field]
));
$result = $rq->fetch(\PDO::FETCH_NUM);
if (isset($result[0]) && $result[0] === '1') {
return false;
}
}
else {
$rq = $this->pdo->prepare(
'SELECT COUNT(*) FROM '.$this->table.'
WHERE '.$this->field.'=? AND '.$this->primary_key.' != ?'
);
$rq->execute(array(
$data[$this->field],
$data[$this->primary_key]
));
$result = $rq->fetch(\PDO::FETCH_NUM);
if (isset($result[0]) && $result[0] === '1') {
return false;
}
}
}
return true;
}
}

View File

@@ -0,0 +1,32 @@
<?php
/*
* This file is part of Simple Validator.
*
* (c) Frédéric Guillot <contact@fredericguillot.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace SimpleValidator\Validators;
use SimpleValidator\Base;
/**
* @author Frédéric Guillot <contact@fredericguillot.com>
* @link http://semver.org/
*/
class Version extends Base
{
public function execute(array $data)
{
if (isset($data[$this->field]) && $data[$this->field] !== '') {
$pattern = '/^[0-9]+\.[0-9]+\.[0-9]+([+-][^+-][0-9A-Za-z-.]*)?$/';
return (bool) preg_match($pattern, $data[$this->field]);
}
return true;
}
}