FunctionListStrategy.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  * FunctionListStrategy implements a list of key/value pairs that is retrieved
20  * by a global function.
21  *
22  * Configuration examples:
23  * @code
24  * // key/value pairs provided by global function g_getListValues
25  * {"type":"function","name":"g_getListValues"}
26  *
27  * // key/value pairs provided by global function g_getListValues with parameters
28  * {"type":"function","name":"g_getListValues","params":["param1","param2"]}
29  *
30  * // key/value pairs provided by static method getValues in class ListValueProvider
31  * {"type":"function","name":"name\\\\space\\\\ListValueProvider::getValues"}
32  * @endcode
33  *
34  * @author ingo herwig <ingo@wemove.com>
35  */
37 
38  /**
39  * @see ListStrategy::getList
40  * $options is an associative array with keys 'name' and 'params' (optional)
41  */
42  public function getList($options, $valuePattern=null, $key=null, $language=null) {
43  if (!isset($options['name'])) {
44  throw new ConfigurationException("No 'name' given in list options: "+StringUtil::getDump($options));
45  }
46  $name = $options['name'];
47  $params = isset($options['params']) ? $options['params'] : null;
48 
49  $nameParts = explode('::', $options['name']);
50  if (count($nameParts) == 1 && function_exists($nameParts[0])) {
51  $functionName = $nameParts[0];
52  $map = call_user_func_array($functionName, $params === null ? [] : $params);
53  }
54  elseif (count($nameParts) == 2 && method_exists($nameParts[0], $nameParts[1])) {
55  $className = $nameParts[0];
56  $methodName = $nameParts[1];
57  $map = $params === null ? $className::$methodName() : $className::$methodName(...$params);
58  }
59  else {
60  throw new ConfigurationException('Function or method '.$name.' is not defined!');
61  }
62 
63  // translate values
64  $result = [];
65  $message = ObjectFactory::getInstance('message');
66  foreach ($map as $curKey => $curValue) {
67  $displayValue = $message->getText($curValue, null, $language);
68  if ((!$valuePattern || preg_match($valuePattern, $displayValue)) && (!$key || $key == $curKey)) {
69  $result[$curKey] = $displayValue;
70  }
71  }
72  return $result;
73  }
74 
75  /**
76  * @see ListStrategy::isStatic
77  */
78  public function isStatic($options) {
79  return false;
80  }
81 }
82 ?>
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
getList($options, $valuePattern=null, $key=null, $language=null)
ConfigurationException signals an exception in the configuration.
static getInstance($name, $dynamicConfiguration=[])
FunctionListStrategy implements a list of key/value pairs that is retrieved by a global function.
ObjectFactory implements the service locator pattern by wrapping a Factory instance and providing sta...