Core\Ldap\Entry::has
Checks whether an entry has a value for an attribute. It returns true or false.
A directory does not send an attribute that has no value. For example, a person without a job title has no title attribute at all. has("title") returns false for that person, so you can test for an empty field before you read it.
Upper and lower case are the same in an attribute name, so has("mail") and has("Mail") return the same result.
Good to know: has only sees the attributes the search returned. If the search's select list does not name an attribute, has returns false for it, even when the entry has a value in the directory.
Parameters
$namestringThe attribute's name, such asmail. Upper and lower case are the same.
Return value
Returns bool. true when the entry has the attribute, and false when it does not or the search did not select it.
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"]Who has a job title
<?nvs// Shows how to check which people have a job title in the directory.// requires: samba-ad
var $corp = Core\Ldap::connect('corp');var $people = $corp->search(Core\Ldap\Filter::present('department'), { base: 'OU=Company,DC=example,DC=test', select: ['cn', 'title'] });
// A search returns entries in no fixed order, so the lines are sorted.array<tainted string> $lines = [];foreach ($people as Core\Ldap\Entry $person) { string $answer = $person->has('title') ? 'has a job title' : 'has no job title'; $lines[] = $person->dn()->rdnValue() . ': ' . $answer;}
// Only Anna Berg has a job title.foreach (Core\Arr::sort($lines) as tainted string $line) { echo $line, "\n";}Anna Berg: has a job titleBen Kohl: has no job titleCarla Diaz: has no job titleDora Lind: has no job titleUpper and lower case are the same
<?nvs// Shows that `has` finds an attribute however its name is written.// requires: samba-ad
var $corp = Core\Ldap::connect('corp');var $person = $corp->read('CN=Ben Kohl,OU=Company,DC=example,DC=test', { select: ['mail'] });
if ($person != null) { // These three lines print `true`. echo $person->has('mail') ? 'true' : 'false', "\n"; echo $person->has('Mail') ? 'true' : 'false', "\n"; echo $person->has('MAIL') ? 'true' : 'false', "\n";
// Ben Kohl has no phone number, so this prints `false`. echo $person->has('telephoneNumber') ? 'true' : 'false', "\n";}truetruetruefalseSelect the attributes you check
<?nvs// Shows that `has` only sees the attributes the search selected.// requires: samba-ad
var $corp = Core\Ldap::connect('corp');string $dn = 'CN=Carla Diaz,OU=Company,DC=example,DC=test';
// The first read selects only `mail`. This prints `false`,// although Carla Diaz has a department in the directory.var $short = $corp->read($dn, { select: ['mail'] });if ($short != null) { echo $short->has('department') ? 'true' : 'false', "\n";}
// The second read also selects `department`. This prints `true`.var $full = $corp->read($dn, { select: ['mail', 'department'] });if ($full != null) { echo $full->has('department') ? 'true' : 'false', "\n";}falsetrue