FixedListStrategy.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2015 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 
16 
17 /**
18  * FixedListStrategy implements a constant list of key/value pairs.
19  *
20  * Configuration examples:
21  * @code
22  * // list with only values (the keys will be the same as the values)
23  * {"type":"fix","items":["val1","val2"]}
24  *
25  * // list with explicit key/value pairs
26  * {"type":"fix","items":{"key1":"val1","key2":"val2"}}
27  *
28  * // list with key/value pairs defined in a global variable
29  * {"type":"fix","items":"$global_array_variable"}
30  * @endcode
31  *
32  * @author ingo herwig <ingo@wemove.com>
33  */
34 class FixedListStrategy implements ListStrategy {
35 
36  /**
37  * @see ListStrategy::getList
38  * $options is an associative array with keys 'items'
39  */
40  public function getList($options, $language=null) {
41  if (!isset($options['items'])) {
42  throw new ConfigurationException("No 'items' given in list options: "+$options);
43  }
44  $items = $options['items'];
45 
46  // check if we have an array variable or a list definition
47  if (!is_array($items) && strPos($options, '$') === 0) {
48  $items = $GLOBALS[subStr($options, 1)];
49  if (!is_array($items)) {
50  throw new ConfigurationException($options['items']." is no array.");
51  }
52  }
53  // check if we only have values and need to create keys
54  else if (array_values($items) === $items) {
55  $items = array_combine($items, $items);
56  }
57 
58  // translate values
59  $result = array();
61  foreach ($items as $key => $value) {
62  $result[$key] = $message->getText($value, null, $language);
63  }
64  return $result;
65  }
66 
67  /**
68  * @see ListStrategy::isStatic
69  */
70  public function isStatic($options) {
71  return true;
72  }
73 }
74 ?>
FixedListStrategy implements a constant list of key/value pairs.
static getInstance($name, $dynamicConfiguration=array())
$message
Predefined errors.
ListStrategy defines the interface for classes that retrieve value lists.
ConfigurationException signals an exception in the configuration.