/** * Prepares a value for a SQL statement * * @param mixed $value The value to prepare * @param Boolean $allowNull Whether NULL is an acceptable value * @param Callback $onString The function to invoke if the value * is a string that needs to be escaped * @return String Returns the cleansed value */ public static function cleanseValue($value, $allowNull, $onString) { if (!is_callable($onString)) { throw new \r8\Exception\Argument(0, "onString Callback", "Must be Callable"); } if (is_array($value)) { $result = array(); foreach ($value as $key => $toCleanse) { $result[$key] = self::cleanseValue($toCleanse, $allowNull, $onString); } return $result; } $value = \r8\reduce($value); switch (gettype($value)) { case "boolean": return $value ? "1" : "0"; case "integer": case "double": return (string) $value; case "NULL": return $allowNull ? "NULL" : call_user_func($onString, ""); default: case "string": if (is_numeric($value) && !preg_match('/[^0-9\\.]/', $value)) { return $value; } return call_user_func($onString, $value); } }
public function testReduce() { $this->assertFalse(\r8\reduce(FALSE)); $this->assertTrue(\r8\reduce(TRUE)); $this->assertNull(\r8\reduce(NULL)); $this->assertEquals(270, \r8\reduce(270)); $this->assertEquals(151.12, \r8\reduce(151.12)); $this->assertEquals(151.12, \r8\reduce(array(151.12, 150))); $this->assertEquals(151.12, \r8\reduce(array(array(151.12, 150)))); $this->assertEquals("String of stuff", \r8\reduce("String of stuff")); $this->assertEquals("stream", \r8\reduce(STDOUT)); }
/** * Converts the given value to an integer * * @param Mixed $value The value to filter * @return Boolean */ public function filter($value) { if (is_array($value)) { if (count($value) == 0) { return 0; } else { $value = \r8\reduce($value); } } if (is_string($value)) { $value = filter_var($value, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION); } else { if (is_object($value)) { return 1; } } return (int) $value; }
/** * Returns the SQL this atom represents * * @param \r8\iface\DB\Link $link The database connection this atom * is being created against. This is being passed in for escaping * purposes * @return String */ public function toAtomSQL(\r8\iface\DB\Link $link) { return $link->quote(\r8\reduce($this->value)); }
/** * Sets the value for this field * * This does not apply the filter when saving, however it will convert any * objects or arrays using the \r8\reduce function * * @param mixed $value The value of this field * @return \r8\Form\Field Returns a self reference */ public function setValue($value) { $this->value = \r8\reduce($value); return $this; }
/** * Sets a specific HTML attribute * * @param String $attr The attribute being set * @param mixed $value The value of this attribute * Setting this to boolean true (or not passing it as an argument), * marks this attribute as a boolean value * @param \r8\HTML\Tag Returns a self reference */ public function setAttr($attr, $value = TRUE) { $this->attrs[self::normalizeAttrName($attr)] = \r8\reduce($value); return $this; }
/** * Constructor... * * @param Mixed $start The starting value * @param Mixed $end The ending value * @param Integer $step The size of the step to take between values */ public function __construct($start, $end, $step = 1) { $start = \r8\reduce($start); $end = \r8\reduce($end); if (ctype_alpha($start) && ctype_alpha($end)) { if (ctype_upper(substr($start, 0, 1))) { $this->mode = self::MODE_ALPHA_UPPER; } else { $this->mode = self::MODE_ALPHA_LOWER; } $this->start = self::alpha2num((string) $start); $this->end = self::alpha2num((string) $end); } else { $this->mode = self::MODE_NUMERIC; $this->start = \r8\numVal($start); $this->end = \r8\numVal($end); } $this->step = (int) $step == 0 ? 1 : $step; }
/** * Prepares a value to be used as an array index * * This will reduce the value down to a basic type, then convert it to an integer * or a string. The idea is to simulate what PHP does when it uses a non-standard * value as an array index. * * @param mixed $value The value to convert * @return String|Integer */ function indexVal($value) { $value = \r8\reduce($value); if (is_string($value) || is_int($value)) { return $value; } else { if (is_float($value) || is_bool($value)) { return (int) $value; } else { return (string) $value; } } }
/** * Sets an index/filter pair in this instance * * This will overwrite any previous filters for the given index * * @param mixed $index The index this filter will be applied to * @param \r8\iface\Filter $filter The filter to apply to the given index * @return \r8\Filter\AryOffset Returns a self reference */ public function setFilter($index, \r8\iface\Filter $filter) { $index = \r8\reduce($index); $this->filters[$index] = $filter; return $this; }
/** * Prepends a value to the end of an existing cached value. * * If the value doesn't exist, it will be set with the given value * * Due to limitations in the APC interface, this is not an atomic operation. * Instead, it is a two step process. The value is pulled from the cache, * updated and then saved. This means that race conditions can possibly * occur in the momemnt between the read and write. * * @param String $key The key for the value * @param mixed $value The value to prepend * @param Integer $expire The lifespan of this cache value, in seconds * @return \r8\Cache\Memcache Returns a self reference */ public function prepend($key, $value, $expire = 0) { return $this->set($key, \r8\reduce($value) . \r8\reduce($this->get($key)), $expire); }
/** * calculates the offset based on the wrap flag * * This is generally used by array functions to wrap offsets * * @param Integer $length Starting from 1 (not 0), the length of the list being wrapped around * @param Integer $offset The offset being wrapped * @param Integer $wrapFlag How to handle offsets that fall outside of the length of the list. * @return Integer|Boolean Returns the wrapped offset. Returns FALSE on failure */ function offsetWrap($length, $offset, $wrapFlag) { $length = (int) $length; if ($length <= 0) { throw new \r8\Exception\Index($offset, "Offset", "List is empty"); } $offset = (int) \r8\reduce($offset); switch ($wrapFlag) { default: throw new \r8\Exception\Argument(2, "wrapFlag", "Invalid offset wrap flag"); case \r8\num\OFFSET_NONE: if (!\r8\num\between($offset, 0 - $length, $length - 1)) { throw new \r8\Exception\Index($offset, "Offset", "Offset is out of bounds"); } else { if ($offset >= 0) { return $offset; } else { return $length + $offset; } } case \r8\num\OFFSET_WRAP: return \r8\num\intWrap($offset, 0, $length - 1); case FALSE: case \r8\num\OFFSET_RESTRICT: $offset = \r8\num\limit($offset, 0 - $length, $length - 1); if ($offset < 0) { $offset = $length + $offset; } return $offset; case \r8\num\OFFSET_LIMIT: return \r8\num\limit($offset, 0, $length - 1); } }