Hoa central
Public Member Functions | Static Public Member Functions | Protected Attributes | Static Protected Attributes | Private Member Functions | List of all members
Hoa\Core\Parameter\Parameter Class Reference

Public Member Functions

 __construct ($owner, Array $keywords=[], Array $parameters=[])
 
 setParameters (Array $parameters)
 
 getParameters ()
 
 setParameter ($key, $value)
 
 getParameter ($parameter)
 
 getFormattedParameter ($parameter)
 
 brancheExists ($branche)
 
 unlinearizeBranche ($branche)
 
 setKeywords ($keywords)
 
 getKeywords ()
 
 setKeyword ($key, $value)
 
 getKeyword ($keyword)
 
 zFormat ($value)
 

Static Public Member Functions

static initializeConstants ()
 
static getConstants ()
 

Protected Attributes

 $_owner = null
 
 $_parameters = []
 
 $_keywords = []
 
 $_cache = []
 

Static Protected Attributes

static $_constants = null
 

Private Member Functions

 setDefault (Array $parameters)
 
 resetCache ()
 

Detailed Description

Class .

Provide a parameter support.

Definition at line 67 of file Parameter.php.

Constructor & Destructor Documentation

Hoa\Core\Parameter\Parameter::__construct (   $owner,
Array  $keywords = [],
Array  $parameters = [] 
)

Construct a new set of parameters.

Parameters
mixed$ownerOwner name or instance.
array$keywordsKeywords.
array$parametersParameters.
Returns
void
Exceptions

Definition at line 115 of file Parameter.php.

119  {
120  if (is_object($owner)) {
121  if (!($owner instanceof Parameterizable)) {
122  throw new Core\Exception(
123  'Only parameterizable object can have parameter; ' .
124  '%s does implement \Hoa\Core\Parameter\Parameterizable.',
125  0,
126  get_class($owner)
127  );
128  }
129 
130  $owner = get_class($owner);
131  } else {
132  $reflection = new \ReflectionClass($owner);
133 
134  if (false === $reflection->implementsInterface('\Hoa\Core\Parameter\Parameterizable')) {
135  throw new Core\Exception(
136  'Only parameterizable object can have parameter; ' .
137  '%s does implement \Hoa\Core\Parameter\Parameterizable.',
138  1,
139  $owner
140  );
141  }
142  }
143 
144  $this->_owner = $owner;
145  $this->setKeywords($keywords);
146  $this->setDefault($parameters);
147 
148  return;
149  }
setDefault(Array $parameters)
Definition: Parameter.php:202

Here is the call graph for this function:

Member Function Documentation

Hoa\Core\Parameter\Parameter::brancheExists (   $branche)

Check a branche exists.

Parameters
string$brancheBranche.
Returns
bool

Definition at line 340 of file Parameter.php.

341  {
342  $qBranche = preg_quote($branche);
343 
344  foreach ($this->getParameters() as $key => $value) {
345  if (0 !== preg_match('#^' . $qBranche . '(.*)?#', $key)) {
346  return true;
347  }
348  }
349 
350  return false;
351  }

Here is the call graph for this function:

static Hoa\Core\Parameter\Parameter::getConstants ( )
static

Get constants.

Returns
array

Definition at line 190 of file Parameter.php.

191  {
192  return self::$_constants;
193  }

Here is the caller graph for this function:

Hoa\Core\Parameter\Parameter::getFormattedParameter (   $parameter)

Get a formatted parameter (i.e. zFormatted).

Parameters
string$parameterParameter.
Returns
mixed

Definition at line 325 of file Parameter.php.

326  {
327  if (null === $value = $this->getParameter($parameter)) {
328  return null;
329  }
330 
331  return $this->zFormat($value);
332  }

Here is the call graph for this function:

Hoa\Core\Parameter\Parameter::getKeyword (   $keyword)

Get a keyword.

Parameters
string$keywordKeyword.
Returns
mixed

Definition at line 453 of file Parameter.php.

