Add method to verify user input exists as class constant of given type

This commit is contained in:
2011-08-28 12:56:53 +01:00
parent 23cca3ff74
commit f0012d7be2
3 changed files with 36 additions and 0 deletions

View File

@@ -7,6 +7,7 @@ class SihnonFramework_Exception_AlreadyInitialisted extends SihnonFramework_E
class SihnonFramework_Exception_NotImplemented extends SihnonFramework_Exception {};
class SihnonFramework_Exception_MissingDefinition extends SihnonFramework_Exception {};
class SihnonFramework_Exception_ClassNotFound extends SihnonFramework_Exception {};
class SihnonFramework_Exception_TemplateException extends SihnonFramework_Exception {};
class SihnonFramework_Exception_AbortEntirePage extends SihnonFramework_Exception_TemplateException {};

View File

@@ -370,6 +370,25 @@ class SihnonFramework_Main {
return $size;
}
public static function isClassConstantValue($class, $prefix, $value) {
try {
$reflected = new ReflectionClass($class);
} catch (ReflectionException $e) {
throw new Sihnon_Exception_ClassNotFound($class);
}
$constants = $reflected->getConstants();
foreach ($constants as $const_name => $const_value) {
if (preg_match("/{$prefix}/", $const_name)) {
if ($value == $const_value) {
return true;
}
}
}
return false;
}
}
SihnonFramework_Main::initialise();