ValueListProvider.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 
15 use wcmf\lib\util\StringUtil;
16 
17 /**
18  * ValueListProvider provides lists of key/values to be used
19  * with list input controls.
20  *
21  * @author ingo herwig <ingo@wemove.com>
22  */
24 
25  /**
26  * Array of known list strategy instances
27  */
28  private static $listStrategies = null;
29 
30  /**
31  * Get a list of key/value pairs defined by the given configuration.
32  * @param $definition The list definition as given in the input_type definition
33  * in the 'list' parameter (e.g. '{"type":"config","section":"EntityStage"}')
34  * @param $valuePattern A regular expression pattern that the returned values should match (optional)
35  * @param $key A key value, if only one item should be returned (optional)
36  * @param $language The language if the values should be localized (optional, default: Localization::getDefaultLanguage())
37  * @return An assoziative array with keys 'items' (array of arrays with keys 'key' and 'value'),
38  * 'isStatic' (indicating if the list may change of not)
39  */
40  public static function getList($definition, $valuePattern=null, $key=null, $language=null) {
41 
42  $decodedDefinition = json_decode($definition, true);
43  if ($decodedDefinition === null) {
44  throw new ConfigurationException("No valid JSON format: ".$definition);
45  }
46  if (!isset($decodedDefinition['type'])) {
47  throw new ConfigurationException("No 'type' given in list definition: ".$definition);
48  }
49 
50  // get the strategy
51  $strategy = self::getListStrategy($decodedDefinition['type']);
52 
53  // add empty item, if defined
54  $items = [];
55  if (isset($decodedDefinition['emptyItem'])) {
56  $emtpyItemText = $decodedDefinition['emptyItem'];
57  $items[] = [
58  'key' => null,
59  'value' => ObjectFactory::getInstance('message')->getText($emtpyItemText, null, $language)
60  ];
61  }
62 
63  // build list
64  foreach($strategy->getList($decodedDefinition, $valuePattern, $key, $language) as $key => $value) {
65  $items[] = ['key' => $key, 'value' => $value];
66  }
67 
68  return [
69  'items' => $items,
70  'isStatic' => $strategy->isStatic($decodedDefinition)
71  ];
72  }
73 
74  /**
75  * Translate a value with use of it's assoziated input type e.g. get the location string from a location id.
76  * (this is only done when the input type has a list definition).
77  * @param $value The value to translate (might be a comma separated list for list controls)
78  * @param $inputType The description of the control as given in the 'input_type' property of a value
79  * @param $language The language if the value should be localized. Optional,
80  * default is Localization::getDefaultLanguage()
81  * @param $itemDelim Delimiter string for array values (optional, default: ", ")
82  * @return String
83  */
84  public static function translateValue($value, $inputType, $language=null, $itemDelim=", ") {
85  // get definition and list from inputType
86  if (strPos($inputType, ':') && strlen($value) > 0) {
87  $translated = '';
88  list(, $options) = preg_split('/:/', $inputType, 2);
89  $decodedOptions = json_decode($options, true);
90  if ($decodedOptions === null) {
91  throw new ConfigurationException("No valid JSON format: ".$options);
92  }
93  if (isset($decodedOptions['list'])) {
94  $listDef = $decodedOptions['list'];
95  $list = self::getList(json_encode($listDef), null, null, $language);
96  $items = $list['items'];
97  // only split, if it's an array to avoid casting null values to strings
98  if (strPos($value, ',')) {
99  $value = preg_split('/,/', $value);
100  }
101  if (is_array($value)) {
102  $translatedItems = [];
103  foreach($value as $curValue) {
104  $curValue = trim($curValue);
105  $translatedItems[] = self::getItemValue($items, $curValue);
106  }
107  $translated = join($itemDelim, $translatedItems);
108  }
109  else {
110  $translated = self::getItemValue($items, $value);
111  }
112  return $translated;
113  }
114  }
115  return $value;
116  }
117 
118  /**
119  * Get the ListStrategy instance for a given list type
120  * @param $listType The list type
121  * @return ListStrategy instance
122  * @throws ConfigurationException
123  */
124  protected static function getListStrategy($listType) {
125  // get list strategies
126  if (self::$listStrategies == null) {
127  self::$listStrategies = ObjectFactory::getInstance('listStrategies');
128  }
129 
130  $strategy = null;
131 
132  // search strategy
133  if (isset(self::$listStrategies[$listType])) {
134  $strategy = self::$listStrategies[$listType];
135  }
136  else {
137  throw new ConfigurationException('No ListStrategy implementation registered for '.$listType);
138  }
139  return $strategy;
140  }
141 
142  /**
143  * Get the value of the item with the given key. Returns the key, if it does
144  * not exist in the list.
145  * @param $list Array of associative arrays with keys 'key' and 'value'
146  * @param $key The key to search
147  * @return String
148  */
149  protected static function getItemValue($list, $key) {
150  foreach ($list as $item) {
151  // strict comparison for null value
152  if (($key === null && $item['key'] === $key) || ($key !== null && strval($item['key']) === strval($key))) {
153  return $item['value'];
154  }
155  }
156  return $key;
157  }
158 }
159 ?>
static getListStrategy($listType)
Get the ListStrategy instance for a given list type.
ConfigurationException signals an exception in the configuration.
ValueListProvider provides lists of key/values to be used with list input controls.
static getItemValue($list, $key)
Get the value of the item with the given key.
static getInstance($name, $dynamicConfiguration=[])
static getList($definition, $valuePattern=null, $key=null, $language=null)
Get a list of key/value pairs defined by the given configuration.
ObjectFactory implements the service locator pattern by wrapping a Factory instance and providing sta...
static translateValue($value, $inputType, $language=null, $itemDelim=", ")
Translate a value with use of it's assoziated input type e.g.