Core\Ldap\Entry::int
Returns the one value of an attribute as a whole number, such as a person's logonCount or the domain's minPwdLength.
If the entry has no value for the attribute, the result is null. A directory does not send an attribute that has no value, so ?? is a simple way to give a default.
int 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 a whole number, for example a name, or a number too large for an int. The message names the attribute.
Good to know: some numbers in Active Directory are sets of flags, such as userAccountControl. int returns the raw number. accountFlags and groupType return the same number with one bool for each flag.
Parameters
$namestringThe attribute's name, such asmail. Upper and lower case are the same.
Return value
Returns ?int. The number, or null when the entry has no value for the attribute.
Errors
LogicErrorThe attribute has more than one value, or its value is not a whole number, or the number is too large for anint. The message names the attribute.
Examples
The examples below run with this configuration file.
# 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"]Read the password rules
<?nvs// Shows how to read the password rules of a domain as whole numbers.// requires: samba-ad
var $corp = Core\Ldap::connect('corp');var $domain = $corp->read('DC=example,DC=test', { select: ['minPwdLength', 'pwdHistoryLength'] });
if ($domain != null) { // This prints `A password has at least 7 characters.` int $length = $domain->int('minPwdLength') ?? 0; echo 'A password has at least ', $length, " characters.\n";
// This prints `The last 24 passwords cannot be used again.` echo 'The last ', $domain->int('pwdHistoryLength') ?? 0, " passwords cannot be used again.\n";}A password has at least 7 characters.The last 24 passwords cannot be used again.A number that is missing
<?nvs// Shows that `int` returns `null` when the entry has no value, and how to give a default.// requires: samba-ad
var $corp = Core\Ldap::connect('corp');var $person = $corp->read('CN=Ben Kohl,OU=Company,DC=example,DC=test', { select: ['badPwdCount', 'lockoutTime'] });
if ($person != null) { // Ben Kohl never typed a wrong password. This prints `0`. echo $person->int('badPwdCount') ?? 0, "\n";
// Ben Kohl was never locked out, so the entry has no `lockoutTime`. // `int` returns `null`. This prints `never locked out`. ?int $lockedAt = $person->int('lockoutTime'); echo $lockedAt == null ? 'never locked out' : 'locked out', "\n";}0never locked outA value that is not a number
<?nvs// Shows what `int` does with a value that is not a whole number.// requires: samba-ad
var $corp = Core\Ldap::connect('corp');var $person = $corp->read('CN=Anna Berg,OU=Company,DC=example,DC=test', { select: ['primaryGroupID', 'department'] });
if ($person != null) { // `primaryGroupID` is a number. 513 is the group `Domain Users`. This prints `513`. echo $person->int('primaryGroupID') ?? 0, "\n";
// `department` is the text `Sales`. `int` throws a `LogicError` that names the attribute. try { echo $person->int('department') ?? 0, "\n"; } catch (LogicError $error) { echo $error->message, "\n"; }}513Core\Ldap\Entry::int: a value of `department` is not a whole number that fits in an `int`