[Solar-svn] Revision 2841
pmjones at solarphp.com
pmjones at solarphp.com
Sat Oct 6 12:42:34 CDT 2007
removed deleted and renamed classes
Deleted: trunk/Solar/Content.php
===================================================================
--- trunk/Solar/Content.php 2007-10-06 16:35:03 UTC (rev 2840)
+++ trunk/Solar/Content.php 2007-10-06 17:42:34 UTC (rev 2841)
@@ -1,93 +0,0 @@
-<?php
-/**
- *
- * Generic content management class.
- *
- * @category Solar
- *
- * @package Solar_Content
- *
- * @author Paul M. Jones <pmjones at solarphp.com>
- *
- * @license http://opensource.org/licenses/bsd-license.php BSD
- *
- * @version $Id$
- *
- */
-
-/**
- *
- * Generic content management class.
- *
- * @category Solar
- *
- * @package Solar_Content
- *
- * @todo Build in content permission system.
- *
- */
-class Solar_Content extends Solar_Base {
-
- /**
- *
- * User-provided configuration values.
- *
- * Keys are ...
- *
- * `sql`
- * : (dependency) A Solar_Sql dependency injection, passed
- * into the table objects at creation time.
- *
- * @var array
- *
- */
- protected $_Solar_Content = array(
- 'sql' => 'sql',
- 'areas' => 'Solar_Model_Areas',
- 'nodes' => 'Solar_Model_Nodes',
- 'tags' => 'Solar_Model_Tags',
- );
-
- /**
- *
- * A table object representing the broad areas of content.
- *
- * @var Solar_Model_Areas
- *
- */
- public $areas;
-
- /**
- *
- * A table object representing the container nodes in an area.
- *
- * @var Solar_Model_Nodes
- *
- */
- public $nodes;
-
- /**
- *
- * A table object representing the searchable tags on each node.
- *
- * @var Solar_Model_Tags
- *
- */
- public $tags;
-
- /**
- *
- * Constructor.
- *
- * @param array $config User-provided configuration values.
- *
- */
- public function __construct($config = null)
- {
- parent::__construct($config);
- $model_config = array('sql' => $this->_config['sql']);
- $this->areas = Solar::dependency($this->_config['areas'], $model_config);
- $this->nodes = Solar::dependency($this->_config['nodes'], $model_config);
- $this->tags = Solar::dependency($this->_config['tags'], $model_config);
- }
-}
Deleted: trunk/Solar/DataFilter.php
===================================================================
--- trunk/Solar/DataFilter.php 2007-10-06 16:35:03 UTC (rev 2840)
+++ trunk/Solar/DataFilter.php 2007-10-06 17:42:34 UTC (rev 2841)
@@ -1,1377 +0,0 @@
-<?php
-/**
- *
- * Methods for validating and sanitizing user input.
- *
- * @category Solar
- *
- * @package Solar
- *
- * @author Paul M. Jones <pmjones at solarphp.com>
- *
- * @author Matthew Weier O'Phinney <mweierophinney at gmail.com>
- *
- * @license http://opensource.org/licenses/bsd-license.php BSD
- *
- * @version $Id$
- *
- */
-
-/**
- *
- * Methods for validating and sanitizing user input.
- *
- * @category Solar
- *
- * @package Solar
- *
- * @todo convert Ipv6() and Ip() to userland, not ext/filter
- *
- */
-class Solar_DataFilter extends Solar_Base {
-
- /**
- *
- * String representations of "true" boolean values.
- *
- * @var array
- *
- */
- protected $_true = array('1', 'on', 'true', 't', 'yes', 'y');
-
- /**
- *
- * String representations of "false" boolean values.
- *
- * @var array
- *
- */
- protected $_false = array('0', 'off', 'false', 'f', 'no', 'n', '');
-
- /**
- *
- * Are values required to be not-blank?
- *
- * For validate methods, when $_require is true, the value must be
- * non-blank for it to validate; when false, blank values are considered
- * valid.
- *
- * For sanitize methods, when $_require is true, the method will attempt
- * to sanitize blank values; when false, the method will return blank
- * values as nulls.
- *
- * @var bool
- *
- * @see setRequire()
- *
- * @see getRequire()
- *
- */
- protected $_require = true;
-
- /**
- *
- * Sets the value of the 'require' flag.
- *
- * @param bool $flag Turn 'require' on (true) or off (false).
- *
- * @return void
- *
- * @see $_require
- *
- */
- public function setRequire($flag)
- {
- $this->_require = (bool) $flag;
- }
-
- /**
- *
- * Returns the value of the 'require' flag.
- *
- * @return bool
- *
- * @see $_require
- *
- */
- public function getRequire()
- {
- return $this->_require;
- }
-
- // -----------------------------------------------------------------
- //
- // Sanitize
- //
- // -----------------------------------------------------------------
-
- /**
- *
- * Forces the value to a boolean.
- *
- * Note that this recognizes $this->_true and $this->_false values.
- *
- * @param mixed $value The value to sanitize.
- *
- * @return bool The sanitized value.
- *
- */
- public function sanitizeBool($value)
- {
- if (! $this->_require && $this->validateBlank($value)) {
- return null;
- }
-
- // PHP booleans
- if ($value === true || $value === false) {
- return $value;
- }
-
- // "string" booleans
- $value = strtolower(trim($value));
- if (in_array($value, $this->_true)) {
- return true;
- }
- if (in_array($value, $this->_false)) {
- return false;
- }
-
- // forcibly recast to a boolean
- return (bool) $value;
- }
-
- /**
- *
- * Forces the value to a float.
- *
- * Attempts to extract a valid float from the given value, using an
- * algorithm somewhat less naive that "remove all characters that are not
- * '0-9.,eE+-'". The result may not be expected, but it will be a float.
- *
- * @param mixed $value The value to be sanitized.
- *
- * @return float The sanitized value.
- *
- * @todo Extract scientific notation from weird strings?
- *
- */
- public function sanitizeFloat($value)
- {
- if (! $this->_require && $this->validateBlank($value)) {
- return null;
- }
-
- if (! is_string($value) || is_numeric($value)) {
- return (float) $value;
- }
-
- // it's a non-numeric string, attempt to extract a float from it.
-
- // remove all + signs; any - sign takes precedence because ...
- // 0 + -1 = -1
- // 0 - +1 = -1
- // ... at least it seems that way to me now.
- $value = str_replace('+', '', $value);
-
- // reduce multiple decimals and minuses
- $value = preg_replace('/[\.-]{2,}/', '.', $value);
-
- // remove all decimals without a digit or minus next to them
- $value = preg_replace('/([^0-9-]\.[^0-9])/', '', $value);
-
- // remove all chars except digit, decimal, and minus
- $value = preg_replace('/[^0-9\.-]/', '', $value);
-
- // remove all trailing decimals and minuses
- $value = rtrim($value, '.-');
-
- // pre-empt further checks if already empty
- if ($value == '') {
- return (float) $value;
- }
-
- // remove all minuses not at the front
- $is_negative = ($value[0] == '-');
- $value = str_replace('-', '', $value);
- if ($is_negative) {
- $value = '-' . $value;
- }
-
- // remove all decimals but the first
- $pos = strpos($value, '.');
- $value = str_replace('.', '', $value);
- if ($pos !== false) {
- $value = substr($value, 0, $pos)
- . '.'
- . substr($value, $pos);
- }
-
- // looks like we're done
- return (float) $value;
- }
-
- /**
- *
- * Forces the value to an integer.
- *
- * Attempts to extract a valid integer from the given value, using an
- * algorithm somewhat less naive that "remove all characters that are not
- * '0-9+-'". The result may not be expected, but it will be a integer.
- *
- * @param mixed $value The value to be sanitized.
- *
- * @return int The sanitized value.
- *
- */
- public function sanitizeInt($value)
- {
- if (! $this->_require && $this->validateBlank($value)) {
- return null;
- }
-
- if (! is_string($value) || is_numeric($value)) {
- // we double-cast here to honor scientific notation.
- // (int) 1E5 == 1, but (int) (float) 1E5 == 100000
- return (int) (float) $value;
- }
-
- // it's a non-numeric string, attempt to extract an integer from it.
-
- // remove all chars except digit and minus.
- // this removes all + signs; any - sign takes precedence because ...
- // 0 + -1 = -1
- // 0 - +1 = -1
- // ... at least it seems that way to me now.
- $value = preg_replace('/[^0-9-]/', '', $value);
-
- // remove all trailing minuses
- $value = rtrim($value, '-');
-
- // pre-empt further checks if already empty
- if ($value == '') {
- return (int) $value;
- }
-
- // remove all minuses not at the front
- $is_negative = ($value[0] == '-');
- $value = str_replace('-', '', $value);
- if ($is_negative) {
- $value = '-' . $value;
- }
-
- // looks like we're done
- return (int) $value;
- }
-
- /**
- *
- * Forces the value to an IPv4 address.
- *
- * @param mixed $value The value to be sanitized.
- *
- * @return string The sanitized value.
- *
- */
- public function sanitizeIpv4($value)
- {
- if (! $this->_require && $this->validateBlank($value)) {
- return null;
- }
-
- return long2ip(ip2long($value));
- }
-
- /**
- *
- * Forces the value to an ISO-8601 formatted date ("yyyy-mm-dd").
- *
- * @param string $value The value to be sanitized. If an integer, it
- * is used as a Unix timestamp; otherwise, converted to a Unix
- * timestamp using [[php::strtotime() | ]].
- *
- * @return string The sanitized value.
- *
- */
- public function sanitizeIsoDate($value)
- {
- if (! $this->_require && $this->validateBlank($value)) {
- return null;
- }
-
- $format = 'Y-m-d';
- if ($this->validateInt($value, true)) {
- return date($format, $value);
- } else {
- return date($format, strtotime($value));
- }
- }
-
- /**
- *
- * Forces the value to an ISO-8601 formatted time ("hh:ii:ss").
- *
- * @param string $value The value to be sanitized. If an integer, it
- * is used as a Unix timestamp; otherwise, converted to a Unix
- * timestamp using [[php::strtotime() | ]].
- *
- * @return string The sanitized value.
- *
- */
- public function sanitizeIsoTime($value)
- {
- if (! $this->_require && $this->validateBlank($value)) {
- return null;
- }
-
- $format = 'H:i:s';
- if (is_int($value)) {
- return date($format, $value);
- } else {
- return date($format, strtotime($value));
- }
- }
-
- /**
- *
- * Forces the value to an ISO-8601 formatted timestamp
- * ("yyyy-mm-ddThh:ii:ss").
- *
- * @param string $value The value to be sanitized. If an integer, it
- * is used as a Unix timestamp; otherwise, converted to a Unix
- * timestamp using [[php::strtotime() | ]].
- *
- * @return string The sanitized value.
- *
- */
- public function sanitizeIsoTimestamp($value)
- {
- if (! $this->_require && $this->validateBlank($value)) {
- return null;
- }
-
- $format = 'Y-m-d\\TH:i:s';
- if (is_int($value)) {
- return date($format, $value);
- } else {
- return date($format, strtotime($value));
- }
- }
-
- /**
- *
- * Forces the value to a numeric string.
- *
- * @param mixed $value The value to be sanitized.
- *
- * @return int The sanitized value.
- *
- */
- public function sanitizeNumeric($value)
- {
- return (string) $this->sanitizeFloat($value);
- }
-
- /**
- *
- * Forces the value to a string; characters are not stripped or encoded.
- *
- * @param mixed $value The value to be sanitized.
- *
- * @return string The sanitized value.
- *
- */
- public function sanitizeString($value)
- {
- if (! $this->_require && $this->validateBlank($value)) {
- return null;
- }
-
- return (string) $value;
- }
-
- /**
- *
- * Strips non-alphabetic characters from the value.
- *
- * @param mixed $value The value to be sanitized.
- *
- * @return string The sanitized value.
- *
- */
- public function sanitizeAlpha($value)
- {
- if (! $this->_require && $this->validateBlank($value)) {
- return null;
- }
-
- return preg_replace('/[^a-z]/i', '', $value);
- }
-
- /**
- *
- * Strips non-alphanumeric characters from the value.
- *
- * @param mixed $value The value to be sanitized.
- *
- * @return string The sanitized value.
- *
- */
- public function sanitizeAlnum($value)
- {
- if (! $this->_require && $this->validateBlank($value)) {
- return null;
- }
-
- return preg_replace('/[^a-z0-9]/i', '', $value);
- }
-
- /**
- *
- * Applies [[php::preg_replace() | ]] to the value.
- *
- * @param mixed $value The value to be sanitized.
- *
- * @param string $pattern The regex pattern to apply.
- *
- * @param string $replace Replace the found pattern with this string.
- *
- * @return string The sanitized value.
- *
- */
- public function sanitizeRegex($value, $pattern, $replace)
- {
- if (! $this->_require && $this->validateBlank($value)) {
- return null;
- }
-
- return preg_replace($pattern, $replace, $value);
- }
-
- /**
- *
- * Applies [[php::str_replace() | ]] to the value.
- *
- * @param mixed $value The value to be sanitized.
- *
- * @param string $find Find this string.
- *
- * @param string $replace Replace with this string.
- *
- * @return string The sanitized value.
- *
- */
- public function sanitizeReplace($value, $find, $replace)
- {
- if (! $this->_require && $this->validateBlank($value)) {
- return null;
- }
-
- return str_replace($find, $replace, $value);
- }
-
- /**
- *
- * Trims characters from the beginning and end of the value.
- *
- * @param mixed $value The value to be sanitized.
- *
- * @param string $chars Trim these characters (default space).
- *
- * @return string The sanitized value.
- *
- */
- public function sanitizeTrim($value, $chars = ' ')
- {
- if (! $this->_require && $this->validateBlank($value)) {
- return null;
- }
-
- return trim($value, $chars);
- }
-
- /**
- *
- * Strips non-word characters within the value.
- *
- * @param mixed $value The value to be sanitized.
- *
- * @return string The sanitized value.
- *
- */
- public function sanitizeWord($value)
- {
- if (! $this->_require && $this->validateBlank($value)) {
- return null;
- }
-
- return preg_replace('/\W/', '', $value);
- }
-
- // -----------------------------------------------------------------
- //
- // Validate
- //
- // -----------------------------------------------------------------
-
- /**
- *
- * Validates that the value is only letters (upper or lower case) and digits.
- *
- * @param mixed $value The value to validate.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function validateAlnum($value)
- {
- if ($this->validateBlank($value)) {
- return ! $this->_require;
- }
-
- return ctype_alnum((string)$value);
- }
-
- /**
- *
- * Validates that the value is letters only (upper or lower case).
- *
- * @param mixed $value The value to validate.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function validateAlpha($value)
- {
- if ($this->validateBlank($value)) {
- return ! $this->_require;
- }
-
- return ctype_alpha($value);
- }
-
- /**
- *
- * Validates that the value is null, or is a string composed only of
- * whitespace.
- *
- * Ignores the [[$_require]] setting.
- *
- * Non-strings and non-nulls never validate as blank; this includes
- * integers, floats, numeric zero, boolean true and false, any array with
- * zero or more elements, and all objects and resources.
- *
- * @param mixed $value The value to validate.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function validateBlank($value)
- {
- if (! is_string($value) && ! is_null($value)) {
- return false;
- }
-
- return trim($value) == '';
- }
-
- /**
- *
- * Validates that the value is a boolean representation.
- *
- * @param mixed $value The value to validate.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function validateBool($value)
- {
- // need to allow for blanks if not required, because
- // empty strings are boolean false, and strings composed of blanks
- // are boolean true.
- if ($this->validateBlank($value) && ! $this->_require) {
- return true;
- }
-
- // PHP booleans
- if ($value === true || $value === false) {
- return true;
- }
-
- // "string" booleans
- $value = strtolower(trim($value));
- if (in_array($value, $this->_true, true) ||
- in_array($value, $this->_false, true)) {
- return true;
- }
-
- return false;
- }
-
- /**
- *
- * Validates the value against a [[php::ctype | ]] function.
- *
- * @param mixed $value The value to validate.
- *
- * @param string $type The ctype to validate against: 'alnum',
- * 'alpha', 'digit', etc.
- *
- * @return bool True if the value matches the ctype, false if not.
- *
- */
- public function validateCtype($value, $type)
- {
- if ($this->validateBlank($value)) {
- return ! $this->_require;
- }
-
- $func = 'ctype_' . $type;
- return (bool) $func((string)$value);
- }
-
- /**
- *
- * Validates that the value is an email address.
- *
- * Taken directly from <http://www.iamcal.com/publish/articles/php/parsing_email/>.
- *
- * @param mixed $value The value to validate.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function validateEmail($value)
- {
- if ($this->validateBlank($value)) {
- return ! $this->_require;
- }
-
- $qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
-
- $dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
-
- $atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c'.
- '\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
-
- $quoted_pair = '\\x5c[\\x00-\\x7f]';
-
- $domain_literal = "\\x5b($dtext|$quoted_pair)*\\x5d";
-
- $quoted_string = "\\x22($qtext|$quoted_pair)*\\x22";
-
- $domain_ref = $atom;
-
- $sub_domain = "($domain_ref|$domain_literal)";
-
- $word = "($atom|$quoted_string)";
-
- $domain = "$sub_domain(\\x2e$sub_domain)*";
-
- $local_part = "$word(\\x2e$word)*";
-
- $addr_spec = "$local_part\\x40$domain";
-
- return (bool) preg_match("!^$addr_spec$!D", $value);
- }
-
- /**
- *
- * Validates that the value represents a float.
- *
- * @param mixed $value The value to validate.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function validateFloat($value)
- {
- if ($this->validateBlank($value)) {
- return ! $this->_require;
- }
-
- if (is_float($value)) {
- return true;
- }
-
- // otherwise, must be numeric, and must be same as when cast to float
- return is_numeric($value) && $value == (float) $value;
- }
-
- /**
- *
- * Validates that the value is a key in the list of allowed options.
- *
- * Given an array (second parameter), the value (first parameter) must
- * match at least one of the array keys.
- *
- * @param mixed $value The value to validate.
- *
- * @param array $array An array of allowed options.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function validateInKeys($value, $array)
- {
- if ($this->validateBlank($value)) {
- return ! $this->_require;
- }
-
- return array_key_exists($value, (array) $array);
- }
-
- /**
- *
- * Validates that the value is in a list of allowed values.
- *
- * Strict checking is enforced, so a string "1" is not the same as
- * an integer 1. This helps to avoid matching 0 and empty, etc.
- *
- * @param mixed $value The value to validate.
- *
- * @param array $array An array of allowed values.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function validateInList($value, $array)
- {
- if ($this->validateBlank($value)) {
- return ! $this->_require;
- }
-
- return in_array($value, (array) $array, true);
- }
-
- /**
- *
- * Validates that the value represents an integer.
- *
- * @param mixed $value The value to validate.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function validateInt($value)
- {
- if ($this->validateBlank($value)) {
- return ! $this->_require;
- }
-
- if (is_int($value)) {
- return true;
- }
-
- // otherwise, must be numeric, and must be same as when cast to int
- return is_numeric($value) && $value == (int) $value;
- }
-
- /**
- *
- * Validates that the value is a legal IP address.
- *
- * Currently validates only IPv4; in future versions, will validate both
- * IPv4 and IPv6.
- *
- * @param mixed $value The value to validate.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function validateIp($value)
- {
- if ($this->validateBlank($value)) {
- return ! $this->_require;
- }
-
- return $this->validateIpv4($value);
- }
-
- /**
- *
- * Validates that the value is a legal IPv4 address.
- *
- * @param mixed $value The value to validate.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function validateIpv4($value)
- {
- if ($this->validateBlank($value)) {
- return ! $this->_require;
- }
-
- $result = ip2long($value);
- if ($result == -1 || $result === false) {
- return false;
- } else {
- return true;
- }
- }
-
- /**
- *
- * Validates that the value is an ISO 8601 date string.
- *
- * The format is "yyyy-mm-dd". Also checks to see that the date
- * itself is valid (for example, no Feb 30).
- *
- * @param mixed $value The value to validate.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function validateIsoDate($value)
- {
- if ($this->validateBlank($value)) {
- return ! $this->_require;
- }
-
- // basic date format
- // yyyy-mm-dd
- $expr = '/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/D';
-
- // validate
- if (preg_match($expr, $value, $match) &&
- checkdate($match[2], $match[3], $match[1])) {
- return true;
- } else {
- return false;
- }
- }
-
- /**
- *
- * Validates that the value is an ISO 8601 time string (hh:ii::ss format).
- *
- * Per note from Chris Drozdowski about ISO 8601, allows two
- * midnight times ... 00:00:00 for the beginning of the day, and
- * 24:00:00 for the end of the day.
- *
- * @param mixed $value The value to validate.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function validateIsoTime($value)
- {
- $expr = '/^(([0-1][0-9])|(2[0-3])):[0-5][0-9]:[0-5][0-9]$/D';
-
- return $this->validateRegex($value, $expr) ||
- ($value == '24:00:00');
- }
-
- /**
- *
- * Validates that the value is an ISO 8601 timestamp string.
- *
- * The format is "yyyy-mm-ddThh:ii:ss" (note the literal "T" in the
- * middle, which acts as a separator -- may also be a space).
- *
- * Also checks that the date itself is valid (for example, no Feb 30).
- *
- * @param mixed $value The value to validate.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function validateIsoTimestamp($value)
- {
- if ($this->validateBlank($value)) {
- return ! $this->_require;
- }
-
- // correct length?
- if (strlen($value) != 19) {
- return false;
- }
-
- // valid date?
- $date = substr($value, 0, 10);
- if (! $this->validateIsoDate($date)) {
- return false;
- }
-
- // valid separator?
- $sep = substr($value, 10, 1);
- if ($sep != 'T' && $sep != ' ') {
- return false;
- }
-
- // valid time?
- $time = substr($value, 11, 8);
- if (! $this->validateIsoTime($time)) {
- return false;
- }
-
- // must be ok
- return true;
- }
-
- /**
- *
- * Validates that the value is a locale code.
- *
- * The format is two lower-case letters, an underscore, and two upper-case
- * letters.
- *
- * @param mixed $value The value to validate.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function validateLocaleCode($value)
- {
- $expr = '/^[a-z]{2}_[A-Z]{2}$/D';
- return $this->validateRegex($value, $expr);
- }
-
- /**
- *
- * Validates that the value is less than than or equal to a maximum.
- *
- * @param mixed $value The value to validate.
- *
- * @param mixed $max The maximum valid value.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function validateMax($value, $max)
- {
- if ($this->validateBlank($value)) {
- return ! $this->_require;
- }
-
- return is_numeric($value) && $value <= $max;
- }
-
- /**
- *
- * Validates that a string is no longer than a certain length.
- *
- * @param mixed $value The value to validate.
- *
- * @param mixed $max The value must have no more than this many
- * characters.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function validateMaxLength($value, $max)
- {
- // reverse the normal check for blankness so that blank strings
- // are not checked for length.
- if ($this->_require && $this->validateBlank($value)) {
- return false;
- }
-
- return strlen($value) <= $max;
- }
-
- /**
- *
- * Validates that the value is formatted as a MIME type.
- *
- * @param mixed $value The value to validate.
- *
- * @param array $allowed The MIME type must be one of these
- * allowed values; if null, then all values are allowed.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function validateMimeType($value, $allowed = null)
- {
- // basically, anything like 'text/plain' or
- // 'application/vnd.ms-powerpoint' or
- // 'text/xml+xhtml'
- $word = '[a-zA-Z][\-\.a-zA-Z0-9+]*';
- $expr = '|^' . $word . '/' . $word . '$|D';
- $ok = $this->validateRegex($value, $expr);
- $allowed = (array) $allowed;
- if ($ok && count($allowed) > 0) {
- $ok = in_array($value, $allowed);
- }
- return $ok;
- }
-
- /**
- *
- * Validates that the value is greater than or equal to a minimum.
- *
- * @param mixed $value The value to validate.
- *
- * @param mixed $min The minimum valid value.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function validateMin($value, $min)
- {
- if ($this->validateBlank($value)) {
- return ! $this->_require;
- }
-
- return is_numeric($value) && $value >= $min;
- }
-
- /**
- *
- * Validates that a string is at least a certain length.
- *
- * @param mixed $value The value to validate.
- *
- * @param mixed $min The value must have at least this many
- * characters.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function validateMinLength($value, $min)
- {
- if ($this->validateBlank($value)) {
- return ! $this->_require;
- }
-
- return strlen($value) >= $min;
- }
-
- /**
- *
- * Validates that the value is not blank whitespace.
- *
- * Ignores the [[$_require]] setting.
- *
- * Boolean, integer, and float types are never "blank". All other types are
- * converted to string and trimmed; if '', then the value is blank.
- *
- * @param mixed $value The value to validate.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function validateNotBlank($value)
- {
- if (is_bool($value) || is_int($value) || is_float($value)) {
- return true;
- }
-
- return (trim((string)$value) != '');
- }
-
- /**
- *
- * Validates that the value **is not** a key in the list of allowed
- * options.
- *
- * Given an array (second parameter), the value (first parameter) must not
- * match any the array keys.
- *
- * @param mixed $value The value to validate.
- *
- * @param array $array An array of disallowed options.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function validateNotInKeys($value, $array)
- {
- if ($this->validateBlank($value)) {
- return ! $this->_require;
- }
-
- return ! array_key_exists($value, (array) $array);
- }
-
- /**
- *
- * Validates that the value **is not** in a list of disallowed values.
- *
- * Strict checking is enforced, so a string "1" is not the same as
- * an integer 1. This helps to avoid matching 0 and empty, etc.
- *
- * @param mixed $value The value to validate.
- *
- * @param array $array An array of disallowed values.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function validateNotInList($value, $array)
- {
- if ($this->validateBlank($value)) {
- return ! $this->_require;
- }
-
- return ! in_array($value, (array) $array, true);
- }
-
- /**
- *
- * Validates that the value is not exactly zero.
- *
- * @param mixed $value The value to validate.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function validateNotZero($value)
- {
- // reverse the blank-check so that empties are not
- // treated as zero.
- if ($this->_require && $this->validateBlank($value)) {
- return false;
- }
-
- $zero = is_numeric($value) && $value == 0;
- return ! $zero;
- }
-
- /**
- *
- * Validates that the value is numeric (any number or number string).
- *
- * @param mixed $value The value to validate.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function validateNumeric($value)
- {
- if ($this->validateBlank($value)) {
- return ! $this->_require;
- }
-
- return is_numeric($value);
- }
-
- /**
- *
- * Validates that the value is within a given range.
- *
- * @param mixed $value The value to validate.
- *
- * @param mixed $min The minimum valid value.
- *
- * @param mixed $max The maximum valid value.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function validateRange($value, $min, $max)
- {
- if ($this->validateBlank($value)) {
- return ! $this->_require;
- }
-
- return ($value >= $min && $value <= $max);
- }
-
- /**
- *
- * Validates that the length of the value is within a given range.
- *
- * @param mixed $value The value to validate.
- *
- * @param mixed $min The minimum valid length.
- *
- * @param mixed $max The maximum valid length.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function validateRangeLength($value, $min, $max)
- {
- if ($this->validateBlank($value)) {
- return ! $this->_require;
- }
-
- $len = strlen($value);
- return ($len >= $min && $len <= $max);
- }
-
- /**
- *
- * Validates the value against a regular expression.
- *
- * Uses [[php::preg_match() | ]] to compare the value against the given
- * regular epxression.
- *
- * @param mixed $value The value to validate.
- *
- * @param string $expr The regular expression to validate against.
- *
- * @return bool True if the value matches the expression, false if not.
- *
- */
- public function validateRegex($value, $expr)
- {
- if ($this->validateBlank($value)) {
- return ! $this->_require;
- }
-
- return (bool) preg_match($expr, $value);
- }
-
- /**
- *
- * See the value has only a certain number of digits and decimals.
- *
- * The value must be numeric, can be no longer than the `$size`,
- * and can have no more decimal places than the `$scope`.
- *
- * @param mixed $value The value to validate.
- *
- * @param int $size The total number of digits allowed in the value,
- * excluding the negative sign and decimal point.
- *
- * @param int $scope The maximum number of decimal places.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function validateSizeScope($value, $size, $scope)
- {
- if ($this->validateBlank($value)) {
- return ! $this->_require;
- }
-
- // scope has to be smaller than size.
- // both size and scope have to be positive numbers.
- if ($size < $scope || $size < 0 || $scope < 0 ||
- ! is_numeric($size) || ! is_numeric($scope)) {
- return false;
- }
-
- // value must be only numeric
- if (! is_numeric($value)) {
- return false;
- }
-
- // drop trailing and leading zeroes
- $value = (float) $value;
-
- // test the size (whole + decimal) and scope (decimal only).
- // does not include signs (+/-) or the decimal point itself.
- //
- // use the @ signs in strlen() checks to suppress errors
- // when the match-element doesn't exist.
- $expr = "/^(\-)?([0-9]+)?((\.)([0-9]+))?\$/D";
- if (preg_match($expr, $value, $match) &&
- @strlen($match[2] . $match[5]) <= $size &&
- @strlen($match[5]) <= $scope) {
- return true;
- } else {
- return false;
- }
- }
-
- /**
- *
- * Validates that the value is composed of one or more words separated by
- * a single separator-character.
- *
- * Word characters include a-z, A-Z, 0-9, and underscore, indicated by the
- * regular expression "\w".
- *
- * By default, the separator is a space, but you can include as many other
- * separators as you like. Two separators in a row will fail validation.
- *
- * @param mixed $value The value to validate.
- *
- * @param string $sep The word separator character(s), such as " -'" (to
- * allow spaces, dashes, and apostrophes in the word). Default is ' '.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function validateSepWords($value, $sep = ' ')
- {
- $expr = '/^[\w' . preg_quote($sep) . ']+$/D';
- return $this->validateRegex($value, $expr);
- }
-
- /**
- *
- * Validates that the value can be represented as a string.
- *
- * Essentially, this means any scalar value is valid (no arrays, objects,
- * resources, etc).
- *
- * @param mixed $value The value to validate.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function validateString($value)
- {
- if ($this->validateBlank($value)) {
- return ! $this->_require;
- }
-
- return is_scalar($value);
- }
-
- /**
- *
- * Validates the value as a URI per RFC2396.
- *
- * The value must match a generic URI format; for example,
- * ``http://example.com``, ``mms://example.org``, and so on.
- *
- * @param mixed $value The value to validate.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function validateUri($value)
- {
- if ($this->validateBlank($value)) {
- return ! $this->_require;
- }
-
- // first, make sure there are no invalid chars, list from ext/filter
- $other = "$-_.+" // safe
- . "!*'()," // extra
- . "{}|\\^~[]`" // national
- . "<>#%\"" // punctuation
- . ";/?:@&="; // reserved
-
- $valid = 'a-zA-Z0-9' . preg_quote($other, '/');
- $clean = preg_replace("/[^$valid]/", '', $value);
- if ($value != $clean) {
- return false;
- }
-
- // now make sure it parses as a URL with scheme and host
- $result = @parse_url($value);
- if (empty($result['scheme']) || trim($result['scheme']) == '' ||
- empty($result['host']) || trim($result['host']) == '') {
- // need a scheme and host
- return false;
- } else {
- // looks ok
- return true;
- }
- }
-
- /**
- *
- * Validates that the value is composed only of "word" characters.
- *
- * These include a-z, A-Z, 0-9, and underscore, indicated by a
- * regular expression "\w".
- *
- * @param mixed $value The value to validate.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function validateWord($value)
- {
- $expr = '/^\w+$/D';
- return $this->validateRegex($value, $expr);
- }
-}
Deleted: trunk/Solar/Valid.php
===================================================================
--- trunk/Solar/Valid.php 2007-10-06 16:35:03 UTC (rev 2840)
+++ trunk/Solar/Valid.php 2007-10-06 17:42:34 UTC (rev 2841)
@@ -1,981 +0,0 @@
-<?php
-/**
- *
- * Methods for validating data.
- *
- * @category Solar
- *
- * @package Solar
- *
- * @author Paul M. Jones <pmjones at solarphp.com>
- *
- * @license http://opensource.org/licenses/bsd-license.php BSD
- *
- * @version $Id$
- *
- */
-
-/**
- *
- * Methods for validating data.
- *
- * Solar_Valid aggregates several validation methods so you can make sure
- * user input matches your requirements. This is useful for checking form
- * values and validating database fields.
- *
- * {{code: php
- * require_once 'Solar.php';
- * Solar::start();
- *
- * // get a validation object
- * $valid = Solar::factory('Solar_Valid');
- *
- * // get a request object
- * $request = Solar::factory('Solar_Request');
- *
- * // Fetch a copy of the GET request variable for 'name'
- * $name = $request->get('name');
- *
- * // Does it match the "alpha" validation rule?
- * // (i.e., A-Z and a-z only).
- * if (! $valid->alpha($name)) {
- * echo htmlspecialchars("Name '$name' is not valid.");
- * }
- *
- * // Fetch a copy of the POST request variable for 'date'
- * $date = $request->post('date');
- *
- * // Is it an ISO-formatted date? (Alternatively,
- * // it can be completely blank.)
- * if (! $valid->isoDate($date, Solar_Valid::OR_BLANK)) {
- * echo "The date must be in 'yyyy-mm-dd' format, or blank.";
- * }
- * }}
- *
- * @category Solar
- *
- * @package Solar
- *
- */
-class Solar_Valid extends Solar_Base {
-
- /**
- * Flag for allowing validation on a blank value.
- */
- const OR_BLANK = true;
-
- /**
- * Flag for disallowing validation on a blank value.
- */
- const NOT_BLANK = false;
-
- /**
- *
- * Validate that a value is only letters (upper or lower case) and digits.
- *
- * @param mixed $value The value to validate.
- *
- * @param bool $blank Allow blank values to be valid.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function alnum($value, $blank = Solar_Valid::NOT_BLANK)
- {
- return $this->ctype($value, 'alnum', $blank);
- }
-
- /**
- *
- * Validate that a value is letters only (upper or lower case).
- *
- * @param mixed $value The value to validate.
- *
- * @param bool $blank Allow blank values to be valid.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function alpha($value, $blank = Solar_Valid::NOT_BLANK)
- {
- return $this->ctype($value, 'alpha', $blank);
- }
-
- /**
- *
- * Validate that a value is empty when trimmed of all whitespace.
- *
- * The value is assessed as a string; thus, if you pass a numeric
- * zero, the value will not validate, becuse string '0' does not
- * trim down to an empty string.
- *
- * @param mixed $value The value to validate.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function blank($value)
- {
- return (trim((string)$value) == '');
- }
-
- /**
- *
- * Validate against a callback function or method.
- *
- * Use this to perform your own ad-hoc validations. The value
- * will be passed as the first argument to the callback; the
- * callback should return boolean true if the value was valid,
- * boolean false if not.
- *
- * {{code: php
- * require_once 'Solar.php';
- * Solar::start();
- *
- * // get a validator object
- * $valid = Solar::factory('Solar_Valid');
- *
- * // validate $value against a function
- * $result = $valid->callback($value, 'my_function');
- *
- * // validate $value against a static method
- * $result = $valid->custom($value, array('SomeClass', 'StaticMethod'));
- *
- * // validate $value against an object method
- * $result = $valid->callback($value, array($object, 'MethodName'));
- *
- * // validate $value against a function,
- * // with added parameters for the function
- * $result = $valid->callback($value, 'my_function', $foo, 'bar', $etc);
- * }}
- *
- * @param mixed $value The value to validate.
- *
- * @param callback $callback A string or array suitable for use
- * as the first argument to [[php::call_user_func_array() | ]].
- *
- * @return bool True if valid, false if not.
- *
- * @see call_user_func_array()
- *
- */
- public function callback($value, $callback)
- {
- // keep all arguments so we can pass extras to the callback
- $args = func_get_args();
- // drop the value and the callback from the arglist
- array_shift($args);
- array_shift($args);
- // put the value back at the top of the argument list
- array_unshift($args, $value);
- // make the callback
- return call_user_func_array($callback, $args);
- }
-
- /**
- *
- * Validate a value against a [[php::ctype | ]] function.
- *
- * @param mixed $value The value to validate.
- *
- * @param string $type The ctype to validate against: 'alnum',
- * 'alpha', 'digit', etc.
- *
- * @param bool $blank Allow blank values to be valid.
- *
- * @return bool True if the value matches the ctype, false if not.
- *
- */
- public function ctype($value, $type, $blank = Solar_Valid::NOT_BLANK)
- {
- if ($blank && $this->blank($value)) {
- return true;
- }
- $func = 'ctype_' . $type;
- return (bool) $func((string)$value);
- }
-
- /**
- *
- * Validate that a value is an email address.
- *
- * @param mixed $value The value to validate.
- *
- * @param bool $blank Allow blank values to be valid.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function email($value, $blank = Solar_Valid::NOT_BLANK)
- {
- // taken from HTML_QuickForm.
- $expr = '/^((\"[^\"\f\n\r\t\v\b]+\")|([\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+(\.[\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+)*))@((\[(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))\])|(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))|((([A-Za-z0-9\-])+\.)+[A-Za-z\-]+))$/';
- return $this->regex($value, $expr, $blank);
- }
-
- /**
- *
- * Validate a value using a callback, and return a message if validation fails.
- *
- * This method is the **opposite** of all other Solar_Valid methods,
- * because it returns a message if validation fails, but returns
- * null if validation succeeds. This is useful for Solar_Sql_Table
- * and Solar_Form validation checking, and other places where
- * you need automated validation with feedback messaging.
- *
- * The $args param is a sequential array, in the format of array('method',
- * 'message', ...) where additional parameters are passed to the callback.
- * This is so that Solar_Form and other similar systems can use a
- * consistent format for storing validation requirements.
- *
- * @param mixed $value The value to validate.
- *
- * @param array $args An array of at least two elements; 0 is the Solar_Valid
- * method to call, 1 is the feedback message to return if validation fails,
- * and all remaining elements are additional parameters to pass to the
- * Solar_Valid method.
- *
- * @return string|void The feedback message if validation fails, or null
- * if the validation succeeded. Note that this is the opposite of all other
- * Solar_Valid returns.
- *
- */
- public function feedback($value, $args)
- {
- // need at least a method and a message in $args
- if (count($args) < 2) {
- throw $this->_exception('ERR_NOT_ENOUGH_ARGS');
- }
-
- // get the method and message,
- // then put the value on top of the args
- $method = array_shift($args);
- $message = array_shift($args);
- array_unshift($args, $value);
-
- // make the callback
- $valid = call_user_func_array(
- array($this, $method),
- $args
- );
-
- if (! $valid) {
- // validation failed, return the message
- return $message;
- }
- }
-
- /**
- *
- * Validate that the value is a key in the list of allowed options.
- *
- * Given the keys of the array (second parameter), the value
- * (first parameter) must match at least one of those keys.
- *
- * @param mixed $value The value to validate.
- *
- * @param array $array An array of allowed options.
- *
- * @param bool $blank Allow blank values to be valid.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function inKeys($value, $array, $blank = Solar_Valid::NOT_BLANK)
- {
- if ($blank && $this->blank($value)) {
- return true;
- }
-
- return array_key_exists($value, (array) $array);
- }
-
- /**
- *
- * Validate that a value is in a list of allowed values.
- *
- * Strict checking is enforced, so a string "1" is not the same as
- * an integer 1. This helps to avoid matching 0 and empty, etc.
- *
- * @param mixed $value The value to validate.
- *
- * @param array $array An array of allowed values.
- *
- * @param bool $blank Allow blank values to be valid.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function inList($value, $array, $blank = Solar_Valid::NOT_BLANK)
- {
- if ($blank && $this->blank($value)) {
- return true;
- }
-
- return in_array($value, (array) $array, true);
- }
-
- /**
- *
- * Validate that a value represents an integer (+/-).
- *
- * @param mixed $value The value to validate.
- *
- * @param bool $blank Allow blank values to be valid.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function integer($value, $blank = Solar_Valid::NOT_BLANK)
- {
- $expr = '/^[\+\-]?[0-9]+$/';
- return $this->regex($value, $expr, $blank);
- }
-
- /**
- *
- * Validate that a value is a legal IPv4 address.
- *
- * @param mixed $value The value to validate.
- *
- * @param bool $blank Allow blank values to be valid.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function ipv4($value, $blank = Solar_Valid::NOT_BLANK)
- {
- if ($blank && $this->blank($value)) {
- return true;
- }
-
- $expr = '/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/';
- $result = preg_match($expr, $value, $matches);
-
- // no match
- if (! $result) {
- return false;
- }
-
- // check that all four quads are 0-255
- for ($i = 1; $i <= 4; $i++) {
- if ($matches[$i] < 0 || $matches[$i] > 255) {
- return false;
- }
- }
-
- // done!
- return true;
- }
-
- /**
- *
- * Validate that a value is an ISO 8601 date string.
- *
- * The format is "yyyy-mm-dd". Also checks to see that the date
- * itself is valid (for example, no Feb 30).
- *
- * @param mixed $value The value to validate.
- *
- * @param bool $blank Allow blank values to be valid.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function isoDate($value, $blank = Solar_Valid::NOT_BLANK)
- {
- if ($blank && $this->blank($value)) {
- return true;
- }
-
- // basic date format
- // yyyy-mm-dd
- $expr = '/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/';
-
- // validate
- if (preg_match($expr, $value, $match) &&
- checkdate($match[2], $match[3], $match[1])) {
- return true;
- } else {
- return false;
- }
- }
-
- /**
- *
- * Validate that a value is an ISO 8601 time string (hh:ii::ss format).
- *
- * Per note from Chris Drozdowski about ISO 8601, allows two
- * midnight times ... 00:00:00 for the beginning of the day, and
- * 24:00:00 for the end of the day.
- *
- * @param mixed $value The value to validate.
- *
- * @param bool $blank Allow blank values to be valid.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function isoTime($value, $blank = Solar_Valid::NOT_BLANK)
- {
- $expr = '/^(([0-1][0-9])|(2[0-3])):[0-5][0-9]:[0-5][0-9]$/';
- return $this->regex($value, $expr, $blank) || ($value == '24:00:00');
- }
-
- /**
- *
- * Validate that a value is an ISO 8601 timestamp string.
- *
- * The format is "yyyy-mm-ddThh:ii:ss" (note the literal "T" in the
- * middle, which acts as a separator).
- *
- * Also checks that the date itself is valid (for example, no Feb 30).
- *
- * @param mixed $value The value to validate.
- *
- * @param bool $blank Allow blank values to be valid.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function isoTimestamp($value, $blank = Solar_Valid::NOT_BLANK)
- {
- if ($blank && $this->blank($value)) {
- return true;
- }
-
- // basic timestamp format (19 chars long)
- // yyyy-mm-ddThh:ii:ss
- // 0123456789012345678
- // get the individual portions
- $date = substr($value, 0, 10);
- $sep = substr($value, 10, 1);
- $time = substr($value, 11, 8);
-
- //echo "'$date' '$sep' '$time'\n";
- // now validate each portion
- if (strlen($value) == 19 &&
- $this->isoDate($date) &&
- $sep == 'T' &&
- $this->isoTime($time)) {
- return true;
- } else {
- return false;
- }
- }
-
- /**
- *
- * Validate that a value is a locale code.
- *
- * Note that this overrides Solar_Base::locale().
- *
- * The format is two lower-case letters, an underscore, and two upper-case
- * letters.
- *
- * @param mixed $value The value to validate.
- *
- * @param bool $blank Allow blank values to be valid.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function localeCode($value, $blank = Solar_Valid::NOT_BLANK)
- {
- $expr = '/^[a-z]{2}_[A-Z]{2}$/';
- return $this->regex($value, $expr, $blank);
- }
-
- /**
- *
- * Validate that a value is less than than or equal to a maximum.
- *
- * @param mixed $value The value to validate.
- *
- * @param mixed $max The maximum valid value.
- *
- * @param bool $blank Allow blank values to be valid.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function max($value, $max, $blank = Solar_Valid::NOT_BLANK)
- {
- // reverse the blank-check so that empties are not
- // treated as zero.
- if (! $blank && $this->blank($value)) {
- return false;
- }
-
- return $value <= $max;
- }
-
- /**
- *
- * Validate that a string is no longer than a certain length.
- *
- * @param mixed $value The value to validate.
- *
- * @param mixed $max The value must have no more than this many
- * characters.
- *
- * @param bool $blank Allow blank values to be valid.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function maxLength($value, $max, $blank = Solar_Valid::NOT_BLANK)
- {
- // reverse the blank-check so that empties are not
- // checked for length.
- if (! $blank && $this->blank($value)) {
- return false;
- }
-
- return (strlen($value) <= $max);
- }
-
- /**
- *
- * Validate that a value is formatted as a MIME type.
- *
- * @param mixed $value The value to validate.
- *
- * @param array $allowed The MIME type must be one of these
- * allowed values; if null, then all values are allowed.
- *
- * @param bool $blank Allow blank values to be valid.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function mimeType($value, $allowed = null,
- $blank = Solar_Valid::NOT_BLANK)
- {
- // basically, anything like 'text/plain' or
- // 'application/vnd.ms-powerpoint' or
- // 'text/xml+xhtml'
- $word = '[a-zA-Z][\-\.a-zA-Z0-9+]*';
- $expr = '|^' . $word . '/' . $word . '$|';
- $ok = $this->regex($value, $expr, $blank);
- $allowed = (array) $allowed;
- if ($ok && count($allowed) > 0) {
- $ok = in_array($value, $allowed);
- }
- return $ok;
- }
-
- /**
- *
- * Validate that a value is greater than or equal to a minimum.
- *
- * @param mixed $value The value to validate.
- *
- * @param mixed $min The minimum valid value.
- *
- * @param bool $blank Allow blank values to be valid.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function min($value, $min, $blank = Solar_Valid::NOT_BLANK)
- {
- if ($blank && $this->blank($value)) {
- return true;
- }
-
- return $value >= $min;
- }
-
- /**
- *
- * Validate that a string is at least a certain length.
- *
- * @param mixed $value The value to validate.
- *
- * @param mixed $min The value must have at least this many
- * characters.
- *
- * @param bool $blank Allow blank values to be valid.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function minLength($value, $min, $blank = Solar_Valid::NOT_BLANK)
- {
- if ($blank && $this->blank($value)) {
- return true;
- }
-
- return (strlen($value) >= $min);
- }
-
- /**
- *
- * Check the value against multiple callback validations.
- *
- * Use this to perform multiple validations on a single value.
- * All of the validations must be successful for the value to be
- * valid. If any of the validations fails, then the value is
- * treated as not valid.
- *
- * The array describing the validations must itself consist of a
- * series of arrays where the first element is a Solar_Valid
- * method name, and the remaining elements are the parameters for
- * that method (not including the value, of course).
- *
- * {{code: php
- * require_once 'Solar.php';
- * Solar::start();
- *
- * // the list of validations to perform
- * $validations = array(
- * array('maxLength', 12),
- * array('regex', '/^\w+$/', Solar_Valid::OR_BLANK),
- * );
- *
- * // this will be valid
- * $valid = Solar_Valid::multiple('something', $validations);
- *
- * // this will not be valid (too long)
- * $valid = Solar_Valid::multiple('somethingelse', $validations);
- *
- * // this will not be valid (non-word character)
- * $valid = Solar_Valid::multiple('some~thing', $validations);
- *
- * // this will be valid (not too long, and OR_BLANK)
- * $valid = Solar_Valid::multiple('', $validations);
- * }}
- *
- * @param mixed $value The value to validate.
- *
- * @param array $validations A sequential array of validations; each
- * element can be a string method name, or an array where element 0 is
- * the string method name and elements 1-N is are the arguments for
- * that method. The method must be a Solar_Valid method.
- *
- * @return bool True if the value passes all validations, false if not.
- *
- */
- public function multiple($value, $validations)
- {
- // loop through all the requested validations
- settype($validations, 'array');
- foreach ($validations as $params) {
-
- // the first element is the method name
- settype($params, 'array');
- $method = array_shift($params);
-
- // put the value at the top of the remaining parameters.
- array_unshift($params, $value);
-
- // call the validation method
- $result = call_user_func_array(
- array($this, $method),
- $params
- );
-
- // if it failed, cancel further validation
- if (! $result) {
- return false;
- }
- }
-
- // passed all validations
- return true;
- }
-
- /**
- *
- * Validate that a value is not exactly zero.
- *
- * @param mixed $value The value to validate.
- *
- * @param bool $blank Allow blank values to be valid.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function notZero($value, $blank = Solar_Valid::NOT_BLANK)
- {
- // reverse the blank-check so that empties are not
- // treated as zero.
- if (! $blank && $this->blank($value)) {
- return false;
- }
-
- // +-000.000
- $expr = '/^(\+|\-)?0+(.0+)?$/';
- return ! $this->regex($value, $expr);
- }
-
- /**
- *
- * Validate that a string is not empty when trimmed.
- *
- * Spaces, newlines, etc. will be trimmed, so a value consisting
- * only of whitespace is considered blank.
- *
- * @param mixed $value The value to validate.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function notBlank($value)
- {
- return (trim((string)$value) != '');
- }
-
- /**
- *
- * Validate that a value is within a given range.
- *
- * @param mixed $value The value to validate.
- *
- * @param mixed $min The minimum valid value.
- *
- * @param mixed $max The maximum valid value.
- *
- * @param bool $blank Allow blank values to be valid.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function range($value, $min, $max, $blank = Solar_Valid::NOT_BLANK)
- {
- if ($blank && $this->blank($value)) {
- return true;
- }
-
- return ($value >= $min && $value <= $max);
- }
-
- /**
- *
- * Validate that the length of a value is within a given range.
- *
- * @param mixed $value The value to validate.
- *
- * @param mixed $min The minimum valid length.
- *
- * @param mixed $max The maximum valid length.
- *
- * @param bool $blank Allow blank values to be valid.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function rangeLength($value, $min, $max, $blank = Solar_Valid::NOT_BLANK)
- {
- if ($blank && $this->blank($value)) {
- return true;
- }
-
- $len = strlen($value);
- return ($len >= $min && $len <= $max);
- }
-
- /**
- *
- * Validate a value against a regular expression.
- *
- * Uses [[php::preg_match() | ]] to compare the value against the given
- * regular epxression.
- *
- * @param mixed $value The value to validate.
- *
- * @param string $expr The regular expression to validate against.
- *
- * @param bool $blank Allow blank values to be valid.
- *
- * @return bool True if the value matches the expression, false if not.
- *
- */
- public function regex($value, $expr, $blank = Solar_Valid::NOT_BLANK)
- {
- if ($blank && $this->blank($value)) {
- return true;
- }
- return (bool) preg_match($expr, $value);
- }
-
- /**
- *
- * See a value has only a certain number of digits and decimals.
- *
- * The value must be numeric, can be no longer than the `$size`,
- * and can have no more decimal places than the `$scope`.
- *
- * @param mixed $value The value to validate.
- *
- * @param int $size The total number of digits allowed in the value,
- * excluding the negative sign and decimal point.
- *
- * @param int $scope The maximum number of decimal places.
- *
- * @param bool $blank Allow blank values to be valid.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function scope($value, $size, $scope, $blank = Solar_Valid::NOT_BLANK)
- {
- // allowed blank?
- if ($blank && $this->blank($value)) {
- return true;
- }
-
- // scope has to be smaller than size.
- // both size and scope have to be positive numbers.
- if ($size < $scope || $size < 0 || $scope < 0 ||
- ! is_numeric($size) || ! is_numeric($scope)) {
- return false;
- }
-
- // value must be only numeric
- if (! is_numeric($value)) {
- return false;
- }
-
- // drop trailing and leading zeroes
- $value = (float) $value;
-
- // test the size (whole + decimal) and scope (decimal only).
- // does not include signs (+/-) or the decimal point itself.
- //
- // use the @ signs in strlen() checks to suppress errors
- // when the match-element doesn't exist.
- $expr = "/^(\-)?([0-9]+)?((\.)([0-9]+))?$/";
- if (preg_match($expr, $value, $match) &&
- @strlen($match[2] . $match[5]) <= $size &&
- @strlen($match[5]) <= $scope) {
- return true;
- } else {
- return false;
- }
- }
-
- /**
- *
- * Validate that a value is composed of separated words.
- *
- * These include a-z, A-Z, 0-9, and underscore, indicated by a
- * regular expression "\w". By default, the separator is a space.
- *
- * @param mixed $value The value to validate.
- *
- * @param string $sep The word separator character.
- *
- * @param bool $blank Allow blank values to be valid.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function sepWords($value, $sep = ' ', $blank = Solar_Valid::NOT_BLANK)
- {
- $expr = '/^[\w' . preg_quote($sep) . ']+$/';
- return $this->regex($value, $expr, $blank);
- }
-
- /**
- *
- * Validate a value as a URI per RFC2396.
- *
- * The value must match a generic URI format; for example,
- * ``http://example.com``, ``mms://example.org``, and so on.
- *
- * @param mixed $value The value to validate.
- *
- * @param string|array $schemes Allowed schemes for the URI; for example,
- * array('http', 'https', 'ftp'). If null, all schemes are allowed.
- *
- * @param bool $blank Allow blank values to be valid.
- *
- * @return bool True if the value is a URI and is one of the allowed
- * schemes, false if not.
- *
- */
- public function uri($value, $schemes = null, $blank = Solar_Valid::NOT_BLANK)
- {
- // allow blankness?
- if ($blank && $this->blank($value)) {
- return true;
- }
-
- // TAKEN (almost) DIRECTLY FROM PEAR_VALIDATE::URI()
- $result = preg_match(
- '{^(?:([a-z][-+.a-z0-9]*):)? # 1. scheme
- (?:// # authority start
- (?:((?:%[0-9a-f]{2}|[-a-z0-9_.!~*\'();:&=+$,])*)@)? # 2. authority-userinfo
- (?:((?:[a-z0-9](?:[-a-z0-9]*[a-z0-9])?\.)*[a-z](?:[-a-z0-9]*[a-z0-9])?\.?) # 3. authority-hostname OR
- |([0-9]{1,3}(?:\.[0-9]{1,3}){3})) # 4. authority-ipv4
- (?::([0-9]*))?)? # 5. authority-port
- ((?:/(?:%[0-9a-f]{2}|[-a-z0-9_.!~*\'():@&=+$,;])*)+)? # 6. path
- (?:\?([^#]*))? # 7. query
- (?:\#((?:%[0-9a-f]{2}|[-a-z0-9_.!~*\'();/?:@&=+$,])*))? # 8. fragment
- $}xi', $value, $matches
- );
-
- if ($result) {
-
- $scheme = isset($matches[1]) ? $matches[1] : '';
- $authority = isset($matches[3]) ? $matches[3] : '' ;
-
- // we need some sort of scheme
- if (! $scheme) {
- return false;
- }
-
- // is the scheme allowed?
- settype($schemes, 'array');
- if ($schemes && ! in_array($scheme, $schemes)) {
- return false;
- }
-
- // check IPv4 addresses as domains
- if (isset($matches[4])) {
- $parts = explode('.', $matches[4]);
- foreach ($parts as $part) {
- if ($part > 255) {
- return false;
- }
- }
- }
-
- // are we doing strict checks?
- $list = ';/?:@$,';
- $strict = '#[' . preg_quote($list, '#') . ']#';
- $test1 = (isset($matches[7]) && preg_match($strict, $matches[7]));
- $test2 = (isset($matches[8]) && preg_match($strict, $matches[8]));
- if ($test1 || $test2) {
- return false;
- }
-
- return true;
- }
-
- // default is to not-validate
- return false;
- }
-
- /**
- *
- * Validate that a value is composed only of "word" characters.
- *
- * These include a-z, A-Z, 0-9, and underscore, indicated by a
- * regular expression "\w".
- *
- * @param mixed $value The value to validate.
- *
- * @param bool $blank Allow blank values to be valid.
- *
- * @return bool True if valid, false if not.
- *
- */
- public function word($value, $blank = Solar_Valid::NOT_BLANK)
- {
- $expr = '/^\w+$/';
- return $this->regex($value, $expr, $blank);
- }
-}
More information about the Solar-svn
mailing list