454  {
455  if (true === array_key_exists($keyword, $this->_keywords)) {
456  return $this->_keywords[$keyword];
457  }
458 
459  return null;
460  }
Hoa\Core\Parameter\Parameter::getKeywords ( )

Get keywords.

Returns
array

Definition at line 421 of file Parameter.php.

422  {
423  return $this->_keywords;
424  }

Here is the caller graph for this function:

Hoa\Core\Parameter\Parameter::getParameter (   $parameter)

Get a parameter.

Parameters
string$parameterParameter.
Returns
mixed

Definition at line 310 of file Parameter.php.

311  {
312  if (array_key_exists($parameter, $this->_parameters)) {
313  return $this->_parameters[$parameter];
314  }
315 
316  return null;
317  }

Here is the caller graph for this function:

Hoa\Core\Parameter\Parameter::getParameters ( )

Get parameters.

Returns
array

Definition at line 278 of file Parameter.php.

279  {
280  return $this->_parameters;
281  }

Here is the caller graph for this function:

static Hoa\Core\Parameter\Parameter::initializeConstants ( )
static

Initialize constants.

Returns
void

Definition at line 156 of file Parameter.php.

157  {
158  $c = explode('…', date('d…j…N…w…z…W…m…n…Y…y…g…G…h…H…i…s…u…O…T…U'));
159  self::$_constants = [
160  'd' => $c[0],
161  'j' => $c[1],
162  'N' => $c[2],
163  'w' => $c[3],
164  'z' => $c[4],
165  'W' => $c[5],
166  'm' => $c[6],
167  'n' => $c[7],
168  'Y' => $c[8],
169  'y' => $c[9],
170  'g' => $c[10],
171  'G' => $c[11],
172  'h' => $c[12],
173  'H' => $c[13],
174  'i' => $c[14],
175  's' => $c[15],
176  'u' => $c[16],
177  'O' => $c[17],
178  'T' => $c[18],
179  'U' => $c[19]
180  ];
181 
182  return;
183  }
Hoa\Core\Parameter\Parameter::resetCache ( )
private

Reset zFormat cache.

Returns
void

Definition at line 740 of file Parameter.php.

741  {
742  unset($this->_cache);
743  $this->_cache = [];
744 
745  return;
746  }

Here is the caller graph for this function:

Hoa\Core\Parameter\Parameter::setDefault ( Array  $parameters)
private

Set default parameters to a class.

Parameters
array$parametersParameters to set.
Returns
void
Exceptions

Definition at line 202 of file Parameter.php.

203  {
204  if ($this->_owner == 'Hoa\Core\Core') {
205  $class = 'HoaCoreCore';
206  $this->_parameters = $parameters;
207  $path = $this->zFormat(
208  $parameters['protocol.Data/Etc/Configuration']
209  ) . '.Cache' . DS . 'HoaCoreCore.php';
210  } else {
211  $class = str_replace(
212  '\\',
213  '',
214  Core\Consistency::getEntityShortestName($this->_owner)
215  );
216  $path = 'hoa://Data/Etc/Configuration/.Cache/' . $class . '.php';
217  }
218 
219  if (file_exists($path)) {
220  $handle = require $path;
221 
222  if (!is_array($handle)) {
223  throw new Core\Exception(
224  'Strange though it may appear, the configuration cache ' .
225  'file %s appears to be corrupted.',
226  0,
227  $path
228  );
229  }
230 
231  if (!array_key_exists('keywords', $handle)) {
232  throw new Core\Exception(
233  'Need keywords in the configuration cache %s.',
234  1,
235  $path
236  );
237  }
238 
239  if (!array_key_exists('parameters', $handle)) {
240  throw new Core\Exception(
241  'Need parameters in the configuration cache %s.',
242  2,
243  $path
244  );
245  }
246 
247  $this->_keywords = $handle['keywords'];
248  $this->_parameters = $handle['parameters'];
249  } else {
250  $this->_parameters = $parameters;
251  }
252 
253  return;
254  }

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Core\Parameter\Parameter::setKeyword (   $key,
  $value 
)

Set a keyword.

Parameters
string$keyKey.
mixed$valueValue.
Returns
mixed

Definition at line 433 of file Parameter.php.

434  {
435  $this->resetCache();
436  $old = null;
437 
438  if (true === array_key_exists($key, $this->_keywords)) {
439  $old = $this->_keywords[$key];
440  }
441 
442  $this->_keywords[$key] = $value;
443 
444  return $old;
445  }

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Core\Parameter\Parameter::setKeywords (   $keywords)

Set keywords.

Parameters
array$keywordsKeywords.
Returns
void
Exceptions

Definition at line 405 of file Parameter.php.

406  {
407  $this->resetCache();
408 
409  foreach ($keywords as $key => $value) {
410  $this->setKeyword($key, $value);
411  }
412 
413  return;
414  }

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Core\Parameter\Parameter::setParameter (   $key,
  $value 
)

Set a parameter.

Parameters
string$keyKey.
mixed$valueValue.
Returns
mixed

Definition at line 290 of file Parameter.php.

291  {
292  $this->resetCache();
293  $old = null;
294 
295  if (true === array_key_exists($key, $this->_parameters)) {
296  $old = $this->_parameters[$key];
297  }
298 
299  $this->_parameters[$key] = $value;
300 
301  return $old;
302  }

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Core\Parameter\Parameter::setParameters ( Array  $parameters)

Set parameters.

Parameters
array$parameterParameters.
Returns
void

Definition at line 262 of file Parameter.php.

263  {
264  $this->resetCache();
265 
266  foreach ($parameters as $key => $value) {
267  $this->setParameter($key, $value);
268  }
269 
270  return;
271  }

Here is the call graph for this function:

Hoa\Core\Parameter\Parameter::unlinearizeBranche (   $branche)

Unlinearize a branche to an array.

Parameters
string$brancheBranche.
Returns
array

Definition at line 359 of file Parameter.php.

360  {
361  $parameters = $this->getParameters();
362  $out = [];
363  $lBranche = strlen($branche);
364 
365  foreach ($parameters as $key => $value) {
366  if ($branche !== substr($key, 0, $lBranche)) {
367  continue;
368  }
369 
370  $handle = [];
371  $explode = preg_split(
372  '#((?<!\\\)\.)#',
373  substr($key, $lBranche + 1),
374  -1,
375  PREG_SPLIT_NO_EMPTY
376  );
377  $end = count($explode) - 1;
378  $i = $end;
379 
380  while ($i >= 0) {
381  $explode[$i] = str_replace('\\.', '.', $explode[$i]);
382 
383  if ($i != $end) {
384  $handle = [$explode[$i] => $handle];
385  } else {
386  $handle = [$explode[$i] => $this->zFormat($value)];
387  }
388 
389  --$i;
390  }
391 
392  $out = array_merge_recursive($out, $handle);
393  }
394 
395  return $out;
396  }

Here is the call graph for this function:

Hoa\Core\Parameter\Parameter::zFormat (   $value)

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'.

Parameters
string$valueParameter value.
Returns
string
Exceptions

Definition at line 554 of file Parameter.php.

