1: <?php
2: /**
3: * This class provides the storage for a preference scope.
4: *
5: * Copyright 2010-2012 Horde LLC (http://www.horde.org/)
6: *
7: * See the enclosed file COPYING for license information (LGPL). If you
8: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
9: *
10: * @author Michael Slusarz <slusarz@horde.org>
11: * @category Horde
12: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
13: * @package Prefs
14: */
15: class Horde_Prefs_Scope implements Iterator, Serializable
16: {
17: /**
18: * Is the object being initialized?
19: *
20: * @var boolean
21: */
22: public $init = false;
23:
24: /**
25: * The scope name.
26: *
27: * @var string
28: */
29: public $scope;
30:
31: /**
32: * List of dirty prefs.
33: *
34: * @var array
35: */
36: protected $_dirty = array();
37:
38: /**
39: * Preferences list. Each preference has the following format:
40: * <pre>
41: * [pref_name] => array(
42: * [d] => (string) Default value
43: * If not present, 'v' is the default value.
44: * [l] => (boolean) Locked
45: * If not present, pref is not locked.
46: * [v] => (string) Current pref value
47: * )
48: * </pre>
49: *
50: * @var array
51: */
52: protected $_prefs = array();
53:
54: /**
55: * Constructor.
56: *
57: * @param string $scope The scope for this set of preferences.
58: */
59: public function __construct($scope)
60: {
61: $this->scope = $scope;
62: }
63:
64: /**
65: * Removes a preference entry.
66: *
67: * @param string $pref The name of the preference to remove.
68: *
69: * @return boolean True if preference was removed.
70: */
71: public function remove($pref)
72: {
73: if (!isset($this->_prefs[$pref])) {
74: return false;
75: }
76:
77: unset($this->_prefs[$pref]);
78: if (!$this->init) {
79: $this->setDirty($pref, true);
80: }
81:
82: return true;
83: }
84:
85: /**
86: * Sets the value for a preference.
87: *
88: * @param string $pref The preference name.
89: * @param string $val The preference value.
90: */
91: public function set($pref, $val)
92: {
93: if (isset($this->_prefs[$pref])) {
94: $ptr = &$this->_prefs[$pref];
95:
96: if ($val != $ptr['v']) {
97: if (isset($ptr['d']) && ($val == $ptr['d'])) {
98: unset($ptr['d']);
99: } else {
100: $ptr['d'] = $ptr['v'];
101: }
102: $ptr['v'] = $val;
103:
104: if (!$this->init) {
105: $this->setDirty($pref, true);
106: }
107: }
108: } else {
109: $this->_prefs[$pref] = array(
110: 'v' => $val
111: );
112:
113: if (!$this->init) {
114: $this->setDirty($pref, true);
115: }
116: }
117: }
118:
119: /**
120: * Does a preference exist in this scope?
121: *
122: * @return boolean True if the preference exists.
123: */
124: public function exists($pref)
125: {
126: return isset($this->_prefs[$pref]);
127: }
128:
129: /**
130: * Returns the value of a preference.
131: *
132: * @param string $pref The preference name to retrieve.
133: *
134: * @return string The value of the preference, null if it doesn't exist.
135: */
136: public function get($pref)
137: {
138: return isset($this->_prefs[$pref]['v'])
139: ? $this->_prefs[$pref]['v']
140: : null;
141: }
142:
143: /**
144: * Mark a preference as locked.
145: *
146: * @param string $pref The preference name.
147: * @param boolean $locked Is the preference locked?
148: */
149: public function setLocked($pref, $locked)
150: {
151: if (isset($this->_prefs[$pref])) {
152: $ptr = &$this->_prefs[$pref];
153:
154: if ($locked) {
155: if (!isset($ptr['l'])) {
156: $ptr['l'] = true;
157: if (!$this->init) {
158: $this->setDirty($pref, true);
159: }
160: }
161: } elseif (isset($ptr['l'])) {
162: unset($ptr['l']);
163: if (!$this->init) {
164: $this->setDirty($pref, true);
165: }
166: }
167: }
168: }
169:
170: /**
171: * Is a preference locked?
172: *
173: * @param string $pref The preference name.
174: *
175: * @return boolean Whether the preference is locked.
176: */
177: public function isLocked($pref)
178: {
179: return !empty($this->_prefs[$pref]['l']);
180: }
181:
182: /**
183: * Is a preference's value the default?
184: *
185: * @param string $pref The preference name.
186: *
187: * @return boolean True if the preference contains the default value.
188: */
189: public function isDefault($pref)
190: {
191: return !isset($this->_prefs[$pref]['d']);
192: }
193:
194: /**
195: * Returns the default value of a preference.
196: *
197: * @param string $pref The preference name.
198: *
199: * @return string The preference's default value.
200: */
201: public function getDefault($pref)
202: {
203: if (!$this->isDefault($pref)) {
204: return $this->_prefs[$pref]['d'];
205: }
206:
207: return isset($this->_prefs[$pref])
208: ? $this->_prefs[$pref]['v']
209: : null;
210: }
211:
212: /**
213: * Get the list of dirty preferences.
214: *
215: * @return array The list of dirty preferences.
216: */
217: public function getDirty()
218: {
219: return array_keys($this->_dirty);
220: }
221:
222: /**
223: * Is a preference marked dirty?
224: *
225: * @param mixed $pref The preference name. If null, will return true if
226: * scope contains at least one dirty pref.
227: *
228: * @return boolean True if the preference is marked dirty.
229: */
230: public function isDirty($pref = null)
231: {
232: return is_null($pref)
233: ? !empty($this->_dirty)
234: : isset($this->_dirty[$pref]);
235: }
236:
237: /**
238: * Set the dirty flag for a preference
239: *
240: * @param string $pref The preference name.
241: * @param boolean $dirty True to mark the pref as dirty.
242: */
243: public function setDirty($pref, $dirty)
244: {
245: if ($dirty) {
246: $this->_dirty[$pref] = true;
247: } else {
248: unset($this->_dirty[$pref]);
249: }
250: }
251:
252: /* Iterator methods. */
253:
254: public function current()
255: {
256: return current($this->_prefs);
257: }
258:
259: public function key()
260: {
261: return key($this->_prefs);
262: }
263:
264: public function next()
265: {
266: next($this->_prefs);
267: }
268:
269: public function rewind()
270: {
271: reset($this->_prefs);
272: }
273:
274: public function valid()
275: {
276: return (key($this->_prefs) !== null);
277: }
278:
279:
280: /* Serializable methods. */
281:
282: /**
283: */
284: public function serialize()
285: {
286: return json_encode(array(
287: $this->scope,
288: $this->_prefs
289: ));
290: }
291:
292: /**
293: */
294: public function unserialize($data)
295: {
296: list($this->scope, $this->_prefs) = json_decode($data, true);
297: }
298:
299: }
300: