FixedListStrategy.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
12 
17 
18 /**
19  * FixedListStrategy implements a constant list of key/value pairs.
20  *
21  * Configuration examples:
22  * @code
23  * // list with only values (the keys will be the same as the values)
24  * {"type":"fix","items":["val1","val2"]}
25  *
26  * // list with explicit key/value pairs
27  * {"type":"fix","items":{"key1":"val1","key2":"val2"}}
28  *
29  * // list with key/value pairs defined in a global variable
30  * {"type":"fix","items":"$global_array_variable"}
31  * @endcode
32  *
33  * @author ingo herwig <ingo@wemove.com>
34  */
35 class FixedListStrategy implements ListStrategy {
36 
37  /**
38  * @see ListStrategy::getList
39  * $options is an associative array with keys 'items'
40  */
41  public function getList($options, $valuePattern=null, $key=null, $language=null) {
42  if (!isset($options['items'])) {
43  throw new ConfigurationException("No 'items' given in list options: "+StringUtil::getDump($options));
44  }
45  $items = $options['items'];
46 
47  // check if we have an array variable or a list definition
48  if (!is_array($items) && strPos($options, '$') === 0) {
49  $items = $GLOBALS[subStr($options, 1)];
50  if (!is_array($items)) {
51  throw new ConfigurationException("'items' option is no array.");
52  }
53  }
54  // check if we only have values and need to create keys
55  else if (array_values($items) === $items) {
56  $items = array_combine($items, $items);
57  }
58 
59  // translate values
60  $result = [];
61  $message = ObjectFactory::getInstance('message');
62  foreach ($items as $curKey => $curValue) {
63  $displayValue = $message->getText($curValue, null, $language);
64  if ((!$valuePattern || preg_match($valuePattern, $displayValue)) && (!$key || $key == $curKey)) {
65  $result[$curKey] = $displayValue;
66  }
67  }
68  return $result;
69  }
70 
71  /**
72  * @see ListStrategy::isStatic
73  */
74  public function isStatic($options) {
75  return true;
76  }
77 }
78 ?>
FixedListStrategy implements a constant list of key/value pairs.
static getDump($variable, $strlen=100, $width=25, $depth=10, $i=0, &$objects=[])
Get the dump of a variable as string.
Definition: StringUtil.php:29
ListStrategy defines the interface for classes that retrieve value lists.
StringUtil provides support for string manipulation.
Definition: StringUtil.php:18
ConfigurationException signals an exception in the configuration.
static getInstance($name, $dynamicConfiguration=[])
getList($options, $valuePattern=null, $key=null, $language=null)
ObjectFactory implements the service locator pattern by wrapping a Factory instance and providing sta...