public function getGroups($fields = [], $recursive = false)
{
$dns = $this->getAttribute($this->getSchema()->memberOf());
$dns = is_array($dns) ? $dns : [];
$query = $this->getQuery()->newInstance();
$groups = $query->newCollection($dns)->map(function ($dn) use($query, $fields) {
return $query->select($fields)->findByDn($dn);
})->filter(function ($group) {
return $group instanceof Group;
});
if ($recursive) {
// If recursive results are requested, we'll ask each group
// for their groups, and merge the resulting collection.
foreach ($groups as $group) {
$groups = $groups->merge($group->getGroups($fields, $recursive));
}
}
return $groups;
}