555  {
556  if (!is_string($value)) {
557  return $value;
558  }
559 
560  if (isset($this->_cache[$value])) {
561  return $this->_cache[$value];
562  }
563 
564  if (null === self::$_constants) {
565  self::initializeConstants();
566  }
567 
568  $self = $this;
569  $keywords = $this->getKeywords();
570  $parameters = $this->getParameters();
571 
572  return $this->_cache[$value] = preg_replace_callback(
573  '#\(:(.*?):\)#',
574  function ($match) use ($self, $value, &$keywords, &$parameters) {
575  preg_match(
576  '#([^:]+)(?::(.*))?#',
577  $match[1],
578  $submatch
579  );
580 
581  if (!isset($submatch[1])) {
582  return '';
583  }
584 
585  $out = null;
586  $key = $submatch[1];
587  $word = substr($key, 1);
588 
589  // Call a parameter.
590  if ('%' == $key[0]) {
591  if (false === array_key_exists($word, $parameters)) {
592  throw new Core\Exception(
593  'Parameter %s is not found in parameters.',
594  0,
595  $word
596  );
597  }
598 
599  $handle = $parameters[$word];
600  $out = $self->zFormat($handle);
601  }
602  // Call a constant.
603  elseif ('_' == $key[0]) {
604  $constants = Parameter::getConstants();
605 
606  foreach (str_split($word) as $k => $v) {
607  if (!isset($constants[$v])) {
608  throw new Core\Exception(
609  'Constant char %s is not supported in the ' .
610  'rule %s.',
611  1,
612  [$v, $value]
613  );
614  }
615 
616  $out .= $constants[$v];
617  }
618  }
619  // Call a keyword.
620  else {
621  if (false === array_key_exists($key, $keywords)) {
622  throw new Core\Exception(
623  'Keyword %s is not found in the rule %s.',
624  2,
625  [$key, $value]
626  );
627  }
628 
629  $out = $keywords[$key];
630  }
631 
632  if (!isset($submatch[2])) {
633  return $out;
634  }
635 
636  preg_match_all(
637  '#(h|t|r|e|l|u|U|s(/|%|\#)(.*?)(?<!\\\)\2(.*?)(?:(?<!\\\)\2|$))#',
638  $submatch[2],
639  $flags
640  );
641 
642  if (empty($flags) || empty($flags[1])) {
643  throw new Core\Exception(
644  'Unrecognized format pattern %s in the rule %s.',
645  3,
646  [$match[0], $value]
647  );
648  }
649 
650  foreach ($flags[1] as $i => $flag) {
651  switch ($flag) {
652  case 'h':
653  $out = dirname($out);
654 
655  break;
656 
657  case 't':
658  $out = basename($out);
659 
660  break;
661 
662  case 'r':
663  if (false !== $position = strrpos($out, '.', 1)) {
664  $out = substr($out, 0, $position);
665  }
666 
667  break;
668 
669  case 'e':
670  if (false !== $position = strrpos($out, '.', 1)) {
671  $out = substr($out, $position + 1);
672  }
673 
674  break;
675 
676  case 'l':
677  $out = strtolower($out);
678 
679  break;
680 
681  case 'u':
682  $out = strtoupper($out);
683 
684  break;
685 
686  case 'U':
687  $handle = null;
688 
689  foreach (explode('\\', $out) as $part) {
690  if (null === $handle) {
691  $handle = ucfirst($part);
692  } else {
693  $handle .= '\\' . ucfirst($part);
694  }
695  }
696 
697  $out = $handle;
698 
699  break;
700 
701  default:
702  if (!isset($flags[3]) && !isset($flags[4])) {
703  throw new Core\Exception(
704  'Unrecognized format pattern in the rule %s.',
705  4,
706  $value
707  );
708  }
709 
710  $l = preg_quote($flags[3][$i], '#');
711  $r = $flags[4][$i];
712 
713  switch ($flags[2][$i]) {
714  case '%':
715  $l = '^' . $l;
716 
717  break;
718 
719  case '#':
720  $l .= '$';
721 
722  break;
723  }
724 
725  $out = preg_replace('#' . $l . '#', $r, $out);
726  }
727  }
728 
729  return $out;
730  },
731  $value
732  );
733  }

Here is the call graph for this function:

Here is the caller graph for this function:

Member Data Documentation

Hoa\Core\Parameter\Parameter::$_cache = []
protected

Definition at line 102 of file Parameter.php.

Hoa\Core\Parameter\Parameter::$_constants = null
staticprotected

Definition at line 95 of file Parameter.php.

Hoa\Core\Parameter\Parameter::$_keywords = []
protected

Definition at line 88 of file Parameter.php.

Hoa\Core\Parameter\Parameter::$_owner = null
protected

Definition at line 74 of file Parameter.php.

Hoa\Core\Parameter\Parameter::$_parameters = []
protected

Definition at line 81 of file Parameter.php.


The documentation for this class was generated from the following file: