Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 1 | <?php |
| 2 | |
| 3 | namespace Adldap\Configuration; |
| 4 | |
| 5 | use Adldap\Schemas\ActiveDirectory; |
| 6 | use Adldap\Connections\ConnectionInterface; |
| 7 | |
| 8 | /** |
| 9 | * Class DomainConfiguration. |
| 10 | * |
| 11 | * Contains an array of configuration options for a single LDAP connection. |
| 12 | */ |
| 13 | class DomainConfiguration |
| 14 | { |
| 15 | /** |
| 16 | * The configuration options array. |
| 17 | * |
| 18 | * The default values for each key indicate the type of value it requires. |
| 19 | * |
| 20 | * @var array |
| 21 | */ |
| 22 | protected $options = [ |
| 23 | // An array of LDAP hosts. |
| 24 | 'hosts' => [], |
| 25 | |
| 26 | // The global LDAP operation timeout limit in seconds. |
| 27 | 'timeout' => 5, |
| 28 | |
| 29 | // The LDAP version to utilize. |
| 30 | 'version' => 3, |
| 31 | |
| 32 | // The port to use for connecting to your hosts. |
| 33 | 'port' => ConnectionInterface::PORT, |
| 34 | |
| 35 | // The schema to use for your LDAP connection. |
| 36 | 'schema' => ActiveDirectory::class, |
| 37 | |
| 38 | // The base distinguished name of your domain. |
| 39 | 'base_dn' => '', |
| 40 | |
| 41 | // The username to connect to your hosts with. |
| 42 | 'username' => '', |
| 43 | |
| 44 | // The password that is utilized with the above user. |
| 45 | 'password' => '', |
| 46 | |
| 47 | // The account prefix to use when authenticating users. |
| 48 | 'account_prefix' => null, |
| 49 | |
| 50 | // The account suffix to use when authenticating users. |
| 51 | 'account_suffix' => null, |
| 52 | |
| 53 | // Whether or not to use SSL when connecting to your hosts. |
| 54 | 'use_ssl' => false, |
| 55 | |
| 56 | // Whether or not to use TLS when connecting to your hosts. |
| 57 | 'use_tls' => false, |
| 58 | |
| 59 | // Whether or not follow referrals is enabled when performing LDAP operations. |
| 60 | 'follow_referrals' => false, |
| 61 | |
| 62 | // Custom LDAP options that you'd like to utilize. |
| 63 | 'custom_options' => [], |
| 64 | ]; |
| 65 | |
| 66 | /** |
| 67 | * Constructor. |
| 68 | * |
| 69 | * @param array $options |
| 70 | * |
| 71 | * @throws ConfigurationException When an option value given is an invalid type. |
| 72 | */ |
| 73 | public function __construct(array $options = []) |
| 74 | { |
| 75 | foreach ($options as $key => $value) { |
| 76 | $this->set($key, $value); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Sets a configuration option. |
| 82 | * |
| 83 | * Throws an exception if the specified option does |
| 84 | * not exist, or if it's an invalid type. |
| 85 | * |
| 86 | * @param string $key |
| 87 | * @param mixed $value |
| 88 | * |
| 89 | * @throws ConfigurationException When an option value given is an invalid type. |
| 90 | */ |
| 91 | public function set($key, $value) |
| 92 | { |
| 93 | if ($this->validate($key, $value)) { |
| 94 | $this->options[$key] = $value; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Returns the value for the specified configuration options. |
| 100 | * |
| 101 | * Throws an exception if the specified option does not exist. |
| 102 | * |
| 103 | * @param string $key |
| 104 | * |
| 105 | * @throws ConfigurationException When the option specified does not exist. |
| 106 | * |
| 107 | * @return mixed |
| 108 | */ |
| 109 | public function get($key) |
| 110 | { |
| 111 | if ($this->has($key)) { |
| 112 | return $this->options[$key]; |
| 113 | } |
| 114 | |
| 115 | throw new ConfigurationException("Option {$key} does not exist."); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Checks if a configuration option exists. |
| 120 | * |
| 121 | * @param string $key |
| 122 | * |
| 123 | * @return bool |
| 124 | */ |
| 125 | public function has($key) |
| 126 | { |
| 127 | return array_key_exists($key, $this->options); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Validates the new configuration option against its |
| 132 | * default value to ensure it's the correct type. |
| 133 | * |
| 134 | * If an invalid type is given, an exception is thrown. |
| 135 | * |
| 136 | * @param string $key |
| 137 | * @param mixed $value |
| 138 | * |
| 139 | * @throws ConfigurationException When an option value given is an invalid type. |
| 140 | * |
| 141 | * @return bool |
| 142 | */ |
| 143 | protected function validate($key, $value) |
| 144 | { |
| 145 | $default = $this->get($key); |
| 146 | |
| 147 | if (is_array($default)) { |
| 148 | $validator = new Validators\ArrayValidator($key, $value); |
| 149 | } elseif (is_int($default)) { |
| 150 | $validator = new Validators\IntegerValidator($key, $value); |
| 151 | } elseif (is_bool($default)) { |
| 152 | $validator = new Validators\BooleanValidator($key, $value); |
| 153 | } elseif (class_exists($default)) { |
| 154 | $validator = new Validators\ClassValidator($key, $value); |
| 155 | } else { |
| 156 | $validator = new Validators\StringOrNullValidator($key, $value); |
| 157 | } |
| 158 | |
| 159 | return $validator->validate(); |
| 160 | } |
| 161 | } |