Esempio n. 1
0
 public function url($name, $params = [])
 {
     if (!isset($this->_named_routes[$name])) {
         throw new InvalidRouteException('Route :name doesnt exist', [':name' => $name]);
     }
     $route = $this->_routes_list[$this->_named_routes[$name]];
     if ($route['is_static']) {
         // This is a static route, no need to replace anything
         // TODO: host?
         return '/' . $route['pattern'];
     }
     // Keep track of whether an optional param was replaced
     $provided_optional = FALSE;
     $uri = $route['pattern'];
     while (preg_match('#\\([^()]++\\)#', $uri, $match)) {
         // Search for the matched value
         $search = $match[0];
         // Remove the parenthesis from the match as the replace
         $replace = substr($match[0], 1, -1);
         while (preg_match('#' . static::REGEX_KEY . '#', $replace, $match)) {
             list($key, $param) = $match;
             if (isset($params[$param]) and $params[$param] !== Arr::get($this->_defaults, $param)) {
                 // Future optional params should be required
                 $provided_optional = TRUE;
                 // Replace the key with the parameter value
                 $replace = str_replace($key, $params[$param], $replace);
             } elseif ($provided_optional) {
                 // Look for a default
                 if (isset($this->_defaults[$param])) {
                     $replace = str_replace($key, $this->_defaults[$param], $replace);
                 } else {
                     // Ungrouped parameters are required
                     throw new InvalidRouteException('Required route parameter not passed: :param', [':param' => $param]);
                 }
             } else {
                 // This group has missing parameters
                 $replace = '';
                 break;
             }
         }
         // Replace the group in the URI
         $uri = str_replace($search, $replace, $uri);
     }
     while (preg_match('#' . static::REGEX_KEY . '#', $uri, $match)) {
         list($key, $param) = $match;
         if (!isset($params[$param])) {
             // Look for a default
             if (isset($this->_defaults[$param])) {
                 $params[$param] = $this->_defaults[$param];
             } else {
                 // Ungrouped parameters are required
                 throw new InvalidRouteException('Required route parameter not passed: :param', [':param' => $param]);
             }
         }
         $uri = str_replace($key, $params[$param], $uri);
     }
     // Trim all extra slashes from the URI
     $uri = preg_replace('#//+#', '/', rtrim($uri, '/'));
     /*if ($this->is_external())
             {
                 // Need to add the host to the URI
                 $host = $this->_defaults['host'];
     
                 if (strpos($host, '://') === FALSE)
                 {
                     // Use the default defined protocol
                     $host = Route::$default_protocol.$host;
                 }
     
                 // Clean up the host and prepend it to the URI
                 $uri = rtrim($host, '/').'/'.$uri;
             }*/
     return '/' . $uri;
 }
Esempio n. 2
0
 /**
  * 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;
 }
Esempio n. 3
0
 public static function message($file, $path = NULL, $default = NULL)
 {
     static $messages;
     if (!isset($messages[$file])) {
         if (!file_exists(path('app') . '/messages/' . $file . '.php')) {
             return;
         }
         // Create a new message list
         $messages[$file] = [];
         $messages[$file] = (include path('app') . '/messages/' . $file . '.php');
     }
     if ($path === NULL) {
         // Return all of the messages
         return $messages[$file];
     } else {
         // Get a message using the path
         return \mii\util\Arr::path($messages[$file], $path, $default);
     }
 }
Esempio n. 4
0
 /**
  * Returns the error messages. If no file is specified, the error message
  * will be the name of the rule that failed. When a file is specified, the
  * message will be loaded from "field/rule", or if no rule-specific message
  * exists, "field/default" will be used. If neither is set, the returned
  * message will be "file/field/rule".
  *
  * By default all messages are translated using the default language.
  * A string can be used as the second parameter to specified the language
  * that the message was written in.
  *
  *     // Get errors from messages/forms/login.php
  *     $errors = $Validation->errors('forms/login');
  *
  * @param   string  $file       file to load error messages from
  * @param   mixed   $translate  translate the message
  * @return  array
  */
 public function errors($file = null, $translate = false)
 {
     if ($file === NULL) {
         // Return the error list
         return $this->_errors;
     }
     // Create a new message list
     $messages = [];
     foreach ($this->_errors as $field => $set) {
         if (is_array($set)) {
             list($error, $params) = $set;
         } else {
             $error = $set;
             $params = [];
         }
         // Get the label for this field
         $label = $this->_labels[$field];
         if ($translate) {
             if (is_string($translate)) {
                 // Translate the label using the specified language
                 $label = __($label, NULL, $translate);
             } else {
                 // Translate the label
                 $label = __($label);
             }
         }
         // Start the translation values list
         $values = [':field' => $label, ':value' => $this->field($field)];
         if (is_array($values[':value'])) {
             // All values must be strings
             $values[':value'] = implode(', ', Arr::flatten($values[':value']));
         }
         if ($params) {
             foreach ($params as $key => $value) {
                 if (is_array($value)) {
                     // All values must be strings
                     $value = implode(', ', Arr::flatten($value));
                 } elseif (is_object($value)) {
                     // Objects cannot be used in message files
                     continue;
                 }
                 // Check if a label for this parameter exists
                 if (isset($this->_labels[$value])) {
                     // Use the label as the value, eg: related field name for "matches"
                     $value = $this->_labels[$value];
                     if ($translate) {
                         if (is_string($translate)) {
                             // Translate the value using the specified language
                             $value = __($value, NULL, $translate);
                         } else {
                             // Translate the value
                             $value = __($value);
                         }
                     }
                 }
                 // Add each parameter as a numbered value, starting from 1
                 $values[':param' . ($key + 1)] = $value;
             }
         }
         if ($message = Arr::path($this->_error_messages, "{$field}.{$error}") and is_string($message)) {
         } elseif ($message = Mii::message($file, "{$field}.{$error}") and is_string($message)) {
             // Found a message for this field and error
         } elseif ($message = Mii::message($file, "{$field}.default") and is_string($message)) {
             // Found a default message for this field
         } elseif ($message = Mii::message($file, $error) and is_string($message)) {
             // Found a default message for this error
         } elseif ($message = Mii::message('validation', $error) and is_string($message)) {
             // Found a default message for this error
         } else {
             // No message exists, display the path expected
             $message = "{$file}.{$field}.{$error}";
         }
         if ($translate) {
             if (is_string($translate)) {
                 // Translate the message using specified language
                 $message = __($message, $values, $translate);
             } else {
                 // Translate the message using the default language
                 $message = __($message);
             }
         } else {
             // Do not translate, just replace the values
             $message = strtr($message, $values);
         }
         // Set the message for this field
         $messages[$field] = $message;
     }
     return $messages;
 }