Skip to content
This repository
Browse code

Moving locale dependent validation rules to g11n resources.

Adding integration test.
Updating `ValidatorTest`.
Defaulting example `Catalog` lithium configuration to `Php` adapter.
  • Loading branch information...
commit 338196bc398f9d1dcc211be2b2a9f7b96fa04385 1 parent fcc0202
David Persson authored January 07, 2010 gwoo committed January 08, 2010
4  app/config/bootstrap.php
@@ -139,8 +139,8 @@
139 139
 // 		'path' => LITHIUM_APP_PATH . '/resources/g11n'
140 140
 // 	),
141 141
 // 	'lithium' => array(
142  
-// 		'adapter' => 'Gettext',
143  
-// 		'path' => LITHIUM_LIBRARY_PATH . '/lithium/g11n/resources'
  142
+// 		'adapter' => 'Php',
  143
+// 		'path' => LITHIUM_LIBRARY_PATH . '/lithium/g11n/resources/php'
144 144
 // 	)
145 145
 // ));
146 146
 
16  libraries/lithium/g11n/resources/php/da_DK/validation/default.php
... ...
@@ -0,0 +1,16 @@
  1
+<?php
  2
+/**
  3
+ * Lithium: the most rad php framework
  4
+ *
  5
+ * @copyright     Copyright 2009, Union of RAD (http://union-of-rad.org)
  6
+ * @license       http://opensource.org/licenses/bsd-license.php The BSD License
  7
+ */
  8
+
  9
+/**
  10
+ * Validation data for `da_DK`.
  11
+ */
  12
+return array(
  13
+	'ssn' => '/\\A\\b[0-9]{6}-[0-9]{4}\\b\\z/i'
  14
+);
  15
+
  16
+?>
16  libraries/lithium/g11n/resources/php/de_BE/validation/default.php
... ...
@@ -0,0 +1,16 @@
  1
+<?php
  2
+/**
  3
+ * Lithium: the most rad php framework
  4
+ *
  5
+ * @copyright     Copyright 2009, Union of RAD (http://union-of-rad.org)
  6
+ * @license       http://opensource.org/licenses/bsd-license.php The BSD License
  7
+ */
  8
+
  9
+/**
  10
+ * Validation data for `de_BE`.
  11
+ */
  12
+return array(
  13
+	'postalCode' => '/^[1-9]{1}[0-9]{3}$/i'
  14
+);
  15
+
  16
+?>
16  libraries/lithium/g11n/resources/php/de_DE/validation/default.php
... ...
@@ -0,0 +1,16 @@
  1
+<?php
  2
+/**
  3
+ * Lithium: the most rad php framework
  4
+ *
  5
+ * @copyright     Copyright 2009, Union of RAD (http://union-of-rad.org)
  6
+ * @license       http://opensource.org/licenses/bsd-license.php The BSD License
  7
+ */
  8
+
  9
+/**
  10
+ * Validation data for `de_DE`.
  11
+ */
  12
+return array(
  13
+	'postalCode' => '/^[0-9]{5}$/i'
  14
+);
  15
+
  16
+?>
17  libraries/lithium/g11n/resources/php/en_CA/validation/default.php
... ...
@@ -0,0 +1,17 @@
  1
+<?php
  2
+/**
  3
+ * Lithium: the most rad php framework
  4
+ *
  5
+ * @copyright     Copyright 2009, Union of RAD (http://union-of-rad.org)
  6
+ * @license       http://opensource.org/licenses/bsd-license.php The BSD License
  7
+ */
  8
+
  9
+/**
  10
+ * Validation data for `en_CA`.
  11
+ */
  12
+return array(
  13
+	'phone' => '/^(?:\+?1)?[-. ]?\\(?[2-9][0-8][0-9]\\)?[-. ]?[2-9][0-9]{2}[-. ]?[0-9]{4}$/',
  14
+	'postalCode' => '/\\A\\b[ABCEGHJKLMNPRSTVXY][0-9][A-Z] [0-9][A-Z][0-9]\\b\\z/i'
  15
+);
  16
+
  17
+?>
18  libraries/lithium/g11n/resources/php/en_GB/validation/default.php
... ...
@@ -0,0 +1,18 @@
  1
+<?php
  2
+/**
  3
+ * Lithium: the most rad php framework
  4
+ *
  5
+ * @copyright     Copyright 2009, Union of RAD (http://union-of-rad.org)
  6
+ * @license       http://opensource.org/licenses/bsd-license.php The BSD License
  7
+ */
  8
+
  9
+/**
  10
+ * Validation data for `en_GB`.
  11
+ *
  12
+ * @link http://en.wikipedia.org/wiki/UK_postcodes#Format
  13
+ */
  14
+return array(
  15
+	'postalCode' => '/\\A\\b[A-Z]{1,2}[0-9][A-Z0-9]? [0-9][ABD-HJLNP-UW-Z]{2}\\b\\z/i'
  16
+);
  17
+
  18
+?>
18  libraries/lithium/g11n/resources/php/en_US/validation/default.php
... ...
@@ -0,0 +1,18 @@
  1
+<?php
  2
+/**
  3
+ * Lithium: the most rad php framework
  4
+ *
  5
+ * @copyright     Copyright 2009, Union of RAD (http://union-of-rad.org)
  6
+ * @license       http://opensource.org/licenses/bsd-license.php The BSD License
  7
+ */
  8
+
  9
+/**
  10
+ * Validation data for `en_US`.
  11
+ */
  12
+return array(
  13
+	'phone' => '/^(?:\+?1)?[-. ]?\\(?[2-9][0-8][0-9]\\)?[-. ]?[2-9][0-9]{2}[-. ]?[0-9]{4}$/',
  14
+	'postalCode' => '/\\A\\b[0-9]{5}(?:-[0-9]{4})?\\b\\z/i',
  15
+	'ssn' => '/\\A\\b[0-9]{3}-[0-9]{2}-[0-9]{4}\\b\\z/i'
  16
+);
  17
+
  18
+?>
16  libraries/lithium/g11n/resources/php/fr_BE/validation/default.php
... ...
@@ -0,0 +1,16 @@
  1
+<?php
  2
+/**
  3
+ * Lithium: the most rad php framework
  4
+ *
  5
+ * @copyright     Copyright 2009, Union of RAD (http://union-of-rad.org)
  6
+ * @license       http://opensource.org/licenses/bsd-license.php The BSD License
  7
+ */
  8
+
  9
+/**
  10
+ * Validation data for `fr_BE`.
  11
+ */
  12
+return array(
  13
+	'postalCode' => '/^[1-9]{1}[0-9]{3}$/i'
  14
+);
  15
+
  16
+?>
17  libraries/lithium/g11n/resources/php/fr_CA/validation/default.php
... ...
@@ -0,0 +1,17 @@
  1
+<?php
  2
+/**
  3
+ * Lithium: the most rad php framework
  4
+ *
  5
+ * @copyright     Copyright 2009, Union of RAD (http://union-of-rad.org)
  6
+ * @license       http://opensource.org/licenses/bsd-license.php The BSD License
  7
+ */
  8
+
  9
+/**
  10
+ * Validation data for `fr_CA`.
  11
+ */
  12
+return array(
  13
+	'phone' => '/^(?:\+?1)?[-. ]?\\(?[2-9][0-8][0-9]\\)?[-. ]?[2-9][0-9]{2}[-. ]?[0-9]{4}$/',
  14
+	'postalCode' => '/\\A\\b[ABCEGHJKLMNPRSTVXY][0-9][A-Z] [0-9][A-Z][0-9]\\b\\z/i'
  15
+);
  16
+
  17
+?>
16  libraries/lithium/g11n/resources/php/it_IT/validation/default.php
... ...
@@ -0,0 +1,16 @@
  1
+<?php
  2
+/**
  3
+ * Lithium: the most rad php framework
  4
+ *
  5
+ * @copyright     Copyright 2009, Union of RAD (http://union-of-rad.org)
  6
+ * @license       http://opensource.org/licenses/bsd-license.php The BSD License
  7
+ */
  8
+
  9
+/**
  10
+ * Validation data for `it_IT`.
  11
+ */
  12
+return array(
  13
+	'postalCode' => '/^[0-9]{5}$/i'
  14
+);
  15
+
  16
+?>
16  libraries/lithium/g11n/resources/php/nl_BE/validation/default.php
... ...
@@ -0,0 +1,16 @@
  1
+<?php
  2
+/**
  3
+ * Lithium: the most rad php framework
  4
+ *
  5
+ * @copyright     Copyright 2009, Union of RAD (http://union-of-rad.org)
  6
+ * @license       http://opensource.org/licenses/bsd-license.php The BSD License
  7
+ */
  8
+
  9
+/**
  10
+ * Validation data for `nl_BE`.
  11
+ */
  12
+return array(
  13
+	'postalCode' => '/^[1-9]{1}[0-9]{3}$/i'
  14
+);
  15
+
  16
+?>
16  libraries/lithium/g11n/resources/php/nl_NL/validation/default.php
... ...
@@ -0,0 +1,16 @@
  1
+<?php
  2
+/**
  3
+ * Lithium: the most rad php framework
  4
+ *
  5
+ * @copyright     Copyright 2009, Union of RAD (http://union-of-rad.org)
  6
+ * @license       http://opensource.org/licenses/bsd-license.php The BSD License
  7
+ */
  8
+
  9
+/**
  10
+ * Validation data for `nl_NL`.
  11
+ */
  12
+return array(
  13
+	'ssn' => '/\\A\\b[0-9]{9}\\b\\z/i'
  14
+);
  15
+
  16
+?>
22  libraries/lithium/tests/cases/util/ValidatorTest.php
@@ -30,10 +30,6 @@ public function testCustomMethodDispatching() {
30 30
 		$this->assertTrue(Validator::isUrl('google.com', 'loose'));
31 31
 		$this->assertTrue(Validator::isUrl('google.com'));
32 32
 		$this->assertFalse(Validator::isUrl('google.com', 'strict'));
33  
-
34  
-		$this->assertTrue(Validator::isSsn('478364120'));
35  
-		$this->assertTrue(Validator::isSsn('478-36-4120'));
36  
-		$this->assertFalse(Validator::isSsn('478-36-41200'));
37 33
 	}
38 34
 
39 35
 	/**
@@ -75,19 +71,15 @@ public function testStateReset() {
75 71
 	 * @return void
76 72
 	 */
77 73
 	public function testAddCustomRegexFormats() {
78  
-		$this->assertTrue(Validator::isPostalCode('11201'));
79  
-		$this->assertTrue(Validator::isPostalCode('11201-0456'));
80  
-
81  
-		$this->assertTrue(Validator::isPostalCode('11201', 'us'));
82  
-		$this->assertTrue(Validator::isPostalCode('11201-0456', 'us'));
  74
+		$this->assertTrue(Validator::isPhone('1234567890'));
  75
+		$this->assertTrue(Validator::isPhone('+1234567890'));
83 76
 
84  
-		$this->assertFalse(Validator::isPostalCode('foo-bar'));
85  
-		Validator::add(array('postalCode' => array('foo' => '/^foo-bar$/')));
  77
+		$this->assertFalse(Validator::isPhone('0800-LITHIUM'));
  78
+		Validator::add(array('phone' => array('foo' => '/^0800-[A-Z]+$/')));
86 79
 
87  
-		$this->assertTrue(Validator::isPostalCode('foo-bar'));
88  
-		$this->assertTrue(Validator::isPostalCode('foo-bar', 'foo'));
89  
-		$this->assertTrue(Validator::isPostalCode('foo-bar', 'any'));
90  
-		$this->assertFalse(Validator::isPostalCode('foo-bar', 'us'));
  80
+		$this->assertTrue(Validator::isPhone('0800-LITHIUM'));
  81
+		$this->assertTrue(Validator::isPhone('0800-LITHIUM', 'foo'));
  82
+		$this->assertTrue(Validator::isPhone('0800-LITHIUM', 'any'));
91 83
 	}
92 84
 
