/** * Merges the current GET parameters with an array of new or overloaded * parameters and returns the resulting query string. * * // Returns "?sort=title&limit=10" combined with any existing GET values * $query = URL::query(array('sort' => 'title', 'limit' => 10)); * * Typically you would use this when you are sorting query results, * or something similar. * * [!!] Parameters with a NULL value are left out. * * @param array $params Array of GET parameters * @param boolean $use_get Include current request GET parameters * @return string */ public static function query(array $params = NULL, $use_get = TRUE) { if ($use_get) { if ($params === NULL) { // Use only the current parameters $params = $_GET; } else { // Merge the current and new parameters $params = Arr::merge($_GET, $params); } } if (empty($params)) { // No query parameters return ''; } // Note: http_build_query returns an empty string for a params array with only NULL values $query = http_build_query($params, '', '&'); // Don't prepend '?' to an empty string return $query === '' ? '' : '?' . $query; }
/** * Executes all validation rules. This should * typically be called within an if/else block. * * if ($validation->check()) * { * // The data is valid, do something here * } * * @return boolean */ public function check() { $benchmark = false; if (config('debug')) { // Start a new benchmark $benchmark = \mii\util\Profiler::start('Validation', __FUNCTION__); } $this->_errors = []; // Get a list of the expected fields $expected = Arr::merge(array_keys($this->_data), array_keys($this->_labels)); // Import the rules locally $rules = $this->_rules; $data = []; foreach ($expected as $field) { // Use the submitted value or NULL if no data exists $data[$field] = isset($this->_data[$field]) ? $this->_data[$field] : null; } // Overload the current array with the new one $this->_data = $data; // Execute the rules foreach ($rules as $field => $set) { // Get the field value $value = $this->_data[$field]; foreach ($set as $array) { // Rules are defined as array($rule, $params) list($rule, $params) = $array; array_unshift($params, $value); // Default the error name to be the rule (except array and lambda rules) $error_name = $rule; if (is_array($rule)) { // Allows rule('field', array(':model', 'some_rule')); if (is_string($rule[0]) and array_key_exists($rule[0], $this->_bound)) { // Replace with bound value $rule[0] = $this->_bound[$rule[0]]; } // This is an array callback, the method name is the error name $error_name = $rule[1]; $passed = call_user_func_array($rule, $params); } elseif (!is_string($rule)) { // This is a lambda function, there is no error name (errors must be added manually) $error_name = FALSE; array_unshift($params, $field); array_unshift($params, $this); $passed = call_user_func_array($rule, $params); } elseif (method_exists('mii\\valid\\Rules', $rule)) { // Use a method in this object $method = new \ReflectionMethod('mii\\valid\\Rules', $rule); // Call static::$rule($this[$field], $param, ...) with Reflection $passed = $method->invokeArgs(NULL, $params); } elseif (strpos($rule, '::') === FALSE) { // Use a function call $function = new \ReflectionFunction($rule); // Call $function($this[$field], $param, ...) with Reflection $passed = $function->invokeArgs($params); } else { // Split the class and method of the rule list($class, $method) = explode('::', $rule, 2); // Use a static method call $method = new \ReflectionMethod($class, $method); // Call $Class::$method($this[$field], $param, ...) with Reflection $passed = $method->invokeArgs(NULL, $params); } // Ignore return values from rules when the field is empty if (!in_array($rule, $this->_empty_rules) and !Rules::not_empty($value)) { continue; } if ($passed === FALSE and $error_name !== FALSE) { // Add the rule to the errors $this->error($field, $error_name, $params); // This field has an error, stop executing rules break; } elseif (isset($this->_errors[$field])) { // The callback added the error manually, stop checking rules break; } } } if ($benchmark) { // Stop benchmarking \mii\util\Profiler::stop($benchmark); } return empty($this->_errors); }