Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
PHP Trait for validating a key=>value configuration array
<?php
namespace Patterns;
trait ConfigurableTrait {
private $requiredConfigKeys;
private function validateConfiguration($config)
{
if (empty($config))
throw new \Exception("\$config parameter should be an array of configuration options");
if (empty($this->requiredConfigKeys))
throw new \Exception("\$this->requiredConfigKeys member should be an array of required configuration options");
foreach ($this->requiredConfigKeys as $key) {
$this->configurationKeyExists($key, $config);
}
}
private function configurationKeyExists($configurationKey, $configurationArray) {
if (!array_key_exists($configurationKey, $configurationArray))
throw new \Exception("Configuration key '$configurationKey' does not exist");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment