Skip to content
70 changes: 29 additions & 41 deletions src/PHPOpenLDAPer/LDAPEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function __construct(Connection $conn, string $dn)
$this->conn = $conn;
$this->dn = $dn;
$this->pullObject();
$this->mods = null;
}

/**
Expand All @@ -52,7 +53,7 @@ private function pullObject(): bool
}
LDAPConn::stripCount($entries);
if (count($entries) > 1) {
throw new \Exception("FATAL: Call to ldapObject with non-unique DN.");
throw new RuntimeException("FATAL: Call to ldapObject with non-unique DN.");
} else {
$this->object = $entries[0];
return true;
Expand Down Expand Up @@ -114,7 +115,7 @@ private function getLdapErrorInfo(): array
* @return void
* @throws RuntimeException if ldap_add / ldap_mod_replace fails
*/
public function write(): void
private function write(): void
{
if ($this->mods == null) {
return;
Expand Down Expand Up @@ -315,11 +316,8 @@ public function numChildren(bool $recursive = false): int
public function setAttribute(string $attr, mixed $value): void
{
$attr = strtolower($attr);
if (is_array($value)) {
$this->mods[$attr] = $value;
} else {
$this->mods[$attr] = array($value);
}
$this->mods = [$attr => $this->convertToArray($value)];
$this->write();
}

/**
Expand All @@ -331,21 +329,9 @@ public function setAttribute(string $attr, mixed $value): void
public function appendAttribute(string $attr, mixed $value): void
{
$attr = strtolower($attr);
$objArr = array();
if (isset($this->object[$attr])) {
$objArr = $this->object[$attr];
}

$modArr = array();
if (isset($this->mods[$attr])) {
$modArr = $this->mods[$attr];
}

if (is_array($value)) {
$this->mods[$attr] = array_merge($objArr, $modArr, $value);
} else {
$this->mods[$attr] = array_merge($objArr, $modArr, (array) $value);
}
$old_value = $this->getAttribute($attr);
$this->mods = [$attr => array_merge($old_value, $this->convertToArray($value))];
$this->write();
}

/**
Expand All @@ -356,7 +342,9 @@ public function appendAttribute(string $attr, mixed $value): void
public function setAttributes(array $arr): void
{
$arr = array_change_key_case($arr, CASE_LOWER);
$this->mods = $arr;
$arr = array_map([$this, "convertToArray"], $arr);
$this->mods = array_merge($this->mods, $arr);
$this->write();
}

/**
Expand All @@ -380,7 +368,8 @@ public function appendAttributes(array $arr): void
public function removeAttribute(string $attr, $item = null): void
{
$attr = strtolower($attr);
$this->mods[$attr] = array();
$this->mods = [$attr => []];
$this->write();
}

/**
Expand All @@ -392,13 +381,23 @@ public function removeAttribute(string $attr, $item = null): void
public function removeAttributeEntryByValue(string $attr, mixed $value): void
{
$attr = strtolower($attr);
$arr = $this->object[$attr];
$arr = $this->getAttribute($attr);
for ($i = 0; $i < count($arr); $i++) {
if ($arr[$i] == $value) {
unset($arr[$i]);
}
}
$this->mods[$attr] = array_values($arr);
$this->mods = [$attr => array_values($arr)];
$this->write();
}

private function convertToArray(mixed $x)
{
if (is_array($x)) {
return $x;
} else {
return [$x];
}
}

/**
Expand All @@ -413,11 +412,10 @@ public function getAttribute(string $attr): mixed
if (!$this->exists()) {
throw new RuntimeException("cannot get attribute from nonexistent entry");
}
if (isset($this->object[$attr])) {
return is_array($this->object[$attr]) ? $this->object[$attr] : [$this->object[$attr]];
} else {
return [];
if (array_key_exists($attr, $this->object)) {
return $this->convertToArray($this->object[$attr]);
}
return [];
}

/**
Expand All @@ -436,7 +434,7 @@ public function getAttributes(): array
continue;
}
$key = strtolower($key);
$output[$key] = is_array($val) ? $val : [$val];
$output[$key] = $this->convertToArray($val);
}
return $output;
}
Expand Down Expand Up @@ -469,14 +467,4 @@ public function attributeValueExists(string $attr, mixed $value): bool
$attr = strtolower($attr);
return in_array($value, $this->getAttribute($attr));
}

/**
* Check if there are pending changes
*
* @return bool true is there are pending changes, false otherwise
*/
public function pendingChanges(): bool
{
return !is_null($this->mods);
}
}