1: <?php
2: /**
3: * A folder stamp that includes a list of UIDs.
4: *
5: * PHP version 5
6: *
7: * @category Kolab
8: * @package Kolab_Storage
9: * @author Thomas Jarosch <thomas.jarosch@intra2net.com>
10: * @author Gunnar Wrobel <wrobel@pardus.de>
11: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
12: * @link http://pear.horde.org/index.php?package=Kolab_Storage
13: */
14:
15: /**
16: * A folder stamp that includes a list of UIDs.
17: *
18: * Copyright 2011-2012 Horde LLC (http://www.horde.org/)
19: *
20: * See the enclosed file COPYING for license information (LGPL). If you
21: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
22: *
23: * @category Kolab
24: * @package Kolab_Storage
25: * @author Thomas Jarosch <thomas.jarosch@intra2net.com>
26: * @author Gunnar Wrobel <wrobel@pardus.de>
27: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
28: * @link http://pear.horde.org/index.php?package=Kolab_Storage
29: */
30: class Horde_Kolab_Storage_Folder_Stamp_Uids
31: implements Horde_Kolab_Storage_Folder_Stamp
32: {
33: /** The UID validity status */
34: const UIDVALIDITY = 'uidvalidity';
35:
36: /** The next UID status */
37: const UIDNEXT = 'uidnext';
38:
39: /**
40: * The folder status.
41: *
42: * @var array
43: */
44: private $_status;
45:
46: /**
47: * The list of backend object IDs.
48: *
49: * @var array
50: */
51: private $_ids;
52:
53: /**
54: * Constructor.
55: *
56: * @param array $status The folder status.
57: * @param array $ids The list of undeleted objects in the folder.
58: */
59: public function __construct($status, $ids)
60: {
61: $this->_status = $status;
62: $this->_ids = $ids;
63: }
64:
65: /**
66: * Return the folder UID validity.
67: *
68: * @return string The folder UID validity marker.
69: */
70: public function uidvalidity()
71: {
72: return $this->_status[self::UIDVALIDITY];
73: }
74:
75: /**
76: * Return the folder next UID number.
77: *
78: * @return string The next UID number.
79: */
80: public function uidnext()
81: {
82: return $this->_status[self::UIDNEXT];
83: }
84:
85: /**
86: * Return the backend object IDs in the folder.
87: *
88: * @return array The list of backend IDs.
89: */
90: public function ids()
91: {
92: return $this->_ids;
93: }
94:
95: /**
96: * Indicate if there was a complete folder reset.
97: *
98: * @param Horde_Kolab_Storage_Folder_Stamp_Uids The stamp to compare against.
99: *
100: * @return boolean True if there was a complete folder reset stamps are
101: * different, false if not.
102: */
103: public function isReset(Horde_Kolab_Storage_Folder_Stamp $stamp)
104: {
105: if (!$stamp instanceOf Horde_Kolab_Storage_Folder_Stamp_Uids) {
106: throw new Horde_Kolab_Storage_Exception('This stamp can only be compared against stamps of its own type.');
107: }
108: if ($this->uidvalidity() != $stamp->uidvalidity()) {
109: return true;
110: } else {
111: return false;
112: }
113: }
114: /**
115: * What changed between this old stamp and the new provided stamp?
116: *
117: * @param Horde_Kolab_Storage_Folder_Stamp_Uids The new stamp to compare against.
118: *
119: * @return array|boolean False if there was no change, an array of two
120: * elements (added IDs, deleted IDs) otherwise.
121: */
122: public function getChanges(Horde_Kolab_Storage_Folder_Stamp $stamp)
123: {
124: if (!$stamp instanceOf Horde_Kolab_Storage_Folder_Stamp_Uids) {
125: throw new Horde_Kolab_Storage_Exception('This stamp can only be compared against stamps of its own type.');
126: }
127: if ($this->uidnext() != $stamp->uidnext()
128: || count($this->ids()) != count($stamp->ids())) {
129: return array(
130: self::DELETED => array_values(
131: array_diff($this->ids(), $stamp->ids())
132: ),
133: self::ADDED => array_values(
134: array_diff($stamp->ids(), $this->ids())
135: )
136: );
137: }
138: return false;
139: }
140:
141: /**
142: * Serialize this object.
143: *
144: * @return string The serialized data.
145: */
146: public function serialize()
147: {
148: return serialize(array($this->_status, $this->_ids));
149: }
150:
151: /**
152: * Reconstruct the object from serialized data.
153: *
154: * @param string $data The serialized data.
155: */
156: public function unserialize($data)
157: {
158: list($this->_status, $this->_ids) = @unserialize($data);
159: }
160:
161: /**
162: * Convert the instance into a string.
163: *
164: * @return string The string representation for this instance.
165: */
166: public function __toString()
167: {
168: return sprintf(
169: "uidvalidity: %s\nuidnext: %s\nuids: %s",
170: $this->uidvalidity(),
171: $this->uidnext(),
172: join(', ', $this->ids())
173: );
174: }
175: }
176: