Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | namespace LdapRecord\Configuration; |
| 4 | |
| 5 | use LdapRecord\LdapInterface; |
| 6 | |
| 7 | class DomainConfiguration |
| 8 | { |
| 9 | /** |
| 10 | * The extended configuration options. |
| 11 | * |
| 12 | * @var array |
| 13 | */ |
| 14 | protected static $extended = []; |
| 15 | |
| 16 | /** |
| 17 | * The configuration options array. |
| 18 | * |
| 19 | * The default values for each key indicate the type of value it requires. |
| 20 | * |
| 21 | * @var array |
| 22 | */ |
| 23 | protected $options = [ |
| 24 | // An array of LDAP hosts. |
| 25 | 'hosts' => [], |
| 26 | |
| 27 | // The global LDAP operation timeout limit in seconds. |
| 28 | 'timeout' => 5, |
| 29 | |
| 30 | // The LDAP version to utilize. |
| 31 | 'version' => 3, |
| 32 | |
| 33 | // The port to use for connecting to your hosts. |
| 34 | 'port' => LdapInterface::PORT, |
| 35 | |
| 36 | // The base distinguished name of your domain. |
| 37 | 'base_dn' => '', |
| 38 | |
| 39 | // The username to use for binding. |
| 40 | 'username' => '', |
| 41 | |
| 42 | // The password to use for binding. |
| 43 | 'password' => '', |
| 44 | |
| 45 | // Whether or not to use SSL when connecting. |
| 46 | 'use_ssl' => false, |
| 47 | |
| 48 | // Whether or not to use TLS when connecting. |
| 49 | 'use_tls' => false, |
| 50 | |
| 51 | // Whether or not follow referrals is enabled when performing LDAP operations. |
| 52 | 'follow_referrals' => false, |
| 53 | |
| 54 | // Custom LDAP options. |
| 55 | 'options' => [], |
| 56 | ]; |
| 57 | |
| 58 | /** |
| 59 | * Constructor. |
| 60 | * |
| 61 | * @param array $options |
| 62 | * |
| 63 | * @throws ConfigurationException When an option value given is an invalid type. |
| 64 | */ |
| 65 | public function __construct(array $options = []) |
| 66 | { |
| 67 | $this->options = array_merge($this->options, static::$extended); |
| 68 | |
| 69 | foreach ($options as $key => $value) { |
| 70 | $this->set($key, $value); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Extend the configuration with a custom option, or override an existing. |
| 76 | * |
| 77 | * @param string $option |
| 78 | * @param mixed $default |
| 79 | * |
| 80 | * @return void |
| 81 | */ |
| 82 | public static function extend($option, $default = null) |
| 83 | { |
| 84 | static::$extended[$option] = $default; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Flush the extended configuration options. |
| 89 | * |
| 90 | * @return void |
| 91 | */ |
| 92 | public static function flushExtended() |
| 93 | { |
| 94 | static::$extended = []; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Get all configuration options. |
| 99 | * |
| 100 | * @return array |
| 101 | */ |
| 102 | public function all() |
| 103 | { |
| 104 | return $this->options; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Set a configuration option. |
| 109 | * |
| 110 | * @param string $key |
| 111 | * @param mixed $value |
| 112 | * |
| 113 | * @throws ConfigurationException When an option value given is an invalid type. |
| 114 | */ |
| 115 | public function set($key, $value) |
| 116 | { |
| 117 | if ($this->validate($key, $value)) { |
| 118 | $this->options[$key] = $value; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Returns the value for the specified configuration options. |
| 124 | * |
| 125 | * @param string $key |
| 126 | * |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 127 | * @return mixed |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 128 | * |
| 129 | * @throws ConfigurationException When the option specified does not exist. |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 130 | */ |
| 131 | public function get($key) |
| 132 | { |
| 133 | if (! $this->has($key)) { |
| 134 | throw new ConfigurationException("Option {$key} does not exist."); |
| 135 | } |
| 136 | |
| 137 | return $this->options[$key]; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Checks if a configuration option exists. |
| 142 | * |
| 143 | * @param string $key |
| 144 | * |
| 145 | * @return bool |
| 146 | */ |
| 147 | public function has($key) |
| 148 | { |
| 149 | return array_key_exists($key, $this->options); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Validate the configuration option. |
| 154 | * |
| 155 | * @param string $key |
| 156 | * @param mixed $value |
| 157 | * |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 158 | * @return bool |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 159 | * |
| 160 | * @throws ConfigurationException When an option value given is an invalid type. |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 161 | */ |
| 162 | protected function validate($key, $value) |
| 163 | { |
| 164 | $default = $this->get($key); |
| 165 | |
| 166 | if (is_array($default)) { |
| 167 | $validator = new Validators\ArrayValidator($key, $value); |
| 168 | } elseif (is_int($default)) { |
| 169 | $validator = new Validators\IntegerValidator($key, $value); |
| 170 | } elseif (is_bool($default)) { |
| 171 | $validator = new Validators\BooleanValidator($key, $value); |
| 172 | } else { |
| 173 | $validator = new Validators\StringOrNullValidator($key, $value); |
| 174 | } |
| 175 | |
| 176 | return $validator->validate(); |
| 177 | } |
| 178 | } |