Permalink
Browse files

Very early stage Internet Domain Name validators. New Contains and Tl…

…d (top-level domain) validators. NoWhitespace now validates for null values.
  • Loading branch information...
1 parent b12b816 commit 191065bf4c029b35f43974fcd45fa22deb704819 @alganet alganet committed Feb 21, 2011
View
@@ -281,6 +281,11 @@ Positive and Negative:
v::negative()->assert(-5);
v::positive()->assert(3);
+Starts with and Ends with:
+
+ v::startsWith('Hello')->assert('Hello World');
+ v::endsWith('World')->assert('Hello World');
+
Whitespace, empty and null
v::noWhitespace()->assert('abc');
@@ -21,7 +21,7 @@ public function chooseTemplate()
public function getParams()
{
if (1 === count($this->related))
- return $this->related[0]->getParams();
+ return current($this->related)->getParams();
else
return parent::getParams();
}
@@ -30,16 +30,16 @@ public function getRelated($full=false)
{
if ($full || 1 !== count($this->related))
return $this->related;
- elseif ($this->related[0] instanceof AbstractNestedException)
- return $this->related[0]->getRelated();
+ elseif (current($this->related) instanceof AbstractNestedException)
+ return current($this->related)->getRelated();
else
return array();
}
public function getTemplate()
{
if (1 === count($this->related))
- return $this->related[0]->getTemplate();
+ return current($this->related)->getTemplate();
else
return parent::getTemplate();
}
@@ -0,0 +1,12 @@
+<?php
+
+namespace Respect\Validation\Exceptions;
+
+class ContainsException extends ValidationException
+{
+
+ public static $defaultTemplates = array(
+ self::STANDARD => '{{name}} must contain the value "{{containsValue}}"',
+ );
+
+}
@@ -0,0 +1,12 @@
+<?php
+
+namespace Respect\Validation\Exceptions;
+
+class DomainException extends AbstractNestedException
+{
+
+ public static $defaultTemplates = array(
+ self::STANDARD => '{{name}} must be a valid domain',
+ );
+
+}
@@ -0,0 +1,12 @@
+<?php
+
+namespace Respect\Validation\Exceptions;
+
+class TldException extends ValidationException
+{
+
+ public static $defaultTemplates = array(
+ self::STANDARD => '{{name}} must be a valid top-level domain name',
+ );
+
+}
@@ -7,7 +7,7 @@ class Call extends AbstractRelated
public function getReferenceValue($input)
{
- return call_user_func($this->reference, $input);
+ return call_user_func($this->reference, &$input);
}
public function hasReference($input)
@@ -0,0 +1,41 @@
+<?php
+
+namespace Respect\Validation\Rules;
+
+class Contains extends AbstractRule
+{
+
+ public $containsValue;
+ public $identical;
+
+ public function __construct($containsValue, $identical=false)
+ {
+ $this->containsValue = $containsValue;
+ $this->identical = $identical;
+ }
+
+ public function validate($input)
+ {
+ if ($this->identical)
+ return $this->validateIdentical($input);
+ else
+ return $this->validateEquals($input);
+ }
+
+ protected function validateEquals($input)
+ {
+ if (is_array($input))
+ return in_array($this->containsValue, $input);
+ else
+ return false !== mb_stripos($input, $this->containsValue);
+ }
+
+ protected function validateIdentical($input)
+ {
+ if (is_array($input))
+ return in_array($this->containsValue, $input, true);
+ else
+ return false !== mb_strpos($input, $this->containsValue);
+ }
+
+}
@@ -0,0 +1,98 @@
+<?php
+
+namespace Respect\Validation\Rules;
+
+use Respect\Validation\Exceptions\ValidationException;
+
+class Domain extends AbstractComposite
+{
+
+ protected $ip;
+ protected $whitespace;
+ protected $dot;
+ protected $start;
+ protected $end;
+ protected $otherParts;
+ protected $domainLength;
+
+ public function __construct()
+ {
+ $this->ip = new Ip();
+ $this->whitespace = new NoWhitespace();
+ $this->dot = new Contains('.');
+ $this->domainLength = new Length(3, null);
+ $this->end = new Tld();
+ $this->otherParts = new Alnum('-');
+ }
+
+ public function validate($input)
+ {
+ if ($this->ip->validate($input))
+ return true;
+ if (!$this->whitespace->validate($input)
+ || !$this->dot->validate($input)
+ || !$this->domainLength->validate($input))
+ return false;
+
+ $parts = explode('.', $input);
+ if (count($parts) < 2)
+ return false;
+ if (!$this->end->validate(array_pop($parts)))
+ return false;
+ foreach ($parts as $p)
+ if (!$this->otherParts->validate($p))
+ return false;
+ return true;
+ }
+
+ public function assert($input)
+ {
+ if ($this->ip->validate($input))
+ return true;
+
+ $e = array();
+
+ $this->collectAssertException($e, $this->whitespace, $input);
+ $this->collectAssertException($e, $this->dot, $input);
+ $this->collectAssertException($e, $this->domainLength, $input);
+
+ $parts = explode('.', $input);
+
+ if (count($parts) >= 2)
+ $this->collectAssertException($e, $this->end, array_pop($parts));
+ foreach ($parts as $p)
+ $this->collectAssertException($e, $this->otherParts, $p);
+
+ if (count($e))
+ throw $this->reportError($input)->setRelated($e);
+ return true;
+ }
+
+ protected function collectAssertException(&$exceptions, $validator, $input)
+ {
+ try {
+ $validator->assert($input);
+ } catch (ValidationException $e) {
+ $exceptions[] = $e;
+ }
+ }
+
+ public function check($input)
+ {
+ if ($this->ip->validate($input))
+ return true;
+ $this->whitespace->check($input);
+ $this->dot->check($input);
+ $this->domainLength->check($input);
+
+ $parts = explode('.', $input);
+
+ if (count($parts) >= 2)
+ $this->end->check(array_pop($parts));
+ foreach ($parts as $p)
+ $this->otherParts->check($p);
+
+ return true;
+ }
+
+}
@@ -7,7 +7,7 @@ class NoWhitespace extends AbstractRule
public function validate($input)
{
- return preg_match('#^\S+$#', $input);
+ return is_null($input) || preg_match('#^\S+$#', $input);
}
}
@@ -0,0 +1,42 @@
+<?php
+
+namespace Respect\Validation\Rules;
+
+class Tld extends AbstractRule
+{
+
+ protected $tldList = array(
+ //generic
+ 'aero', 'asia', 'biz', 'cat', 'com', 'coop', 'edu', 'gov', 'info',
+ 'int', 'jobs', 'mil', 'mobi', 'museum', 'name', 'net', 'org', 'pro',
+ 'tel', 'travel',
+ //country
+ 'ac', 'ad', 'ae', 'af', 'ag', 'ai', 'al', 'am', 'ao', 'aq', 'ar', 'as',
+ 'at', 'au', 'aw', 'ax', 'az', 'ba', 'bb', 'bd', 'be', 'bf', 'bg', 'bh',
+ 'bi', 'bj', 'bm', 'bn', 'bo', 'br', 'bs', 'bt', 'bw', 'by', 'bz',
+ 'ca', 'cc', 'cd', 'cf', 'cg', 'ch', 'ci', 'ck', 'cl', 'cm', 'cn', 'co',
+ 'cr', 'cs', 'cu', 'cv', 'cx', 'cy', 'cz', 'de', 'dj', 'dk', 'dm', 'do',
+ 'dz', 'ec', 'ee', 'eg', 'er', 'es', 'et', 'eu', 'fi', 'fj', 'fk', 'fm',
+ 'fo', 'fr', 'ga', 'gb', 'gd', 'ge', 'gf', 'gg', 'gh', 'gi', 'gl', 'gm',
+ 'gn', 'gp', 'gq', 'gr', 'gs', 'gt', 'gu', 'gw', 'gy', 'hk', 'hm', 'hn',
+ 'hr', 'ht', 'hu', 'id', 'ie', 'il', 'im', 'in', 'io', 'iq', 'ir', 'is',
+ 'it', 'je', 'jm', 'jo', 'jp', 'ke', 'kg', 'kh', 'ki', 'km', 'kn', 'kp',
+ 'kr', 'kw', 'ky', 'kz', 'la', 'lb', 'lc', 'li', 'lk', 'lr', 'ls', 'lt',
+ 'lu', 'lv', 'ly', 'ma', 'mc', 'md', 'me', 'mg', 'mh', 'mk', 'ml', 'mm',
+ 'mn', 'mo', 'mp', 'mq', 'mr', 'ms', 'mt', 'mu', 'mv', 'mw', 'mx', 'my',
+ 'mz', 'na', 'nc', 'ne', 'nf', 'ng', 'ni', 'nl', 'no', 'np', 'nr', 'nu',
+ 'nz', 'om', 'pa', 'pe', 'pf', 'pg', 'ph', 'pk', 'pl', 'pm', 'pn', 'pr',
+ 'ps', 'pt', 'pw', 'py', 'qa', 're', 'ro', 'rs', 'ru', 'rw', 'sa', 'sb',
+ 'sc', 'sd', 'se', 'sg', 'sh', 'si', 'sk', 'sl', 'sm', 'sn', 'so',
+ 'sr', 'st', 'su', 'sv', 'sy', 'sz', 'tc', 'td', 'tf', 'tg', 'th', 'tj',
+ 'tk', 'tl', 'tm', 'tn', 'to', 'tp', 'tr', 'tt', 'tv', 'tw', 'tz', 'ua',
+ 'ug', 'uk', 'us', 'uy', 'uz', 'va', 'vc', 've', 'vg', 'vi', 'vn', 'vu',
+ 'wf', 'ws', 'ye', 'yt', 'za', 'zm', 'zw'
+ );
+
+ public function validate($input)
+ {
+ return in_array(strtolower($input), $this->tldList);
+ }
+
+}
@@ -4,6 +4,9 @@
use Respect\Validation\Exceptions\ValidationException;
+/**
+ * @method Validator int() int() integer
+ */
interface Validatable
{
@@ -11,7 +11,7 @@
<active>yes</active>
</lead>
<date>2011-02-20</date>
- <time>15:02:52</time>
+ <time>21:53:10</time>
<version>
<release>0.2</release>
<api>0.2</api>
@@ -28,7 +28,7 @@
<dir baseinstalldir="Respect/Validation" name="/">
<file baseinstalldir="Respect/Validation" md5sum="4ef5c6ff21a7382ac26d74354a9c2ece" name="Contexts/AbstractContext.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="a99e9fcc3f89d82a4b6dfd0fbe487014" name="Contexts/Form.php" role="php" />
- <file baseinstalldir="Respect/Validation" md5sum="ef1b18cf77f1806f777f0600abde19c1" name="Exceptions/AbstractGroupedException.php" role="php" />
+ <file baseinstalldir="Respect/Validation" md5sum="848e2674e0575c4f66ddcb5fb3287003" name="Exceptions/AbstractGroupedException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="19c0319d71edea33dee50be75e973185" name="Exceptions/AbstractNestedException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="3e4da4e7b629a4889f87517506664bf3" name="Exceptions/AllOfException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="ae818fe09029e856ed02ce70273da0f5" name="Exceptions/AlnumException.php" role="php" />
@@ -40,9 +40,12 @@
<file baseinstalldir="Respect/Validation" md5sum="887d77c80cbc7176186d62485fb1630b" name="Exceptions/CallbackException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="f5a187f5f40048b7e24022ba8c1c7196" name="Exceptions/CallException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="b22f51d15eb7d8fffceadcaf237948f0" name="Exceptions/ComponentException.php" role="php" />
+ <file baseinstalldir="Respect/Validation" md5sum="32146fc10623f56e79c9efd8210c4072" name="Exceptions/ContainsException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="f4b82cd945205c262506688eada72c91" name="Exceptions/DateException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="294cc49907860578e0afc8f6b67849c0" name="Exceptions/DigitsException.php" role="php" />
+ <file baseinstalldir="Respect/Validation" md5sum="dfcfff040a20f79561e404b1dd6a5a59" name="Exceptions/DomainException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="17457e33110d00c69cd2fc14ff73a125" name="Exceptions/EachException.php" role="php" />
+ <file baseinstalldir="Respect/Validation" md5sum="4974e7de4aa07184eaca0f2ac4eaf280" name="Exceptions/EndsWithException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="48dda2bfad790b78fd0952b3d1425415" name="Exceptions/EqualsException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="9aa8e37c1e8d11335fbdf073eb40c31f" name="Exceptions/FloatException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="d5b41b6bd10891ed842c2285f0975c6d" name="Exceptions/HexaException.php" role="php" />
@@ -66,7 +69,9 @@
<file baseinstalldir="Respect/Validation" md5sum="7df34686076be3531c9bbfca3926728b" name="Exceptions/PositiveException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="cb0971a4379482977ddf22a675581255" name="Exceptions/RegexException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="d0498462a420d57ca74fa47e3c8c6194" name="Exceptions/SfException.php" role="php" />
+ <file baseinstalldir="Respect/Validation" md5sum="14f770fa62744d4593c3aa3cbc72c3c0" name="Exceptions/StartsWithException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="a9d0825a244e0b7d6412777c01367650" name="Exceptions/StringException.php" role="php" />
+ <file baseinstalldir="Respect/Validation" md5sum="1793eec444cfa656116d986652328f61" name="Exceptions/TldException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="29a4034c405fab7925927a2f9ebc72a9" name="Exceptions/ValidationException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="f7294294fd18031fd9ad746ca4945369" name="Exceptions/ZendException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="67eecfc191e3e8d2b7137ba7bac0c574" name="Rules/AbstractComposite.php" role="php" />
@@ -79,11 +84,14 @@
<file baseinstalldir="Respect/Validation" md5sum="9bf907f29fedce62683ba48ccff8ebe0" name="Rules/AtLeast.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="099870a584374ddbff431ad5e504e5ed" name="Rules/Attribute.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="ca4e19a40d5b248a915198c10cde2630" name="Rules/Between.php" role="php" />
- <file baseinstalldir="Respect/Validation" md5sum="952c2d9d0f61c1613d43554a0fd17e42" name="Rules/Call.php" role="php" />
+ <file baseinstalldir="Respect/Validation" md5sum="2a7c997f8bb0732cc6c9d3e50d027765" name="Rules/Call.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="fc648db054e8001969516e07bd331a26" name="Rules/Callback.php" role="php" />
+ <file baseinstalldir="Respect/Validation" md5sum="0350c0f5f0d26f1463dfc7efd74fae29" name="Rules/Contains.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="94f6e04b93cbc45c04207b0ab32e4d98" name="Rules/Date.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="9f72c097ae08a41b0b612799b01d7465" name="Rules/Digits.php" role="php" />
+ <file baseinstalldir="Respect/Validation" md5sum="0855f2bd37ccfd6eef022834fbade6d2" name="Rules/Domain.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="9a20af2507bce58028134aa45d2c375d" name="Rules/Each.php" role="php" />
+ <file baseinstalldir="Respect/Validation" md5sum="52a853d45b508aa6c11b56bca98ae303" name="Rules/EndsWith.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="01d224d7f90af4df2ff2eacb6b1c64b4" name="Rules/Equals.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="3374c2cf568efc7326468193878792bd" name="Rules/Float.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="cf8c4770ebb3021bd1737b677c7270c9" name="Rules/Hexa.php" role="php" />
@@ -99,18 +107,20 @@
<file baseinstalldir="Respect/Validation" md5sum="ecf7ebf9af5c2c963dbde65efc5cfad2" name="Rules/Negative.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="27c5207a71b9bfabc8baf2c60e701889" name="Rules/NoneOf.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="e6f25a27e2bda69903d501fe94b17ffe" name="Rules/NotEmpty.php" role="php" />
- <file baseinstalldir="Respect/Validation" md5sum="a8a1d7a823cd22f3446b8db96f30f2fc" name="Rules/NoWhitespace.php" role="php" />
+ <file baseinstalldir="Respect/Validation" md5sum="59425c3d12731bb2ba6a713ba5468c10" name="Rules/NoWhitespace.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="c5d8a9ee171e6acf3bdab26ef08350c2" name="Rules/NullValue.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="34ed73b15119f9fa8e54d61009fcf0a4" name="Rules/Numeric.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="8a6462204bffbb3eead42a1b92b7d17e" name="Rules/Object.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="67bfc60e4ef4a796a471a3edee9a24bc" name="Rules/OneOf.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="2b9e4d588eefda35f48b2360dcf5b36d" name="Rules/Positive.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="c6cc0755ba8b36aebaa6ad7670fc5124" name="Rules/Regex.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="fad11bb57927795ef56cd97e57b5bb50" name="Rules/Sf.php" role="php" />
+ <file baseinstalldir="Respect/Validation" md5sum="352041ebb93d201605edf2d111013d94" name="Rules/StartsWith.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="d3cf890b95664760156f902207242250" name="Rules/String.php" role="php" />
+ <file baseinstalldir="Respect/Validation" md5sum="997006c2897222ee4915c159efff9f00" name="Rules/Tld.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="1b8f6f1f600788e30d8d56a4a3de905a" name="Rules/Zend.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="707527a26df31bbf09896ecb50573a47" name="ExceptionIterator.php" role="php" />
- <file baseinstalldir="Respect/Validation" md5sum="13a221cd7ccc263afb1605371c8b7a50" name="Validatable.php" role="php" />
+ <file baseinstalldir="Respect/Validation" md5sum="9d4b13cb07c7928fa208a4a0fcaad30a" name="Validatable.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="2f8c0b76a6fb39943e61fa041f4f227a" name="Validator.php" role="php" />
</dir>
</contents>
Oops, something went wrong.

0 comments on commit 191065b

Please sign in to comment.