示例#1
0
 /**
  * Performs the validation and returns the result
  *
  * @param Mixed $value The value to validate
  * @return \r8\Validator\Result
  */
 public function validate($value)
 {
     // Invoke the internal validator
     $result = $this->process($value);
     if ($result instanceof \Traversable) {
         $result = \iterator_to_array($result);
     }
     // Normalize the results if it is an array
     if (\is_array($result)) {
         $result = \r8\ary\flatten($result);
         $result = \r8\ary\stringize($result);
         $result = \r8\ary\compact($result);
     } elseif ($result instanceof \r8\Validator\Result) {
         $result = $result->getErrors();
     } elseif (is_null($result) || is_bool($result) || $result === 0 || $result === 0.0) {
         $result = null;
     } else {
         $result = (string) $result;
     }
     // Boot up the results of the validation process
     $output = new \r8\Validator\Result($value);
     // If the internal validator returned a non-empty value
     // (either an array with values or a non-blank string)
     if (!\r8\isEmpty($result)) {
         // If this validator is hooked up with a set of custom error messages,
         // use them instead of what the result returned
         if ($this->hasErrors()) {
             $output->addErrors($this->getErrors());
         } else {
             $output->addErrors($result);
         }
     }
     return $output;
 }
示例#2
0
 /**
  * Converts the given value to boolean
  *
  * @param Mixed $value The value to filter
  * @return Boolean
  */
 public function filter($value)
 {
     if (is_bool($value)) {
         return $value;
     } else {
         if (is_int($value) || is_float($value)) {
             return $value == 0 ? FALSE : TRUE;
         } else {
             if (is_null($value)) {
                 return FALSE;
             } else {
                 if (is_string($value)) {
                     $value = strtolower(\r8\str\stripW($value));
                     if ($value == "f" || $value == "false" || $value == "n" || $value == "no" || $value == "off" || \r8\isEmpty($value)) {
                         return FALSE;
                     } else {
                         return TRUE;
                     }
                 } else {
                     if (is_array($value)) {
                         return count($value) == 0 ? FALSE : TRUE;
                     } else {
                         return $value ? TRUE : FALSE;
                     }
                 }
             }
         }
     }
 }
示例#3
0
 /**
  * Constructor...
  *
  * @param String $regex The Regular Expression to compare the value to
  */
 public function __construct($regex)
 {
     $regex = (string) $regex;
     if (\r8\isEmpty($regex)) {
         throw new \r8\Exception\Argument(0, "Regular Expression", "Must not be empty");
     }
     $this->regex = $regex;
 }
示例#4
0
 /**
  * Constructor...
  *
  * @param String $tag The name of the tag to construct
  * @param Array $attrs Any attributes to add to the created element
  */
 public function __construct($tag, array $attrs = array())
 {
     $tag = trim((string) $tag);
     if (\r8\isEmpty($tag)) {
         throw new \r8\Exception\Argument(1, "Tag Name", "Must not be empty");
     }
     $this->tag = $tag;
     $this->setAttributes($attrs);
 }
示例#5
0
 /**
  * Sets the value of the condition in this instance
  *
  * @param String $condition The condition this instance represents
  * @return \r8\HTML\Conditional Returns a self reference
  */
 public function setCondition($condition)
 {
     $condition = trim(\r8\str\stripW($condition, " "));
     if (\r8\isEmpty($condition)) {
         throw new \r8\Exception\Argument(0, "Condition", "Must not be empty");
     }
     $this->condition = $condition;
     return $this;
 }
示例#6
0
 /**
  * Constructor...
  *
  * @param String $name The name of this argument
  * @param \r8\iface\Filter $filter The filter to apply to this argument's data
  * @param \r8\iface\Validator $validator The validator to use for checking
  *      this argument's data
  */
 public function __construct($name, \r8\iface\Filter $filter = NULL, \r8\iface\Validator $validator = NULL)
 {
     $name = \r8\str\stripNoPrint($name);
     if (\r8\isEmpty($name)) {
         throw new \r8\Exception\Argument(0, "Name", "Must not be empty");
     }
     $this->name = $name;
     $this->filter = $filter ?: new \r8\Filter\Identity();
     $this->validator = $validator ?: new \r8\Validator\Pass();
 }
