/** * 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 testFlatten() { $this->assertSame(array(1, 2, 3), \r8\ary\flatten(array(array(1, 2, 3)))); $this->assertSame(array(1, 2, 3, 4, 5, 6), \r8\ary\flatten(array(array(1, 2, 3), array(4, 5, 6)))); $this->assertSame(array(1, 2, 3, 4, 5, 6, 7, 8), \r8\ary\flatten(array(array(1, 2, 3), array(4, 5, array(6, 7, 8))))); $this->assertSame(array(array(1, 2, 3), array(4, 5, 6, 7, 8)), \r8\ary\flatten(array(array(1, 2, 3), array(4, 5, array(6, 7, 8))), 2)); $this->assertSame(array(array(1, 2, 3), array(4, 5, array(6, 7, 8))), \r8\ary\flatten(array(array(1, 2, 3), array(4, 5, array(6, 7, 8))), 3)); $this->assertSame(array(array(1, 2, 3), array(4, 5, array(6, 7, 8))), \r8\ary\flatten(array(array(1, 2, 3), array(4, 5, array(6, 7, 8))), 4)); $this->assertSame(array(1 => 'one', 2 => 'two', 3 => 'three'), \r8\ary\flatten(array(1 => 'one', 2 => 'two', 3 => 'three'))); }
/** * Takes an array of fields and constructs a field list for a query * * @param array $fields The fields to iterate over where the key * is the field name and the value is the field value * @return String Returns a SQL field list */ public function getFieldList(array $fields) { $fields = \r8\ary\flatten($fields); if (count($fields) <= 0) { throw new \r8\Exception\Argument(0, "Field List", "Must not be empty"); } foreach ($fields as $name => $value) { $fields[$name] = "`" . $name . "` = " . $this->quote($value); } return implode(", ", $fields); }
/** * Translates an array to contain the specified keys * * If a key isn't set in the original array, it fills the array by offset. * * @param mixed $keys... The keys being filtered * @return array */ function hone(array $array, $keys) { $keys = \func_get_args(); \array_shift($keys); $keys = \r8\ary\flatten($keys); $keys = \array_unique($keys); // get values in the array that do not have the required keys $no_keys = array_diff_key($array, array_flip($keys)); $out = array(); // Rather than using internal functions, we are looping in order to // preserve the order of the keys foreach ($keys as $key) { if (array_key_exists($key, $array)) { $out[$key] = $array[$key]; } else { if (count($no_keys) > 0) { $out[$key] = array_shift($no_keys); } } } return $out; }
/** * Adds many validators to this instance at once * * @param mixed $validators... Any arguments passed will be flattened down and filtered * @return Object Returns a self reference */ public function addMany($validators) { $validators = func_get_args(); $validators = \r8\ary\flatten($validators); $validators = array_filter($validators, function ($validator) { return $validator instanceof \r8\iface\Validator; }); array_walk($validators, array($this, "add")); return $this; }
/** * Imports a set of options from an array or traversable object * * @param array $source * @return \r8\Form\Multi Returns a self reference */ public function importOptions(array $source) { $source = \r8\ary\flatten($source); foreach ($source as $key => $value) { $this->addOption($key, $value); } return $this; }
/** * Returns a flat list of all the open and close quotes registered in this instance * * @return Array */ public function getAllQuotes() { $quotes = array_values($this->quotes); $quotes = \r8\ary\flatten($quotes); $quotes = \array_merge($quotes, array_keys($this->quotes)); $quotes = \array_unique($quotes); return \array_values($quotes); }
/** * Splits a string at the given offsets * * Breaks are done right before the given offset * * @param String $string The string being split * @param Integer $offsets... The list of offsets where the string should be split * @return Array Returns an array of the segmented string */ function partition($string, $offsets) { $string = (string) $string; if (strlen($string) <= 0) { return array(); } $offsets = func_get_args(); array_shift($offsets); $offsets = \r8\ary\flatten($offsets); $offsets = \array_map("intval", $offsets); $offsets = \array_unique($offsets); sort($offsets); $out = array(); $last = 0; foreach ($offsets as $break) { if ($break > 0 && $break < strlen($string)) { $out[] = substr($string, $last, $break - $last); $last = $break; } } $out[] = substr($string, $last); return $out; }
/** * Sets the attributes that will be attached to the wrapping node after * it is imported into the document. * * @param array $attrs The attribute arrays to add to the wrapping node * @return \r8\XMLBuilder\Wrap Returns a self reference */ public function setAttributes(array $attrs) { $this->attrs = \r8\ary\flatten($attrs); return $this; }