1: <?php
2: /**
3: * A helper class to determine an LDAP changeset.
4: *
5: * PHP version 5
6: *
7: * @category Kolab
8: * @package Kolab_Server
9: * @author Gunnar Wrobel <wrobel@pardus.de>
10: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
11: * @link http://pear.horde.org/index.php?package=Kolab_Server
12: */
13:
14: /**
15: * A helper class to determine an LDAP changeset.
16: *
17: * Copyright 2008-2012 Horde LLC (http://www.horde.org/)
18: *
19: * See the enclosed file COPYING for license information (LGPL). If you
20: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
21: *
22: * @category Kolab
23: * @package Kolab_Server
24: * @author Gunnar Wrobel <wrobel@pardus.de>
25: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
26: * @link http://pear.horde.org/index.php?package=Kolab_Server
27: */
28: class Horde_Kolab_Server_Ldap_Changes
29: {
30: /**
31: * The object to be modified.
32: *
33: * @var Horde_Kolab_Server_Object
34: */
35: private $_object;
36:
37: /**
38: * The new dataset.
39: *
40: * @var array
41: */
42: private $_data;
43:
44: /**
45: * Constructor.
46: *
47: * @param Horde_Kolab_Server_Object $object The object to be modified.
48: * @param array $data The attributes of the object
49: * to be stored.
50: */
51: public function __construct(
52: Horde_Kolab_Server_Object_Interface $object,
53: array $data
54: ) {
55: $this->_object = $object;
56: $this->_data = $data;
57: }
58:
59: /**
60: * Return an LDAP changeset from the difference between the current object
61: * data and the new dataset.
62: *
63: * @return array The LDAP changeset.
64: */
65: public function getChangeset()
66: {
67: $cs = array();
68: $old = $this->_object->readInternal();
69: $new = $this->_data;
70: $attributes = array_merge(array_keys($old), array_keys($new));
71: foreach ($attributes as $attribute) {
72: if (!isset($old[$attribute])) {
73: $cs['add'][$attribute] = $new[$attribute];
74: continue;
75: }
76: if (!isset($new[$attribute])) {
77: $cs['delete'][] = $attribute;
78: continue;
79: }
80: if (count($new[$attribute]) == 1
81: && count($old[$attribute]) == 1
82: ) {
83: if ($new[$attribute][0] == $old[$attribute][0]) {
84: continue;
85: } else {
86: $cs['replace'][$attribute] = $new[$attribute][0];
87: continue;
88: }
89: }
90: $adds = array_diff($new[$attribute], $old[$attribute]);
91: if (!empty($adds)) {
92: $cs['add'][$attribute] = array_values($adds);
93: }
94: $deletes = array_diff($old[$attribute], $new[$attribute]);
95: if (!empty($deletes)) {
96: $cs['delete'][$attribute] = array_values($deletes);
97: }
98: }
99: return $cs;
100: }
101: }