Permalink
Browse files

Merge branch '1.3' of github.com:cakephp/cakephp1x into 1.3

  • Loading branch information...
markstory committed Jan 7, 2010
2 parents be7ce6e + e2a2770 commit 683be26bcb37adc782f918ebee4b18817908016f
View
@@ -189,12 +189,12 @@ class Inflector {
var $_singularized = array();
/**
- * Cached Underscored Inflections
+ * Cached Underscore Inflections
*
* @var array
* @access protected
*/
- var $_underscored = array();
+ var $_underscore = array();
/**
* Cached Camelize Inflections
@@ -213,7 +213,7 @@ class Inflector {
var $_classify = array();
/**
- * Tablized cached inflections
+ * Tablize cached inflections
*
* @var array
* @access protected
@@ -243,6 +243,31 @@ function &getInstance() {
return $instance[0];
}
+/**
+ * Cache inflected values, and return if already available
+ *
+ * @param string $type Inflection type
+ * @param string $key Original value
+ * @param string $value Inflected value
+ * @return string Inflected value, from cache
+ * @access protected
+ */
+ function _cache($type, $key, $value = false) {
+ $_this =& Inflector::getInstance();
+
+ $key = '_' . $key;
+ $type = '_' . $type;
+ if ($value !== false) {
+ $_this->{$type}[$key] = $value;
+ return $value;
+ }
+
+ if (!isset($_this->{$type}[$key])) {
+ return false;
+ }
+ return $_this->{$type}[$key];
+ }
+
/**
* Adds custom inflection $rules, of either 'plural' or 'singular' $type.
*
@@ -379,11 +404,11 @@ function singularize($word) {
*/
function camelize($lowerCaseAndUnderscoredWord) {
$_this =& Inflector::getInstance();
-
- if (!isset($_this->_camelize[$lowerCaseAndUnderscoredWord])) {
- $_this->_camelize[$lowerCaseAndUnderscoredWord] = str_replace(" ", "", ucwords(str_replace("_", " ", $lowerCaseAndUnderscoredWord)));
+ if (!($result = $_this->_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord))) {
+ $result = str_replace(' ', '', Inflector::humanize($lowerCaseAndUnderscoredWord));
+ $_this->_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord, $result);
}
- return $_this->_camelize[$lowerCaseAndUnderscoredWord];
+ return $result;
}
/**
@@ -397,11 +422,11 @@ function camelize($lowerCaseAndUnderscoredWord) {
*/
function underscore($camelCasedWord) {
$_this =& Inflector::getInstance();
-
- if (!isset($_this->_underscored[$camelCasedWord])) {
- $_this->_underscored[$camelCasedWord] = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $camelCasedWord));
+ if (!($result = $_this->_cache(__FUNCTION__, $camelCasedWord))) {
+ $result = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $camelCasedWord));
+ $_this->_cache(__FUNCTION__, $camelCasedWord, $result);
}
- return $_this->_underscored[$camelCasedWord];
+ return $result;
}
/**
@@ -416,11 +441,11 @@ function underscore($camelCasedWord) {
*/
function humanize($lowerCaseAndUnderscoredWord) {
$_this =& Inflector::getInstance();
-
- if (!isset($_this->_humanize[$lowerCaseAndUnderscoredWord])) {
- $_this->_humanize[$lowerCaseAndUnderscoredWord] = ucwords(str_replace("_", " ", $lowerCaseAndUnderscoredWord));
+ if (!($result = $_this->_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord))) {
+ $result = ucwords(str_replace('_', ' ', $lowerCaseAndUnderscoredWord));
+ $_this->_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord, $result);
}
- return $_this->_humanize[$lowerCaseAndUnderscoredWord];
+ return $result;
}
/**
@@ -434,11 +459,11 @@ function humanize($lowerCaseAndUnderscoredWord) {
*/
function tableize($className) {
$_this =& Inflector::getInstance();
-
- if (!isset($_this->_tableize[$className])) {
- $_this->_tableize[$className] = Inflector::pluralize(Inflector::underscore($className));
+ if (!($result = $_this->_cache(__FUNCTION__, $className))) {
+ $result = Inflector::pluralize(Inflector::underscore($className));
+ $_this->_cache(__FUNCTION__, $className, $result);
}
- return $_this->_tableize[$className];
+ return $result;
}
/**
@@ -452,11 +477,11 @@ function tableize($className) {
*/
function classify($tableName) {
$_this =& Inflector::getInstance();
-
- if (!isset($_this->_classify[$tableName])) {
- $_this->_classify[$tableName] = Inflector::camelize(Inflector::singularize($tableName));
+ if (!($result = $_this->_cache(__FUNCTION__, $tableName))) {
+ $result = Inflector::camelize(Inflector::singularize($tableName));
+ $_this->_cache(__FUNCTION__, $tableName, $result);
}
- return $_this->_classify[$tableName];
+ return $result;
}
/**
@@ -470,13 +495,13 @@ function classify($tableName) {
*/
function variable($string) {
$_this =& Inflector::getInstance();
-
- if (!isset($_this->_variable[$string])) {
- $string = Inflector::camelize(Inflector::underscore($string));
- $replace = strtolower(substr($string, 0, 1));
- $_this->_variable[$string] = preg_replace('/\\w/', $replace, $string, 1);
+ if (!($result = $_this->_cache(__FUNCTION__, $string))) {
+ $string2 = Inflector::camelize(Inflector::underscore($string));
+ $replace = strtolower(substr($string2, 0, 1));
+ $result = preg_replace('/\\w/', $replace, $string2, 1);
+ $_this->_cache(__FUNCTION__, $string, $result);
}
- return $_this->_variable[$string];
+ return $result;
}
/**
@@ -81,8 +81,20 @@ class DboPostgres extends DboSource {
'inet' => array('name' => 'inet')
);
+/**
+ * Starting Quote
+ *
+ * @var string
+ * @access public
+ */
var $startQuote = '"';
+/**
+ * Ending Quote
+ *
+ * @var string
+ * @access public
+ */
var $endQuote = '"';
/**
View
@@ -541,22 +541,63 @@ function extension($check, $extensions = array('gif', 'jpeg', 'png', 'jpg')) {
}
/**
- * Validation of an IPv4 address.
+ * Validation of an IP address.
+ *
+ * Valid IP version strings for type restriction are:
+ * - both: Check both IPv4 and IPv6, return true if the supplied address matches either version
+ * - IPv4: Version 4 (Eg: 127.0.0.1, 192.168.10.123, 203.211.24.8)
+ * - IPv6: Version 6 (Eg: ::1, 2001:0db8::1428:57ab)
*
* @param string $check The string to test.
+ * @param string $type The IP Version to test against
* @return boolean Success
* @access public
*/
- function ip($check, $type = 'IPv4') {
+ function ip($check, $type = 'both') {
+ $_this =& Validation::getInstance();
+ $success = false;
+ $type = strtolower($type);
+ if ($type === 'ipv4' || $type === 'both') {
+ $success |= $_this->_ipv4($check);
+ }
+ if ($type === 'ipv6' || $type === 'both') {
+ $success |= $_this->_ipv6($check);
+ }
+ return $success;
+ }
+
+/**
+ * Validation of IPv4 addresses.
+ *
+ * @param string $check IP Address to test
+ * @return boolean Success
+ * @access protected
+ */
+ function _ipv4($check) {
if (function_exists('filter_var')) {
- return (boolean) filter_var($check, FILTER_VALIDATE_IP);
+ return filter_var($check, FILTER_VALIDATE_IP, array('flags' => FILTER_FLAG_IPV4)) !== false;
}
+ $this->__populateIp();
+ $this->check = $check;
+ $this->regex = '/^' . $this->__pattern['IPv4'] . '$/';
+ return $this->_check();
+ }
- $_this =& Validation::getInstance();
- $_this->check = $check;
- $_this->__populateIp();
- $_this->regex = '/^' . $_this->__pattern[$type] . '$/';
- return $_this->_check();
+/**
+ * Validation of IPv6 addresses.
+ *
+ * @param string $check IP Address to test
+ * @return boolean Success
+ * @access protected
+ */
+ function _ipv6($check) {
+ if (function_exists('filter_var')) {
+ return filter_var($check, FILTER_VALIDATE_IP, array('flags' => FILTER_FLAG_IPV6)) !== false;
+ }
+ $this->__populateIp();
+ $this->check = $check;
+ $this->regex = '/^' . $this->__pattern['IPv6'] . '$/';
+ return $this->_check();
}
/**
@@ -816,7 +857,8 @@ function url($check, $strict = false) {
$_this->check = $check;
$validChars = '([' . preg_quote('!"$&\'()*+,-.@_:;=~') . '\/0-9a-z]|(%[0-9a-f]{2}))';
$_this->regex = '/^(?:(?:https?|ftps?|file|news|gopher):\/\/)' . (!empty($strict) ? '' : '?') .
- '(?:' . $_this->__pattern['IPv4'] . '|' . $_this->__pattern['hostname'] . ')(?::[1-9][0-9]{0,3})?' .
+ '(?:' . $_this->__pattern['IPv4'] . '|\[' . $_this->__pattern['IPv6'] . '\]|' . $_this->__pattern['hostname'] . ')' .
+ '(?::[1-9][0-9]{0,4})?' .
'(?:\/?|\/' . $validChars . '*)?' .
'(?:\?' . $validChars . '*)?' .
'(?:#' . $validChars . '*)?$/i';
@@ -954,6 +996,7 @@ function _luhn() {
* Lazily popualate the IP address patterns used for validations
*
* @return void
+ * @access private
*/
function __populateIp() {
if (!isset($this->__pattern['IPv6'])) {
@@ -1725,13 +1725,16 @@ function testEqualTo() {
* @access public
* @return void
*/
- function testIp() {
- $this->assertTrue(Validation::ip('0.0.0.0'));
- $this->assertTrue(Validation::ip('192.168.1.156'));
- $this->assertTrue(Validation::ip('255.255.255.255'));
- $this->assertFalse(Validation::ip('127.0.0'));
- $this->assertFalse(Validation::ip('127.0.0.a'));
- $this->assertFalse(Validation::ip('127.0.0.256'));
+ function testIpv4() {
+ $this->assertTrue(Validation::ip('0.0.0.0', 'IPv4'));
+ $this->assertTrue(Validation::ip('192.168.1.156', 'IPv4'));
+ $this->assertTrue(Validation::ip('255.255.255.255', 'IPv4'));
+
+ $this->assertFalse(Validation::ip('127.0.0', 'IPv4'));
+ $this->assertFalse(Validation::ip('127.0.0.a', 'IPv4'));
+ $this->assertFalse(Validation::ip('127.0.0.256', 'IPv4'));
+
+ $this->assertFalse(Validation::ip('2001:0db8:85a3:0000:0000:8a2e:0370:7334', 'IPv4'));
}
/**
@@ -1772,6 +1775,32 @@ function testIpv6() {
$this->assertFalse(Validation::ip('1:2:3::4:5:6:7:8:9', 'IPv6'));
$this->assertFalse(Validation::ip('::ffff:2.3.4', 'IPv6'));
$this->assertFalse(Validation::ip('::ffff:257.1.2.3', 'IPv6'));
+
+ $this->assertFalse(Validation::ip('0.0.0.0', 'IPv6'));
+ }
+
+/**
+ * testIpBoth method
+ *
+ * @return void
+ * @access public
+ */
+ function testIpBoth() {
+ $this->assertTrue(Validation::ip('0.0.0.0'));
+ $this->assertTrue(Validation::ip('192.168.1.156'));
+ $this->assertTrue(Validation::ip('255.255.255.255'));
+
+ $this->assertFalse(Validation::ip('127.0.0'));
+ $this->assertFalse(Validation::ip('127.0.0.a'));
+ $this->assertFalse(Validation::ip('127.0.0.256'));
+
+ $this->assertTrue(Validation::ip('2001:0db8:85a3:0000:0000:8a2e:0370:7334'));
+ $this->assertTrue(Validation::ip('2001:db8:85a3:0:0:8a2e:370:7334'));
+ $this->assertTrue(Validation::ip('2001:db8:85a3::8a2e:370:7334'));
+
+ $this->assertFalse(Validation::ip('2001:db8:85a3::8a2e:37023:7334'));
+ $this->assertFalse(Validation::ip('2001:db8:85a3::8a2e:370k:7334'));
+ $this->assertFalse(Validation::ip('1:2:3:4:5:6:7:8:9'));
}
/**
@@ -1847,6 +1876,23 @@ function testUrl() {
$this->assertFalse(Validation::url('www.cakephp.org', true));
$this->assertTrue(Validation::url('http://www.cakephp.org', true));
$this->assertTrue(Validation::url('http://example.com/~userdir/'));
+
+ $this->assertTrue(Validation::url('http://cakephp.org:80'));
+ $this->assertTrue(Validation::url('http://cakephp.org:443'));
+ $this->assertTrue(Validation::url('http://cakephp.org:2000'));
+ $this->assertTrue(Validation::url('http://cakephp.org:27000'));
+ $this->assertTrue(Validation::url('http://cakephp.org:65000'));
+
+ $this->assertTrue(Validation::url('[2001:0db8::1428:57ab]'));
+ $this->assertTrue(Validation::url('[::1]'));
+ $this->assertTrue(Validation::url('[2001:0db8::1428:57ab]:80'));
+ $this->assertTrue(Validation::url('[::1]:80'));
+ $this->assertTrue(Validation::url('http://[2001:0db8::1428:57ab]'));
+ $this->assertTrue(Validation::url('http://[::1]'));
+ $this->assertTrue(Validation::url('http://[2001:0db8::1428:57ab]:80'));
+ $this->assertTrue(Validation::url('http://[::1]:80'));
+
+ $this->assertFalse(Validation::url('[1::2::3]'));
}
/**

0 comments on commit 683be26

Please sign in to comment.