93 85
 	public function testPrefilterMethodAccess() {
137  libraries/lithium/tests/integration/g11n/ResourcesValidatorTest.php
... ...
@@ -0,0 +1,137 @@
  1
+<?php
  2
+/**
  3
+ * Lithium: the most rad php framework
  4
+ *
  5
+ * @copyright     Copyright 2009, Union of RAD (http://union-of-rad.org)
  6
+ * @license       http://opensource.org/licenses/bsd-license.php The BSD License
  7
+ */
  8
+
  9
+namespace lithium\tests\integration\g11n;
  10
+
  11
+use \lithium\g11n\Catalog;
  12
+use \lithium\g11n\catalog\adapter\Memory;
  13
+use \lithium\util\Validator;
  14
+
  15
+class ResourcesValidatorTest extends \lithium\test\Unit {
  16
+
  17
+	protected $_backups = array();
  18
+
  19
+	public function setUp() {
  20
+		$this->_backups['catalogConfig'] = Catalog::config();
  21
+		Catalog::reset();
  22
+		Catalog::config(array(
  23
+			'lithium' => array(
  24
+				'adapter' => 'Php',
  25
+				'path' => LITHIUM_LIBRARY_PATH . '/lithium/g11n/resources/php'
  26
+		)));
  27
+		Validator::__init();
  28
+	}
  29
+
  30
+	public function tearDown() {
  31
+		Catalog::reset();
  32
+		Catalog::config($this->_backups['catalogConfig']);
  33
+	}
  34
+
  35
+	public function testDaDk() {
  36
+		Validator::add(Catalog::read('validation', 'da_DK'));
  37
+
  38
+		$this->assertTrue(Validator::isSsn('123456-1234'));
  39
+		$this->assertFalse(Validator::isSsn('12345-1234'));
  40
+	}
  41
+
  42
+	public function testDeBe() {
  43
+		Validator::add(Catalog::read('validation', 'de_BE'));
  44
+
  45
+		$this->assertTrue(Validator::isPostalCode('1234'));
  46
+		$this->assertTrue(Validator::isPostalCode('1234'));
  47
+		$this->assertFalse(Validator::isPostalCode('0123'));
  48
+	}
  49
+
  50
+	public function testDeDe() {
  51
+		Validator::add(Catalog::read('validation', 'de_DE'));
  52
+
  53
+		$this->assertTrue(Validator::isPostalCode('12345'));
  54
+		$this->assertFalse(Validator::isPostalCode('123456'));
  55
+	}
  56
+
  57
+	public function testEnCa() {
  58
+		Validator::add(Catalog::read('validation', 'en_CA'));
  59
+
  60
+		$this->assertTrue(Validator::isPhone('(401) 321-9876'));
  61
+
  62
+		$this->assertTrue(Validator::isPostalCode('M5J 2G8'));
  63
+		$this->assertTrue(Validator::isPostalCode('H2X 3X5'));
  64
+	}
  65
+
  66
+	public function testEnGb() {
  67
+		Validator::add(Catalog::read('validation', 'en_GB'));
  68
+
  69
+		$this->assertTrue(Validator::isPostalCode('M1 1AA'));
  70
+		$this->assertTrue(Validator::isPostalCode('M60 1NW'));
  71
+		$this->assertTrue(Validator::isPostalCode('CR2 6XH'));
  72
+		$this->assertTrue(Validator::isPostalCode('DN55 1PT'));
  73
+		$this->assertTrue(Validator::isPostalCode('W1A 1HQ'));
  74
+		$this->assertTrue(Validator::isPostalCode('EC1A 1BB'));
  75
+		$this->assertTrue(Validator::isPostalCode('FK7 0AQ'));
  76
+		$this->assertTrue(Validator::isPostalCode('FK8 2ET'));
  77
+		$this->assertTrue(Validator::isPostalCode('FK8 1EB'));
  78
+		$this->assertTrue(Validator::isPostalCode('EH1 1QX'));
  79
+		$this->assertFalse(Validator::isPostalCode('EH1-1QX'));
  80
+		$this->assertFalse(Validator::isPostalCode('EH11QX'));
  81
+		$this->assertFalse(Validator::isPostalCode('FEH1 1QX'));
  82
+	}
  83
+
  84
+	public function testEnUs() {
  85
+		Validator::add(Catalog::read('validation', 'en_US'));
  86
+
  87
+		$this->assertTrue(Validator::isPhone('(401) 321-9876'));
  88
+
  89
+		$this->assertTrue(Validator::isPostalCode('11201'));
  90
+		$this->assertTrue(Validator::isPostalCode('11201-0456'));
  91
+
  92
+		$this->assertTrue(Validator::isSsn('478-36-4120'));
  93
+		$this->assertFalse(Validator::isSsn('478-36-41200'));
  94
+		$this->assertFalse(Validator::isSsn('478364120'));
  95
+	}
  96
+
  97
+	public function testFrBe() {
  98
+		Validator::add(Catalog::read('validation', 'fr_BE'));
  99
+
  100
+		$this->assertTrue(Validator::isPostalCode('1234'));
  101
+		$this->assertTrue(Validator::isPostalCode('1234'));
  102
+		$this->assertFalse(Validator::isPostalCode('0123'));
  103
+	}
  104
+
  105
+	public function testFrCa() {
  106
+		Validator::add(Catalog::read('validation', 'fr_CA'));
  107
+
  108
+		$this->assertTrue(Validator::isPhone('(401) 321-9876'));
  109
+
  110
+		$this->assertTrue(Validator::isPostalCode('M5J 2G8'));
  111
+		$this->assertTrue(Validator::isPostalCode('H2X 3X5'));
  112
+	}
  113
+
  114
+	public function testItIt() {
  115
+		Validator::add(Catalog::read('validation', 'it_IT'));
  116
+
  117
+		$this->assertTrue(Validator::isPostalCode('12345'));
  118
+		$this->assertFalse(Validator::isPostalCode('123456'));
  119
+	}
  120
+
  121
+	public function testNlBe() {
  122
+		Validator::add(Catalog::read('validation', 'nl_BE'));
  123
+
  124
+		$this->assertTrue(Validator::isPostalCode('1234'));
  125
+		$this->assertTrue(Validator::isPostalCode('1234'));
  126
+		$this->assertFalse(Validator::isPostalCode('0123'));
  127
+	}
  128
+
  129
+	public function testNlNl() {
  130
+		Validator::add(Catalog::read('validation', 'nl_NL'));
  131
+
  132
+		$this->assertTrue(Validator::isSsn('123456789'));
  133
+		$this->assertFalse(Validator::isSsn('12345678'));
  134
+	}
  135
+}
  136
+
  137
+?>
19  libraries/lithium/util/Validator.php
@@ -136,24 +136,9 @@ public static function __init() {
136 136
 				              '(?:\1\d{3})*|(?:\d+))((?!\1)[,.]\d{2})?$/u'
137 137
 			),
138 138
 			'notEmpty'     => '/[^\s]+/m',
139  
-			'phone'        => array(
140  
-				'us'       => '/^(?:\+?1)?[-. ]?\\(?[2-9][0-8][0-9]\\)?[-. ]?[2-9][0-9]{2}[-. ]' .
141  
-			                  '?[0-9]{4}$/',
142  
-			),
143  
-			'postalCode'   => array(
144  
-				'uk'       => '/\\A\\b[A-Z]{1,2}[0-9][A-Z0-9]? [0-9][ABD-HJLNP-UW-Z]{2}\\b\\z/i',
145  
-				'ca'       => '/\\A\\b[ABCEGHJKLMNPRSTVXY][0-9][A-Z] [0-9][A-Z][0-9]\\b\\z/i',
146  
-				'it'       => '/^[0-9]{5}$/i',
147  
-				'de'       => '/^[0-9]{5}$/i',
148  
-				'be'       => '/^[1-9]{1}[0-9]{3}$/i',
149  
-				'us'       => '/\\A\\b[0-9]{5}(?:-[0-9]{4})?\\b\\z/i'
150  
-			),
  139
+			'phone'        => '/^\+?[0-9\(\)\-]{10,20}$/',
  140
+			'postalCode'   => '/(^|\A\b)[A-Z0-9\s\-]{5,}($|\b\z)/i',
151 141
 			'regex'        => '/^\/(.+)\/[gimsxu]*$/',
152  
-			'ssn'          => array(
153  
-				'dk'       => '/\\A\\b[0-9]{6}-[0-9]{4}\\b\\z/i',
154  
-				'nl'       => '/\\A\\b[0-9]{9}\\b\\z/i',
155  
-				'us'       => '/\\A\\b[0-9]{3}-[0-9]{2}-[0-9]{4}\\b\\z/i'
156  
-			),
157 142
 			'time'         => '%^((0?[1-9]|1[012])(:[0-5]\d){0,2}([AP]M|[ap]m))$|^([01]\d|2[0-3])' .
158 143
 			                  '(:[0-5]\d){0,2}$%',
159 144
 			'boolean' => function($value) {

0 notes on commit 338196b

Please sign in to comment.
Something went wrong with that request. Please try again.