示例#7
0
 /**
  * Adds a new error to this instance
  *
  * @param String $message The error message to add
  * @return \r8\Validator\ErrorList Returns a self reference
  */
 public function addError($message)
 {
     $message = (string) $message;
     if (\r8\isEmpty($message)) {
         throw new \r8\Exception\Argument(0, "Error Message", "Must Not Be Empty");
     }
     if (!in_array($message, $this->errors)) {
         $this->errors[] = $message;
     }
     return $this;
 }
示例#8
0
 /**
  * Constructor...
  *
  * @param String $message
  * @param String $level
  * @param String $code
  * @param Array $data
  */
 public function __construct($message, $level, $code, array $data = array())
 {
     $code = \r8\str\stripW($code, "_");
     if (\r8\isEmpty($code)) {
         throw new \r8\Exception\Argument(2, 'Code', 'Can only contain Numbers, Strings and Underscores');
     }
     $this->message = (string) $message;
     $this->level = \r8\Log\Level::resolveValue($level);
     $this->code = (string) $code;
     $this->data = $data;
     $this->time = \microtime(TRUE);
 }
示例#9
0
 /**
  * Cleans and normalizes a flag
  *
  * @param String $flag
  * @param Boolean $require Whether to throw an exception if
  *      this flag is empty
  * @return String
  */
 public static function normalizeFlag($flag, $require = TRUE)
 {
     $flag = trim(str_replace(" ", "-", (string) $flag), "-");
     $flag = \r8\str\stripW($flag, "-");
     if ($require && \r8\isEmpty($flag)) {
         throw new \r8\Exception\Argument(0, "Flag", "Must not be empty");
     } else {
         if (strlen($flag) > 1) {
             $flag = strtolower($flag);
         }
     }
     return $flag;
 }
示例#10
0
 /**
  * Adds a new prefix and where to look for it
  *
  * @param String $prefix The class Prefix to look for
  * @param String $location Where to look for this file
  * @return \r8\Autoload Returns a self reference
  */
 public function register($prefix, $location)
 {
     $prefix = trim((string) $prefix, '\\ ');
     if (\r8\isEmpty($prefix)) {
         throw new \r8\Exception\Argument(0, "Class Prefix", "Must not be empty");
     }
     $location = rtrim((string) $location, "/");
     if (\r8\isEmpty($location)) {
         throw new \r8\Exception\Argument(1, "File Location", "Must not be empty");
     }
     $this->map = array($prefix => $location) + $this->map;
     return $this;
 }
示例#11
0
 /**
  * Constructor...
  *
  * @param String $name The name of this command
  * @param String $description A description of this command
  */
 public function __construct($name, $description)
 {
     $name = \r8\str\stripNoPrint($name);
     if (\r8\isEmpty($name)) {
         throw new \r8\Exception\Argument(0, "Name", "Must not be empty");
     }
     $description = \r8\str\stripNoPrint($description);
     if (\r8\isEmpty($description)) {
         throw new \r8\Exception\Argument(0, "Description", "Must not be empty");
     }
     $this->name = $name;
     $this->description = $description;
     $this->forms[] = new \r8\CLI\Form();
 }
示例#12
0
 /**
  * Validates an e-mail address
  *
  * @param mixed $value The value to validate
  * @return String|NULL Any errors encountered
  */
 protected function process($value)
 {
     $value = (string) $value;
     if (\r8\isEmpty($value)) {
         return "Email Address must not be empty";
     }
     $atCount = substr_count($value, "@");
     if ($atCount == 0) {
         return "Email Address must contain an 'at' (@) symbol";
     }
     if ($atCount > 1) {
         return "Email Address must only contain one 'at' (@) symbol";
     }
     if (\r8\str\contains(" ", $value)) {
         return "Email Address must not contain spaces";
     }
     if (\r8\str\contains("\n", $value) || \r8\str\contains("\r", $value)) {
         return "Email Address must not contain line breaks";
     }
     if (\r8\str\contains("\t", $value)) {
         return "Email Address must not contain tabs";
     }
     if (preg_match('/\\.\\.+/', $value)) {
         return "Email Address must not contain repeated periods";
     }
     if (preg_match('/[^a-z0-9' . preg_quote('!#$%&\'*+-/=?^_`{|}~@.[]', '/') . ']/i', $value)) {
         return "Email Address contains invalid characters";
     }
     if (\r8\str\endsWith($value, ".")) {
         return "Email Address must not end with a period";
     }
     list($local, $domain) = explode("@", $value);
     if (\r8\str\startsWith($local, ".")) {
         return "Email Address must not start with a period";
     }
     // This is hard to describe to a user, so just give them a vague description
     if (\r8\str\endsWith($local, ".")) {
         return "Email Address is not valid";
     }
     if (strlen($local) > 64 || strlen($domain) > 255) {
         return "Email Address is too long";
     }
     $regex = '/' . '^' . '[\\w!#$%&\'*+\\/=?^`{|}~.-]+' . '@' . '(?:[a-z\\d][a-z\\d-]*(?:\\.[a-z\\d][a-z\\d-]*)?)+' . '\\.(?:[a-z][a-z\\d-]+)' . '$' . '/iD';
     // Do a final regex to match the basic form
     if (!preg_match($regex, $value)) {
         return "Email Address is not valid";
     }
 }
示例#13
0
 /**
  * Constructor...
  *
  * @param String $host The host of the Memcache server
  * @param Integer $port The port to connect to
  * @param Boolean $persistent Whether to connect with a persistent connection
  * @param Integer $timeout The connection timeout value in seconds
  */
 public function __construct($host = "127.0.0.1", $port = 11211, $persistent = FALSE, $timeout = 1)
 {
     if (!extension_loaded('memcache')) {
         throw new \r8\Exception\Extension("memcache", "Extension is not loaded");
     }
     $this->host = trim((string) $host);
     if (\r8\isEmpty($this->host)) {
         throw new \r8\Exception\Argument(0, "Memcache Host", "Must not be empty");
     }
     $this->port = (int) $port;
     if ($this->port < 0) {
         throw new \r8\Exception\Argument(1, "Memcache Port", "Must be at least 0");
     }
     $this->persistent = (bool) $persistent;
     $this->timeout = (int) $timeout;
     if ($this->timeout <= 0) {
         throw new \r8\Exception\Argument(1, "Memcache Port", "Must be greater than 0");
     }
 }
示例#14
0
 public function testIsEmpty()
 {
     $this->assertTrue(\r8\isEmpty(""));
     $this->assertTrue(\r8\isEmpty(0));
     $this->assertTrue(\r8\isEmpty(NULL));
     $this->assertTrue(\r8\isEmpty(FALSE));
     $this->assertTrue(\r8\isEmpty(array()));
     $this->assertTrue(\r8\isEmpty("  "));
     $this->assertFalse(\r8\isEmpty("string"));
     $this->assertFalse(\r8\isEmpty(1));
     $this->assertFalse(\r8\isEmpty("0"));
     $this->assertFalse(\r8\isEmpty("1"));
     $this->assertFalse(\r8\isEmpty(TRUE));
     $this->assertFalse(\r8\isEmpty(array(1)));
     $this->assertFalse(\r8\isEmpty("", \r8\ALLOW_BLANK));
     $this->assertFalse(\r8\isEmpty(0, \r8\ALLOW_ZERO));
     $this->assertFalse(\r8\isEmpty(NULL, \r8\ALLOW_NULL));
     $this->assertFalse(\r8\isEmpty(FALSE, \r8\ALLOW_FALSE));
     $this->assertFalse(\r8\isEmpty(array(), \r8\ALLOW_EMPTY_ARRAYS));
     $this->assertFalse(\r8\isEmpty("  ", \r8\ALLOW_SPACES));
 }
