Skip to content

Core\Ldap\Change::replace

Returns a Core\Ldap\Change that replaces every value of one attribute of a directory entry. You give the attribute's name and the new value or list of values.

The change does nothing on its own. You put it in a list and pass the list to a connection's modify method. modify sends every change in the list in one request, so the server makes all of them or none of them.

Good to know: replace works whether the attribute has values or not. With null or an empty list, the attribute is removed. A value is written in the form that Core\Ldap\Entry returns it, such as an int, a bool, a Core\Uuid or an Instant. A name that is not an attribute name throws a LogicError at once.

Parameters

  • $attributestringThe attribute's name, such as description. It cannot be tainted.
  • $valuemixedOne value, or a list of values. A value is written in the form the methods of Entry return it, such as an int, a Uuid or an Instant.

Return value

Returns Core\Ldap\Change. The change.

Errors

  • LogicErrorattribute is not an attribute name.

Examples

Set a description

<?nvs
// Shows how to prepare a change that sets a user's description.
var $text = Core\Ldap\Change::replace('description', 'Works in the Shop team');
// The old description is gone after `modify` sends this list. This prints 1.
array<Core\Ldap\Change> $changes = [$text];
echo Core\Arr::count($changes), "\n";
Output
1

Values that are not text

<?nvs
// Shows changes with values that are not text.
// An `int` is sent as its digits, and a `bool` as TRUE or FALSE.
array<Core\Ldap\Change> $changes = [
Core\Ldap\Change::replace('employeeNumber', 1042),
Core\Ldap\Change::replace('msExchHideFromAddressLists', true),
];
// This prints 2.
echo Core\Arr::count($changes), "\n";
Output
2

Remove with null

<?nvs
// Shows how `replace` removes an attribute.
// With `null` or an empty list, `replace` removes the attribute.
// It also works when the user has no value there.
array<string> $none = [];
array<Core\Ldap\Change> $changes = [
Core\Ldap\Change::replace('pager', null),
Core\Ldap\Change::replace('mobile', $none),
];
// This prints 2.
echo Core\Arr::count($changes), "\n";
Output
2