Skip to content

Core\Ldap\Entry::bool

$entry->bool(string $name): ?bool$entry->bool(
    string $name,
): ?bool

Returns the one value of an attribute as true or false, such as isCriticalSystemObject.

A directory writes a yes-or-no value as the text TRUE or FALSE. bool reads that text and returns a bool. If the entry has no value for the attribute, the result is null. Many entries have no value for such an attribute, so decide what a missing value means, for example with ?? false.

bool reads an attribute with one value. For an attribute with several values, it throws a LogicError. It also throws a LogicError when the value is not TRUE or FALSE, and the message names the attribute.

Parameters

  • $namestringThe attribute's name, such as mail. Upper and lower case are the same.

Return value

Returns ?bool. true for TRUE, false for FALSE, or null when the entry has no value for the attribute.

Errors

  • LogicErrorThe attribute has more than one value, or its value is not TRUE or FALSE. The message names the attribute.

Examples

The examples below run with this configuration file.

nvs.toml
# These examples use the LDAP directory server on this computer at port 16636.
# `tls_ca_file` is the certificate that the server's certificate is checked against.
[ldap.corp]
url = "ldaps://127.0.0.1:16636"
base = "DC=example,DC=test"
user = "CN=Reader,OU=Company,DC=example,DC=test"
password = "Novis-reader-1"
tls_ca_file = "../../../../../tests/db/ca.crt"
timeout = "5s"
# The program may open the `corp` directory.
[capabilities.ldap]
connect = ["corp"]

Is this account critical

<?nvs
// Shows how to read a yes-or-no value of an entry.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
var $admin = $corp->read('CN=Administrator,CN=Users,DC=example,DC=test', { select: ['isCriticalSystemObject'] });
if ($admin != null) {
// The domain needs the account `Administrator`, so the value is `TRUE`.
// This prints `Do not delete this account.`
if ($admin->bool('isCriticalSystemObject') ?? false) {
echo "Do not delete this account.\n";
}
}
Output
Do not delete this account.

A value that is missing

<?nvs
// Shows that `bool` returns `null` when the entry has no value.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
array<string> $dns = [
'CN=Administrator,CN=Users,DC=example,DC=test',
'CN=Anna Berg,OU=Company,DC=example,DC=test',
];
foreach ($dns as string $dn) {
var $account = $corp->read($dn, { select: ['isCriticalSystemObject'] });
if ($account == null) {
continue;
}
// Anna Berg has no `isCriticalSystemObject`, so `bool` returns `null`.
// This prints `Administrator: true` and `Anna Berg: no value`.
?bool $critical = $account->bool('isCriticalSystemObject');
if ($critical == null) {
echo $account->dn()->rdnValue(), ": no value\n";
} else {
echo $account->dn()->rdnValue(), ': ', $critical ? 'true' : 'false', "\n";
}
}
Output
Administrator: true
Anna Berg: no value

A value that is not true or false

<?nvs
// Shows what `bool` does with a value that is not `TRUE` or `FALSE`.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
var $person = $corp->read('CN=Carla Diaz,OU=Company,DC=example,DC=test', { select: ['department'] });
if ($person != null) {
// `department` is the text `Sales`. `bool` throws a `LogicError` that names the attribute.
try {
echo ($person->bool('department') ?? false) ? 'true' : 'false', "\n";
} catch (LogicError $error) {
echo $error->message, "\n";
}
}
Output
Core\Ldap\Entry::bool: a value of `department` is not `TRUE` or `FALSE`