composer package updates
[openemr.git] / vendor / adldap2 / adldap2 / src / Adldap.php
blob5f011ac7e5dab02b722e6c6071fb5c572cdf525c
1 <?php
3 namespace Adldap;
5 use InvalidArgumentException;
6 use Adldap\Connections\Provider;
7 use Adldap\Schemas\SchemaInterface;
8 use Adldap\Connections\ProviderInterface;
9 use Adldap\Connections\ConnectionInterface;
10 use Adldap\Configuration\DomainConfiguration;
12 class Adldap implements AdldapInterface
14 /**
15 * The default provider name.
17 * @var string
19 protected $default = 'default';
21 /**
22 * The connection providers.
24 * @var array
26 protected $providers = [];
28 /**
29 * {@inheritdoc}
31 public function __construct(array $providers = [])
33 foreach ($providers as $name => $config) {
34 $this->addProvider($config, $name);
37 if ($default = key($providers)) {
38 $this->setDefaultProvider($default);
42 /**
43 * {@inheritdoc}
45 public function addProvider($config = [], $name = 'default', ConnectionInterface $connection = null, SchemaInterface $schema = null)
47 if ($this->isValidConfig($config)) {
48 $config = new Provider($config, $connection, $schema);
51 if ($config instanceof ProviderInterface) {
52 $this->providers[$name] = $config;
54 return $this;
57 throw new InvalidArgumentException(
58 "You must provide a configuration array or an instance of Adldap\Connections\ProviderInterface."
62 /**
63 * Determines if the given config is valid.
65 * @param mixed $config
67 * @return bool
69 protected function isValidConfig($config)
71 return is_array($config) || $config instanceof DomainConfiguration;
74 /**
75 * {@inheritdoc}
77 public function getProviders()
79 return $this->providers;
82 /**
83 * {@inheritdoc}
85 public function getProvider($name)
87 if (array_key_exists($name, $this->providers)) {
88 return $this->providers[$name];
91 throw new AdldapException("The connection provider '$name' does not exist.");
94 /**
95 * {@inheritdoc}
97 public function setDefaultProvider($name = 'default')
99 if ($this->getProvider($name) instanceof ProviderInterface) {
100 $this->default = $name;
105 * {@inheritdoc}
107 public function getDefaultProvider()
109 return $this->getProvider($this->default);
113 * {@inheritdoc}
115 public function removeProvider($name)
117 unset($this->providers[$name]);
119 return $this;
123 * {@inheritdoc}
125 public function connect($name = null, $username = null, $password = null)
127 $provider = $name ? $this->getProvider($name) : $this->getDefaultProvider();
129 return $provider->connect($username, $password);
133 * {@inheritdoc}
135 public function __call($method, $parameters)
137 $provider = $this->getDefaultProvider();
139 if (!$provider->getConnection()->isBound()) {
140 // We'll make sure we have a bound connection before
141 // allowing dynamic calls on the default provider.
142 $provider->connect();
145 return call_user_func_array([$provider, $method], $parameters);