示例#15
0
 /**
  * Sets the encoding this for should use
  *
  * @param String $encoding
  * @return Object Returns a self reference
  */
 public function setEncoding($encoding)
 {
     $encoding = trim((string) $encoding);
     if (\r8\isEmpty($encoding)) {
         throw new \r8\Exception\Argument(0, "Form Encoding", "Must not be empty");
     }
     $this->encoding = $encoding;
     return $this;
 }
示例#16
0
 /**
  * Sets the value of the tag in this instance
  *
  * @param String $tag The tag this instance represents
  * @return \r8\HTML\Tag Returns a self reference
  */
 public function setTag($tag)
 {
     $tag = strtolower(\r8\str\stripW($tag));
     if (\r8\isEmpty($tag)) {
         throw new \r8\Exception\Argument(0, "Tag", "Must not be empty");
     }
     $this->tag = $tag;
     return $this;
 }
示例#17
0
/**
 * Collapses long words down with elipses
 *
 * @param String $string The string to scan for long words
 * @param Integer $maxLength The maximum length a word can be
 * @param Integer|Boolean $trimTo How short to make the long words
 * @param String $glue The delimiter to replace the removed characters with
 * @return String Returns the string with all the long words replaced
 */
function truncateWords($string, $maxLength, $trimTo = FALSE, $glue = '...')
{
    $string = (string) $string;
    if (\r8\isEmpty($string)) {
        return '';
    }
    $glue = (string) $glue;
    // Maxlength must be at least 1
    $maxLength = max($maxLength, 1);
    // If they didn't define a trimTo, then default it to 2/3 the max length
    if (\r8\isVague($trimTo) || (int) $trimTo <= 0) {
        $trimTo = ceil($maxLength * (2 / 3));
    }
    // The trimTo length can't be greater than the max length
    // The trimTo must be at least the length of the glue + characters on each side
    $trimTo = max($trimTo, strlen($glue) + 2);
    // We now double check to make sure the maxlength is at least equal to the trimTo
    $maxLength = max($maxLength, $trimTo);
    $first = ceil(($trimTo - strlen($glue)) / 2);
    $third = $trimTo - strlen($glue) - $first;
    $second = $maxLength - $trimTo + strlen($glue);
    return preg_replace('/\\b(\\w{' . $first . '})\\w{' . $second . ',}(\\w{' . $third . '})\\b/i', '\\1' . $glue . '\\2', $string);
}
示例#18
0
 /**
  * Parses a query string into an array
  *
  * @param String $query The query string to parser
  * @return Array Returns the parsed string as an array
  */
 public function parse($query)
 {
     $query = (string) $query;
     // Grab everything after the starting delimiter
     if (\r8\str\contains($this->startDelim, $query)) {
         $query = substr($query, strpos($query, $this->startDelim) + 1);
     }
     // Cut off everything after the ending delimiter
     if (\r8\str\contains($this->endDelim, $query)) {
         $query = substr($query, 0, strpos($query, $this->endDelim));
     }
     // Split the query into its pairs
     $query = explode($this->outerDelim, $query);
     $result = array();
     // Loop through each pair
     foreach ($query as $pair) {
         // Skip over empty pairs
         if (\r8\isEmpty($pair)) {
             continue;
         }
         // split the pair up into its key and value
         if (\r8\str\contains($this->innerDelim, $pair)) {
             list($key, $value) = explode($this->innerDelim, $pair, 2);
         } else {
             list($key, $value) = array($pair, "");
         }
         // if the key is empty, do nothing with it
         if (\r8\isEmpty($key, \r8\ALLOW_SPACES)) {
             continue;
         }
         // Apply the filters to the key and value
         $key = $this->keyFilter->filter($key);
         $value = $this->valueFilter->filter($value);
         // parse the list of keys into an array
         $key = $this->parseKey($key);
         // Add the branch to the result array
         \r8\ary\branch($result, $value, $key);
     }
     return $result;
 }
