Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace SyzerPHPEnvironment;
- /**
- * Config.
- */
- class Config extends Environment
- {
- /**
- * validate().
- *
- * Validate the config array.
- *
- * @param array $conf The config array.
- *
- * @throws DomainException If `$conf` argument is empty.
- * @throws DomainException If `$conf` argument does not have a depth of 2.
- * @throws UnexpectedValueException If a section is not the start of an array.
- * @throws UnexpectedValueException If the section variable names are not capital letters.
- * @throws LengthException If the section variable name and/or value is too long.
- *
- * @return void.
- */
- public static function validate(array $conf): void
- {
- if (empty($conf)) {
- throw new ExceptionDomainException('The config array is empty.');
- }
- if (depth($conf) != 2) {
- throw new ExceptionDomainException(sprintf(
- 'The config array does not have a depth of 2. Depth: `%s`.',
- (string) depth($conf)
- ));
- }
- foreach ($conf as $var => $val) {
- if (!is_array($val)) {
- throw new ExceptionUnexpectedValueException(sprintf(
- 'The section is not the start of an array. Passed: `%s`.',
- e($var)
- ));
- }
- foreach ($val as $var2 => $val2) {
- if (!ctype_upper(str_replace('_', '', $var2))) {
- throw new ExceptionUnexpectedValueException(sprintf(
- 'The section variable name must all be caps. Passed: `%s`.',
- e($var2)
- ));
- }
- if (strlen($var2) > 30 || strlen($val2) > 250) {
- throw new ExceptionLengthException(sprintf(
- 'The `$var2` and/or `$val2` variable is too long. Passed: `$var2` = `%s` `$val2` = `%s`.',
- (string) strlen($var2),
- (string) strlen($val2)
- ));
- }
- }
- }
- self::clearConfig();
- self::$conf = $conf;
- }
- }
Add Comment
Please, Sign In to add comment