示例#1
0
 /**
  * Parse the url params in params array searching for some expected params. If someone is found modify the array.
  * $params array is referenced.
  *
  * @internal param array $params Get params.
  * @return array
  */
 protected function parseParams()
 {
     $expected_params = array();
     $expected_url_params = $this->getParamsDefinition();
     if (empty($expected_url_params)) {
         // The controller didn't declare any parameters, nothing to do.
         return array();
     }
     // Build "reverse" keys array.
     foreach ($expected_url_params as $key => $value) {
         $expected_url_keys[$value['internal_key']] = $key;
         // Check if a "default_value" is used when there aren't any parameters in the URL.
         if (isset($expected_url_params[$key]['default_value'])) {
             $expected_params[$key] = $expected_url_params[$key]['default_value'];
         }
     }
     if (empty($expected_params) && (empty($this->params['params']) || empty($expected_url_params))) {
         return array();
     }
     // Expected params:
     $max = count($this->params['params']) - 1;
     // -1 because the last param never is a param key and to avoid asking a non existing key.
     if (count($expected_url_params)) {
         for ($i = 0; $i < $max; $i++) {
             $param = $this->params['params'][$i];
             if (1 == strlen($param) && filter_var($param, FILTER_DEFAULT) && array_key_exists($param, $expected_url_keys) && (false !== filter_var($this->params['params'][$i + 1], FILTER_DEFAULT) && '' != filter_var($this->params['params'][$i + 1], FILTER_DEFAULT))) {
                 $value = $this->params['params'][$i + 1];
                 if ($expected_url_params[$expected_url_keys[$param]]['is_list']) {
                     $value = explode(',', $value);
                 }
                 if ($expected_url_params[$expected_url_keys[$param]]['apply_translation']) {
                     // Save current domain:
                     $current_domain = $this->i18n->getDomain();
                     $this->i18n->setDomain('urlparams', $this->language);
                     if (is_array($value)) {
                         // Try to translate all the params.
                         foreach ($value as &$item) {
                             $item = $this->i18n->getReverseTranslation($item);
                         }
                     } else {
                         $value = $this->i18n->getReverseTranslation($value);
                     }
                     // Restore current domain:
                     $this->i18n->setDomain($current_domain, $this->language);
                 }
                 if (isset($expected_url_params[$expected_url_keys[$param]]['accepted_values'])) {
                     if (is_array($value) && count(array_diff($value, $expected_url_params[$expected_url_keys[$param]]['accepted_values']))) {
                         throw new Exception_404('The value passed in the parameters is not included in the "accepted_values"');
                     }
                     if (!is_array($value) && !in_array($value, $expected_url_params[$expected_url_keys[$param]]['accepted_values'])) {
                         throw new Exception_404('The value passed is the parameters is not included in the "accepted_values"');
                     }
                 }
                 $expected_params[$expected_url_keys[$param]] = $value;
                 unset($this->params['params'][$i]);
                 // Clean param
                 unset($this->params['params'][++$i]);
                 // Clean value
             }
         }
     }
     return $expected_params;
 }