function test_ToIntArray() { $stringVar = 'string'; // One string to empty array $testVar = $stringVar; Oops_Utils::ToIntArray($testVar); $this->assertEquals(array(), $testVar); $testVar = $stringVar; Oops_Utils::ToIntArray($testVar, false); $this->assertEquals(array(), $testVar); $testVar = $stringVar; Oops_Utils::ToIntArray($testVar, true); $this->assertEquals(array(0), $testVar); $mixedVar = array($stringVar, 0, 10, 2.5, '15', '3.5'); $testVar = $mixedVar; Oops_Utils::ToIntArray($testVar); $this->assertEquals(array(0, 10, 15), $testVar); $testVar = $mixedVar; Oops_Utils::ToIntArray($testVar, false); $this->assertEquals(array(0, 10, 15), $testVar); $testVar = $mixedVar; Oops_Utils::ToIntArray($testVar, true); $this->assertEquals(array(0, 0, 10, 0, 15, 0), $testVar); $mixedVar = array('string' => $stringVar, 'zero' => 0, 10, 'float' => 2.5, 'intstring' => '15', 'floatstring' => '3.5'); $testVar = $mixedVar; Oops_Utils::ToIntArray($testVar); $this->assertEquals(array('zero' => 0, 10, 'intstring' => 15), $testVar); $testVar = $mixedVar; Oops_Utils::ToIntArray($testVar, false); $this->assertEquals(array('zero' => 0, 10, 'intstring' => 15), $testVar); $testVar = $mixedVar; Oops_Utils::ToIntArray($testVar, true); $this->assertEquals(array('string' => 0, 'zero' => 0, 10, 'float' => 0, 'intstring' => 15, 'floatstring' => 0), $testVar); }
/** * Get requested value, modified to the requested type * * @param string $key * request key * @param string $type * required value type * @param mixed $default * default value * @return mixed * @tutorial Oops/Oops/Controller.cls#handling_request */ function Request($key, $type = null, $default = null) { if (!strlen($key)) { return false; } if (is_null($value = $this->_request->get($key))) { return $default; } if (is_null($type)) { return $value; } switch (strtolower($type)) { case 'bool': case 'boolean': return (bool) $value; case 'int': case 'integer': return (int) $value; case 'float': case 'double': case 'numeric': case 'decimal': return (double) $value; case 'array': require_once 'Oops/Utils.php'; Oops_Utils::ToArray($value); return $value; case 'arrayint': require_once 'Oops/Utils.php'; Oops_Utils::ToIntArray($value); return $value; case 'arraysql': require_once 'Oops/Utils.php'; Oops_Utils::ToIntArray($value); return $value; case 'sql': require_once 'Oops/Sql.php'; return Oops_Sql::Escape($value); case 'words': return preg_replace('/[^\\s\\w]/', '', $value); case 'trimmedwords': return trim(preg_replace('/[^\\s\\w]/', '', $value)); default: return $value; } }