/** * 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; }
/** * Adds multiple errors at once * * This method accepts any number of arguments. They will be flattened down, * converted to strings and added as errors * * @param String|Array $errors... Errors to add to this instance * @return \r8\Validator\ErrorList Returns a self reference */ public function addErrors($errors) { $errors = func_get_args(); $errors = \r8\ary\flatten($errors); $errors = \r8\ary\compact($errors); $errors = \array_unique($errors); array_walk($errors, array($this, "addError")); return $this; }
public function testCompact() { $ary = array(0, TRUE, NULL, "string", FALSE, 1, array(), array(1.5, ""), " ", "0"); $this->assertEquals(array(1 => TRUE, 3 => "string", 5 => 1, 7 => array(1.5), 9 => "0"), \r8\ary\compact($ary)); $this->assertEquals(array(1 => TRUE, 2 => NULL, 3 => "string", 4 => FALSE, 5 => 1, 7 => array(1.5), 9 => "0"), \r8\ary\compact($ary, \r8\ALLOW_FALSE | \r8\ALLOW_NULL)); $ary = array(array("full", "of", "stuff", FALSE), array(), array(1.5, "")); $this->assertEquals(array(0 => array("full", "of", "stuff"), 2 => array(1.5)), \r8\ary\compact($ary)); }
/** * Registers a set of quotes * * If the opening quote has already been registered, the closing quotes will * be replaced with the new set * * @param String $open The opening quote * @param Null|String|Array $close If left empty, this will assume the closing quote * is the same as the opening quote. If an array is given, it will be * flattened and compacted. * @return object Returns a self reference */ public function setQuote($open, $close = FALSE) { $open = (string) $open; if (\r8\isEmpty($open, ALLOW_SPACES)) { throw new \r8\Exception\Argument(0, "Open Quote", "Must not be empty"); } if (\r8\isVague($close, ALLOW_SPACES)) { $close = array($open); } else { $close = (array) $close; $close = \r8\ary\flatten($close); $close = \r8\ary\stringize($close); $close = \r8\ary\compact($close, \r8\ALLOW_SPACES); $close = \array_unique($close); } $this->quotes[$open] = $close; return $this; }
/** * Removes quoted text from a string. * * To define your own quotes, simply add them as arguments. For a more advanced * Quotation interface, see the Quoted Class. * * @param String $string The string to remove the quoted values from * @param String $quotes Strings that should be treated as quotes * @return String Returns the string with all quoted segments removed */ function stripQuoted($string, $quotes = array("'", '"')) { $string = (string) $string; $quotes = (array) $quotes; $quotes = \r8\ary\flatten($quotes); $quotes = \array_map('trim', $quotes); $quotes = \array_unique($quotes); $quotes = \r8\ary\compact($quotes); $quoteString = array_map(r8(new \r8\Curry\Call("preg_quote"))->setRight("/")->setLimit(1), $quotes); $quoteString = implode("|", $quoteString); $split = preg_split('/(?<!\\\\)(?:\\\\\\\\)*(' . $quoteString . ')/i', $string, -1, PREG_SPLIT_DELIM_CAPTURE); $curQuote = NULL; $output = ""; foreach ($split as $key => $part) { if (is_null($curQuote) && in_array($part, $quotes)) { $curQuote = $part; } else { if (is_null($curQuote)) { $output .= $part; } else { if (in_array($part, $quotes)) { $curQuote = null; } } } } return $output; }