[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/symfony/ldap/Adapter/ExtLdap/ -> Collection.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <[email protected]>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Symfony\Component\Ldap\Adapter\ExtLdap;
  13  
  14  use Symfony\Component\Ldap\Adapter\CollectionInterface;
  15  use Symfony\Component\Ldap\Entry;
  16  use Symfony\Component\Ldap\Exception\LdapException;
  17  
  18  /**
  19   * @author Charles Sarrazin <[email protected]>
  20   */
  21  class Collection implements CollectionInterface
  22  {
  23      private $connection;
  24      private $search;
  25      /** @var list<Entry>|null */
  26      private $entries;
  27  
  28      public function __construct(Connection $connection, Query $search)
  29      {
  30          $this->connection = $connection;
  31          $this->search = $search;
  32      }
  33  
  34      /**
  35       * {@inheritdoc}
  36       */
  37      public function toArray()
  38      {
  39          if (null === $this->entries) {
  40              $this->entries = iterator_to_array($this->getIterator(), false);
  41          }
  42  
  43          return $this->entries;
  44      }
  45  
  46      /**
  47       * @return int
  48       */
  49      #[\ReturnTypeWillChange]
  50      public function count()
  51      {
  52          $con = $this->connection->getResource();
  53          $searches = $this->search->getResources();
  54          $count = 0;
  55          foreach ($searches as $search) {
  56              $searchCount = ldap_count_entries($con, $search);
  57              if (false === $searchCount) {
  58                  throw new LdapException('Error while retrieving entry count: '.ldap_error($con));
  59              }
  60              $count += $searchCount;
  61          }
  62  
  63          return $count;
  64      }
  65  
  66      /**
  67       * @return \Traversable<int, Entry>
  68       */
  69      #[\ReturnTypeWillChange]
  70      public function getIterator()
  71      {
  72          if (0 === $this->count()) {
  73              return;
  74          }
  75  
  76          $con = $this->connection->getResource();
  77          $searches = $this->search->getResources();
  78          foreach ($searches as $search) {
  79              $current = ldap_first_entry($con, $search);
  80  
  81              if (false === $current) {
  82                  throw new LdapException('Could not rewind entries array: '.ldap_error($con));
  83              }
  84  
  85              yield $this->getSingleEntry($con, $current);
  86  
  87              while (false !== $current = ldap_next_entry($con, $current)) {
  88                  yield $this->getSingleEntry($con, $current);
  89              }
  90          }
  91      }
  92  
  93      /**
  94       * @return bool
  95       */
  96      #[\ReturnTypeWillChange]
  97      public function offsetExists($offset)
  98      {
  99          $this->toArray();
 100  
 101          return isset($this->entries[$offset]);
 102      }
 103  
 104      /**
 105       * @return Entry|null
 106       */
 107      #[\ReturnTypeWillChange]
 108      public function offsetGet($offset)
 109      {
 110          $this->toArray();
 111  
 112          return $this->entries[$offset] ?? null;
 113      }
 114  
 115      /**
 116       * @return void
 117       */
 118      #[\ReturnTypeWillChange]
 119      public function offsetSet($offset, $value)
 120      {
 121          $this->toArray();
 122  
 123          $this->entries[$offset] = $value;
 124      }
 125  
 126      /**
 127       * @return void
 128       */
 129      #[\ReturnTypeWillChange]
 130      public function offsetUnset($offset)
 131      {
 132          $this->toArray();
 133  
 134          unset($this->entries[$offset]);
 135      }
 136  
 137      private function getSingleEntry($con, $current): Entry
 138      {
 139          $attributes = ldap_get_attributes($con, $current);
 140  
 141          if (false === $attributes) {
 142              throw new LdapException('Could not fetch attributes: '.ldap_error($con));
 143          }
 144  
 145          $attributes = $this->cleanupAttributes($attributes);
 146  
 147          $dn = ldap_get_dn($con, $current);
 148  
 149          if (false === $dn) {
 150              throw new LdapException('Could not fetch DN: '.ldap_error($con));
 151          }
 152  
 153          return new Entry($dn, $attributes);
 154      }
 155  
 156      private function cleanupAttributes(array $entry): array
 157      {
 158          $attributes = array_diff_key($entry, array_flip(range(0, $entry['count'] - 1)) + [
 159                  'count' => null,
 160                  'dn' => null,
 161              ]);
 162          array_walk($attributes, function (&$value) {
 163              unset($value['count']);
 164          });
 165  
 166          return $attributes;
 167      }
 168  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer