Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.ConfigurationStore.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * ConfigurationStore handles (de)serialization of the
8  * DocFam->configuration property from/to JSON into a array() struct
9  * called "store".
10  */
12 {
13  private $store = array();
14  /**
15  * Load a configuration JSON string into the corresponding array() struct
16  *
17  * @param string $str The configuration's JSON string
18  * @return bool boolean true on success, or boolean false on error
19  */
20  public function load($str)
21  {
22  $codec = new JSONCodec();
23  try {
24  $store = $codec->decode($str, true);
25  }
26  catch(Exception $e) {
27  return false;
28  }
29  $this->store = $store;
30  return true;
31  }
32  /**
33  * Reset the array() struct
34  */
35  public function reset()
36  {
37  $this->store = array();
38  }
39  /**
40  * Add a configuration parameter into the store
41  *
42  * @param string $class The parameter's class
43  * @param string $propName The property's name
44  * @param string $pName The parameter's name
45  * @param string $pValue The parameter's value
46  * @return ConfigurationStore
47  */
48  public function add($class, $propName, $pName, $pValue)
49  {
50  if (!isset($this->store[$class])) {
51  $this->store[$class] = array();
52  }
53  if (!isset($this->store[$class][$propName])) {
54  $this->store[$class][$propName] = array();
55  }
56  $this->store[$class][$propName][$pName] = $pValue;
57  return $this;
58  }
59  /**
60  * Get a configuration parameter from the store
61  *
62  * @param string $class The parameter's class
63  * @param null $propName The property's name. If $propName is null, then it will lookup all parameters matching the $pName
64  * @param null $pName The parameter's name. If $pName is null, then it will lookup all parameters matching the $propName
65  * @return array|null null on error, or array() containing the queried properties' parameters
66  */
67  public function get($class, $propName = null, $pName = null)
68  {
69  if (!isset($this->store[$class])) {
70  return null;
71  }
72  if ($propName === null) {
73  if ($pName === null) {
74  return $this->store[$class];
75  } else {
76  $res = array();
77  foreach ($this->store[$class] as $propName => $elmt) {
78  if (isset($elmt[$pName])) {
79  $res[$propName] = $elmt;
80  }
81  }
82  return $res;
83  }
84  } else {
85  if ($pName === null) {
86  if (isset($this->store[$class][$propName])) {
87  return $this->store[$class][$propName];
88  }
89  } else {
90  if (isset($this->store[$class]) && isset($this->store[$class][$propName])) {
91  return $this->store[$class][$propName][$pName];
92  }
93  }
94  }
95  return null;
96  }
97  /**
98  * Returns the JSON text serialization of the store
99  *
100  * @return bool|string boolean false on error, or JSON string on success
101  */
102  public function getText()
103  {
104  $codec = new JSONCodec();
105  try {
106  $str = $codec->encode($this->store);
107  }
108  catch(Exception $e) {
109  return false;
110  }
111  return $str;
112  }
113 }
add($class, $propName, $pName, $pValue)
$class
Definition: updateclass.php:38
← centre documentaire © anakeen