Exemple #1
0
 /**
  * Dispatch a router rule.
  *
  * @param   \Hoa\Router         $router    Router.
  * @param   \Hoa\View\Viewable  $view      View.
  * @return  mixed
  * @throws  \Hoa\Controller\Exception
  */
 public function dispatch(Router $router, View\Viewable $view = null)
 {
     $rule = $router->getTheRule();
     if (null === $rule) {
         $router->route();
         $rule = $router->getTheRule();
     }
     if (null === $view) {
         $view = $this->_currentView;
     } else {
         $this->_currentView = $view;
     }
     $parameters = $this->_parameters;
     $this->_parameters = clone $this->_parameters;
     foreach ($rule[Router::RULE_VARIABLES] as $key => $value) {
         $this->_parameters->setParameter('variables.' . $key, $value);
     }
     $out = $this->resolve($rule, $router, $view);
     unset($this->_parameters);
     $this->_parameters = $parameters;
     return $out;
 }
Exemple #2
0
 /**
  * Generate a continuous uniform distribution.
  *
  * @param   float   $lower    Lower bound value.
  * @param   float   $upper    Upper bound value.
  * @return  float
  */
 public function getFloat($lower = null, $upper = null)
 {
     if (null === $lower) {
         $lower = $this->_parameters->getParameter('float.min');
     }
     /*
     $lower = true === S_32\BITS
                  ? -3.4028235e38 + 1
                  : -1.7976931348623157e308 + 1;
     */
     if (null === $upper) {
         $upper = $this->_parameters->getParameter('float.max');
     }
     /*
     $upper = true === S_32\BITS
                  ? 3.4028235e38 - 1
                  : 1.7976931348623157e308 - 1;
     */
     if ($lower > $upper) {
         throw new Math\Exception('Unexpected values, float %f should be lower than %f', 2, [$lower, $upper]);
     }
     return $this->_getFloat($lower, $upper);
 }
Exemple #3
0
 /**
  * Set the current ID.
  *
  * @param   string  $id    ID.
  * @return  string
  */
 protected function setId($id)
 {
     $old = $this->_parameters->getKeyword('id');
     $this->_parameters->setKeyword('id', $id);
     return $old;
 }
Exemple #4
0
 /**
  * zFormat a string.
  * zFormat is inspired from the famous Zsh (please, take a look at
  * http://zsh.org), and specifically from ZStyle.
  *
  * ZFormat has the following pattern:
  *     (:subject[:format]:)
  *
  * where subject could be a:
  *   • keyword, i.e. a simple string: foo;
  *   • reference to an existing parameter, i.e. a simple string prefixed by
  *     a %: %bar;
  *   • constant, i.e. a combination of chars, first is prefixed by a _: _Ymd
  *     will given the current year, followed by the current month and
  *     finally the current day.
  *
  * and where the format is a combination of chars, that apply functions on
  * the subject:
  *   • h: to get the head of a path (equivalent to dirname);
  *   • t: to get the tail of a path (equivalent to basename);
  *   • r: to get the path without extension;
  *   • e: to get the extension;
  *   • l: to get the result in lowercase;
  *   • u: to get the result in uppercase;
  *   • U: to get the result with the first letter in uppercase (understand
  *        classname);
  *   • s/<foo>/<bar>/: to replace all matches <foo> by <bar> (the last / is
  *     optional, only if more options are given after);
  *   • s%<foo>%<bar>%: to replace the prefix <foo> by <bar> (the last % is
  *     also optional);
  *   • s#<foo>#<bar>#: to replace the suffix <foo> by <bar> (the last # is
  *     also optional).
  *
  * Known constants are:
  *   • d: day of the month, 2 digits with leading zeros;
  *   • j: day of the month without leading zeros;
  *   • N: ISO-8601 numeric representation of the day of the week;
  *   • w: numeric representation of the day of the week;
  *   • z: the day of the year (starting from 0);
  *   • W: ISO-8601 week number of year, weeks starting on Monday;
  *   • m: numeric representation of a month, with leading zeros;
  *   • n: numeric representation of a month, without leading zeros;
  *   • Y: a full numeric representation of a year, 4 digits;
  *   • y: a two digit representation of a year;
  *   • g: 12-hour format of an hour without leading zeros;
  *   • G: 24-hour format of an hour without leading zeros;
  *   • h: 12-hour format of an hour with leading zeros;
  *   • H: 24-hour format of an hour with leading zeros;
  *   • i: minutes with leading zeros;
  *   • s: seconds with leading zeros;
  *   • u: microseconds;
  *   • O: difference to Greenwich time (GMT) in hours;
  *   • T: timezone abbreviation;
  *   • U: seconds since the Unix Epoch (a timestamp).
  * They are very usefull for dynamic cache paths for example.
  *
  * Examples:
  *   Let keywords $k and parameters $p:
  *     $k = [
  *         'foo'      => 'bar',
  *         'car'      => 'DeLoReAN',
  *         'power'    => 2.21,
  *         'answerTo' => 'life_universe_everything_else',
  *         'answerIs' => 42,
  *         'hello'    => 'wor.l.d'
  *     ];
  *     $p = [
  *         'plpl'        => '(:foo:U:)',
  *         'foo'         => 'ar(:%plpl:)',
  *         'favoriteCar' => 'A (:car:l:)!',
  *         'truth'       => 'To (:answerTo:ls/_/ /U:) is (:answerIs:).',
  *         'file'        => '/a/file/(:_Ymd:)/(:hello:trr:).(:power:e:)',
  *         'recursion'   => 'oof(:%foo:s#ar#az:)'
  *     ];
  *   Then, after applying the zFormat, we get:
  *     • plpl:        'Bar', put the first letter in uppercase;
  *     • foo:         'arBar', call the parameter plpl;
  *     • favoriteCar: 'A delorean!', all is in lowercase;
  *     • truth:       'To Life universe everything else is 42', all is in
  *                    lowercase, then replace underscores by spaces, and
  *                    finally put the first letter in uppercase; and no
  *                    transformation for 42;
  *     • file:        '/a/file/20090505/wor.21', get date constants, then
  *                    get the tail of the path and remove extension twice,
  *                    and add the extension of power;
  *     • recursion:   'oofarBaz', get 'arbar' first, and then, replace the
  *                    suffix 'ar' by 'az'.
  *
  * @param   string  $value    Parameter value.
  * @return  string
  * @throws  \Hoa\Zformat\Exception
  */
 public function zFormat($value)
 {
     if (!is_string($value)) {
         return $value;
     }
     if (isset($this->_cache[$value])) {
         return $this->_cache[$value];
     }
     if (null === self::$_constants) {
         self::initializeConstants();
     }
     $self = $this;
     $keywords = $this->getKeywords();
     $parameters = $this->getParameters();
     return $this->_cache[$value] = preg_replace_callback('#\\(:(.*?):\\)#', function ($match) use($self, $value, &$keywords, &$parameters) {
         preg_match('#([^:]+)(?::(.*))?#', $match[1], $submatch);
         if (!isset($submatch[1])) {
             return '';
         }
         $out = null;
         $key = $submatch[1];
         $word = substr($key, 1);
         // Call a parameter.
         if ('%' == $key[0]) {
             if (false === array_key_exists($word, $parameters)) {
                 throw new Exception('Parameter %s is not found in parameters.', 0, $word);
             }
             $handle = $parameters[$word];
             $out = $self->zFormat($handle);
         } elseif ('_' == $key[0]) {
             $constants = Parameter::getConstants();
             foreach (str_split($word) as $k => $v) {
                 if (!isset($constants[$v])) {
                     throw new Exception('Constant char %s is not supported in the ' . 'rule %s.', 1, [$v, $value]);
                 }
                 $out .= $constants[$v];
             }
         } else {
             if (false === array_key_exists($key, $keywords)) {
                 throw new Exception('Keyword %s is not found in the rule %s.', 2, [$key, $value]);
             }
             $out = $keywords[$key];
         }
         if (!isset($submatch[2])) {
             return $out;
         }
         preg_match_all('#(h|t|r|e|l|u|U|s(/|%|\\#)(.*?)(?<!\\\\)\\2(.*?)(?:(?<!\\\\)\\2|$))#', $submatch[2], $flags);
         if (empty($flags) || empty($flags[1])) {
             throw new Exception('Unrecognized format pattern %s in the rule %s.', 3, [$match[0], $value]);
         }
         foreach ($flags[1] as $i => $flag) {
             switch ($flag) {
                 case 'h':
                     $out = dirname($out);
                     break;
                 case 't':
                     $out = basename($out);
                     break;
                 case 'r':
                     if (false !== ($position = strrpos($out, '.', 1))) {
                         $out = substr($out, 0, $position);
                     }
                     break;
                 case 'e':
                     if (false !== ($position = strrpos($out, '.', 1))) {
                         $out = substr($out, $position + 1);
                     }
                     break;
                 case 'l':
                     $out = strtolower($out);
                     break;
                 case 'u':
                     $out = strtoupper($out);
                     break;
                 case 'U':
                     $handle = null;
                     foreach (explode('\\', $out) as $part) {
                         if (null === $handle) {
                             $handle = ucfirst($part);
                         } else {
                             $handle .= '\\' . ucfirst($part);
                         }
                     }
                     $out = $handle;
                     break;
                 default:
                     if (!isset($flags[3]) && !isset($flags[4])) {
                         throw new Exception('Unrecognized format pattern in the rule %s.', 4, $value);
                     }
                     $l = preg_quote($flags[3][$i], '#');
                     $r = $flags[4][$i];
                     switch ($flags[2][$i]) {
                         case '%':
                             $l = '^' . $l;
                             break;
                         case '#':
                             $l .= '$';
                             break;
                     }
                     $out = preg_replace('#' . $l . '#', $r, $out);
             }
         }
         return $out;
     }, $value);
 }