3 namespace Adldap\Configuration
;
5 use Adldap\Connections\ConnectionInterface
;
6 use Adldap\Configuration\Validators\ArrayValidator
;
7 use Adldap\Configuration\Validators\StringOrNullValidator
;
8 use Adldap\Configuration\Validators\BooleanValidator
;
9 use Adldap\Configuration\Validators\IntegerValidator
;
12 * Class DomainConfiguration
14 * Contains an array of configuration options for a single LDAP connection.
16 * @package Adldap\Configuration
18 class DomainConfiguration
21 * The configuration options array.
23 * The default values for each key indicate the type of value it requires.
27 protected $options = [
28 // An array of LDAP hosts.
29 'domain_controllers' => [],
31 // The global LDAP operation timeout limit in seconds.
34 // The LDAP version to utilize.
37 // The port to use for connecting to your hosts.
38 'port' => ConnectionInterface
::PORT
,
40 // The base distinguished name of your domain.
43 // Whether or not to use SSL when connecting to your hosts.
46 // Whether or not to use TLS when connecting to your hosts.
49 // Whether or not follow referrals is enabled when performing LDAP operations.
50 'follow_referrals' => false,
52 // The account prefix to use when authenticating users.
53 'account_prefix' => null,
55 // The account suffix to use when authenticating users.
56 'account_suffix' => null,
58 // The username to connect to your hosts with.
59 'admin_username' => '',
61 // The password that is utilized with the above user.
62 'admin_password' => '',
64 // The account prefix to use when authenticating your admin account above.
65 'admin_account_prefix' => null,
67 // The account prefix to use when authenticating your admin account above.
68 'admin_account_suffix' => null,
70 // Custom LDAP options that you'd like to utilize.
71 'custom_options' => [],
77 * @param array $options
79 * @throws ConfigurationException When an option value given is an invalid type.
81 public function __construct(array $options = [])
83 foreach ($options as $key => $value) {
84 $this->set($key, $value);
89 * Sets a configuration option.
91 * Throws an exception if the specified option does
92 * not exist, or if it's an invalid type.
97 * @throws ConfigurationException When an option value given is an invalid type.
99 public function set($key, $value)
101 if($this->validate($key, $value)) {
102 $this->options
[$key] = $value;
107 * Returns the value for the specified configuration options.
109 * Throws an exception if the specified option does not exist.
115 * @throws ConfigurationException When the option specified does not exist.
117 public function get($key)
119 if ($this->has($key)) {
120 return $this->options
[$key];
123 throw new ConfigurationException("Option {$key} does not exist.");
127 * Checks if a configuration option exists.
133 public function has($key)
135 return array_key_exists($key, $this->options
);
139 * Validates the new configuration option against its
140 * default value to ensure it's the correct type.
142 * If an invalid type is given, an exception is thrown.
145 * @param mixed $value
149 * @throws ConfigurationException When an option value given is an invalid type.
151 protected function validate($key, $value)
153 $default = $this->get($key);
155 if (is_array($default)) {
156 $validator = new ArrayValidator($key, $value);
157 } elseif (is_int($default)) {
158 $validator = new IntegerValidator($key, $value);
159 } elseif (is_bool($default)) {
160 $validator = new BooleanValidator($key, $value);
162 $validator = new StringOrNullValidator($key, $value);
165 return $validator->validate();