示例#19
0
 /**
  * Performs the filtering
  *
  * @param Mixed $value The value to filter
  * @return Mixed
  */
 public function filter($value)
 {
     return \r8\isEmpty($value, $this->flags) ? $this->value : $value;
 }
示例#20
0
 /**
  * Recursively builds an XML tree
  *
  * @param \DOMDocument $doc The document being built
  * @param String $parent The tag name of the parent element
  * @param Mixed $data The data being pieced together
  * @param Boolean $root Whether the data being parsed is at the root level
  *      This is used during iteration, for example, to ensure lists are
  *      created properly
  * @return DOMNode Returns the built node
  */
 protected function build(\DOMDocument $doc, $parent, &$data, $root = FALSE)
 {
     if (\r8\isEmpty($data)) {
         return $this->createElement($doc, $parent);
     } else {
         if (\r8\isBasic($data) && $data !== NULL) {
             $node = $this->createElement($doc, $parent);
             $node->appendChild($doc->createTextNode((string) $data));
         } else {
             if (is_array($data) || $data instanceof \Traversable) {
                 $node = $this->iterate($doc, $parent, $data, $root);
             } else {
                 if ($data instanceof \r8\iface\XMLBuilder) {
                     $node = $this->createElement($doc, $parent);
                     $node->appendChild(\r8\XMLBuilder::buildNode($data, $doc));
                 } else {
                     if (is_object($data)) {
                         // If it is an object that supports "toString"
                         if (\r8\respondTo($data, "__toString")) {
                             $node = $this->createElement($doc, $parent);
                             $node->appendChild($doc->createTextNode($data->__toString()));
                         } else {
                             $props = get_object_vars($data);
                             $node = $this->iterate($doc, $parent, $props, $root);
                         }
                     }
                 }
             }
         }
     }
     return $node;
 }
示例#21
0
 /**
  * Renders the contained page and returns the results
  *
  * @return \r8\iface\Template Returns the rendered template
  */
 public function getTemplate()
 {
     $context = $this->getContext();
     try {
         $template = $this->getPage()->getContent($context);
     } catch (\r8\Page\Interrupt $err) {
         // If an interrupt is thrown, suppress the page load
         $context->suppress();
     }
     // Pull the redirect URL
     $redirect = $context->getRedirect();
     // If the redirect URL isn't empty, send the appropriate header
     if (!\r8\isEmpty($redirect)) {
         $this->getResponse()->setHeader("Location", $redirect);
     }
     // If the context denotes a suppressed page, return a blank template
     if ($context->isSuppressed()) {
         return new \r8\Template\Blank();
     }
     return $template;
 }
示例#22
0
 /**
  * Sets the escape string
  *
  * @param String The new escape string
  * @return Object Returns a self reference
  */
 public function setEscape($escape)
 {
     $escape = (string) $escape;
     if (\r8\isEmpty($escape, ALLOW_SPACES)) {
         throw new \r8\Exception\Argument(0, "Escape String", "Must not be empty");
     }
     $this->escape = $escape;
     return $this;
 }
示例#23
0
 /**
  * Sets the filename for this file
  *
  * @param String $filename The new filename
  * @return \r8\FileSys\File Returns a self reference
  */
 public function setFilename($filename)
 {
     $filename = trim((string) $filename);
     $filename = rtrim($filename, ".");
     $this->filename = \r8\isEmpty($filename) ? null : $filename;
     return $this;
 }
示例#24
0
/**
 * Returns true if the value is boolean or empty
 *
 * @param mixed $value The value being tested
 * @param Integer $flags Any isEmpty() flags
 * @return Boolean
 */
function isVague($value, $flags = 0)
{
    return is_bool($value) || \r8\isEmpty($value, $flags);
}
示例#25
0
 /**
  * Sets the label that describes this form field
  *
  * @param String $label
  * @return \r8\Form\Field Returns a self reference
  */
 public function setLabel($label)
 {
     $this->label = \r8\isEmpty((string) $label) ? NULL : (string) $label;
     return $this;
 }
示例#26
0
 /**
  * Returns the full URL contained in this instance
  *
  * @return String
  */
 public function getURL()
 {
     $url = $this->getBase() . $this->getRelative();
     return \r8\isEmpty($url, \r8\ALLOW_SPACES) ? null : $url;
 }
示例#27
0
 /**
  * Runs a query and returns the result
  *
  * @param String $query The query to run
  * @param Integer $flags Any boolean flags to set
  * @return \r8\iface\DB\Result Returns a result object
  */
 public function query($query, $flags = 0)
 {
     if ($query instanceof \r8\iface\DB\Query) {
         $query = (string) $query->toSQL($this);
     } else {
         $query = (string) $query;
     }
     if (\r8\isEmpty($query)) {
         throw new \r8\Exception\Argument(0, "Query", "Must not be empty");
     }
     if (!$this->adapter->isConnected()) {
         $this->adapter->connect();
     }
     $result = $this->adapter->query($query);
     if (!$result instanceof \r8\iface\DB\Result) {
         throw new \r8\Exception\DB\Query($query, 'Query did not return a \\r8\\iface\\DB\\Result object', 0, $this->adapter);
     }
     return $result;
 }
示例#28
0
 /**
  * Helper function that returns whether a given array has key with a
  * non-empty value
  *
  * @param Array $array The array to test
  * @param String $key The key to test
  * @return Boolean
  */
 private static function hasKey(array &$array, $key)
 {
     if (!array_key_exists($key, $array)) {
         return FALSE;
     }
     if (\r8\isEmpty($array[$key])) {
         return FALSE;
     }
     return TRUE;
 }
示例#29
0
/**
 * Recursively removes all the empty values from an array
 *
 * @param array $array The array to compact
 * @param integer $flags Any valid isEmpty flags to use to determine if a value is empty
 * @return object Returns a compacted version of the current array
 */
function compact(array $array, $flags = 0)
{
    $flags = max((int) $flags, 0);
    // Create the callback to apply to each sub-array
    $compact = function ($array, &$compact) use($flags) {
        $output = array();
        foreach ($array as $key => $value) {
            // Recurse in to sub arrays
            if (is_array($value) && count($value) > 0) {
                $value = $compact($value, $compact);
            }
            // Add the value on to the result array only if it isn't empty
            if (!\r8\isEmpty($value, $flags)) {
                $output[$key] = $value;
            }
        }
        return $output;
    };
    return $compact($array, $compact);
}
示例#30
0
 /**
  * Counts the number of rows in a table
  *
  * To restrict which rows are affected, pass a WHERE clause through the $where
  * argument.
  *
  * @param String $table The table to update
  * @param String $where Any WHERE clause restrictions to place on the query
  * @return Integer Returns the number of counted rows
  */
 public function count($table, $where = FALSE)
 {
     $table = (string) $table;
     if (\r8\isEmpty($table)) {
         throw new \r8\Exception\Argument(0, "Table Name", "Must not be empty");
     }
     $query = "SELECT COUNT(*) AS cnt FROM " . $table;
     $where = trim((string) $where);
     if (!\r8\isEmpty($where)) {
         $query .= " WHERE " . $where;
     }
     return (int) $this->getField("cnt", $query, 0);
 }