Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Commit a3c51d0

Browse files
committed
Merge branch 'develop'
2 parents fb7c9b7 + d742925 commit a3c51d0

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
All notable changes to this project will be documented in this file, in reverse chronological order by release.
44

5-
## 2.7.2 - TBD
5+
## 2.8.0 - TBD
66

77
### Added
88

9+
- [#53](https://github.com/zendframework/zend-ldap/pull/53) Adds addAttribute-method
10+
to Ldap-class
911
- [#57](https://github.com/zendframework/zend-ldap/pull/57) adds support for new
1012
coding-standards.
1113

src/Ldap.php

+36
Original file line numberDiff line numberDiff line change
@@ -1314,6 +1314,42 @@ public function delete($dn, $recursively = false)
13141314
return $this;
13151315
}
13161316

1317+
/**
1318+
* Add one or more attributes to the specified dn
1319+
*
1320+
* @param string|Dn $dn
1321+
* @param array $attributes
1322+
* @param bool $allowEmptyAttributes
1323+
* @return Ldap Provides a fluid interface
1324+
* @throws LdapException
1325+
*/
1326+
public function addAttributes($dn, array $attributes, $allowEmptyAttributes = false)
1327+
{
1328+
// Safety-flap: Check whether there are empty arrays that would cause
1329+
// complete removal of entries without the emptyAll flag.
1330+
if ($allowEmptyAttributes !== true) {
1331+
foreach ($attributes as $key => $value) {
1332+
if (empty($value)) {
1333+
unset($attributes[$key]);
1334+
}
1335+
}
1336+
}
1337+
1338+
if ($dn instanceof Dn) {
1339+
$dn = $dn->toString();
1340+
}
1341+
1342+
ErrorHandler::start(E_WARNING);
1343+
$entryAdded = ldap_mod_add($this->resource, $dn, $attributes);
1344+
ErrorHandler::stop();
1345+
1346+
if ($entryAdded === false) {
1347+
throw new Exception\LdapException($this, 'adding attribute: ' . $dn);
1348+
}
1349+
1350+
return $this;
1351+
}
1352+
13171353
/**
13181354
* Delete single attributes from a LDAP-Node
13191355
*

test/OfflineTest.php

+36
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,40 @@ public function testRemovingAttributesFails()
183183
$ldap = new \Zend\Ldap\Ldap();
184184
$ldap->deleteAttributes('foo', ['bar']);
185185
}
186+
187+
/**
188+
* @dataProvider removingAttributesProvider
189+
*/
190+
public function testAddingAttributes(
191+
$dn,
192+
$attributes,
193+
$allowEmptyAttributes,
194+
$expectedDn,
195+
$expectedAttributesToRemove
196+
) {
197+
$ldap_mod_add = $this->getFunctionMock('Zend\\Ldap', "ldap_mod_add");
198+
$ldap_mod_add->expects($this->once())
199+
->with(
200+
$this->isNull(),
201+
$this->equalTo($expectedDn),
202+
$this->equalTo($expectedAttributesToRemove)
203+
)
204+
->willReturn(true);
205+
206+
$ldap = new \Zend\Ldap\Ldap();
207+
$this->assertSame($ldap, $ldap->addAttributes($dn, $attributes, $allowEmptyAttributes));
208+
}
209+
210+
/**
211+
* @expectedException \Zend\Ldap\Exception\LdapException
212+
*/
213+
public function testAddingAttributesFails()
214+
{
215+
$ldap_mod_del = $this->getFunctionMock('Zend\\Ldap', 'ldap_mod_add');
216+
$ldap_mod_del->expects($this->once())
217+
->willReturn(false);
218+
219+
$ldap = new \Zend\Ldap\Ldap();
220+
$ldap->addAttributes('foo', ['bar']);
221+
}
186222
}

0 commit comments

Comments
 (0)