5 * Validates a keyword against a list of valid values.
6 * @warning The case-insensitive compare of this function uses PHP's
7 * built-in strtolower and ctype_lower functions, which may
8 * cause problems with international comparisons
10 class HTMLPurifier_AttrDef_Enum
extends HTMLPurifier_AttrDef
14 * Lookup table of valid values.
15 * @todo Make protected
17 public $valid_values = array();
20 * Bool indicating whether or not enumeration is case sensitive.
21 * @note In general this is always case insensitive.
23 protected $case_sensitive = false; // values according to W3C spec
26 * @param $valid_values List of valid values
27 * @param $case_sensitive Bool indicating whether or not case sensitive
29 public function __construct(
30 $valid_values = array(), $case_sensitive = false
32 $this->valid_values
= array_flip($valid_values);
33 $this->case_sensitive
= $case_sensitive;
36 public function validate($string, $config, $context) {
37 $string = trim($string);
38 if (!$this->case_sensitive
) {
39 // we may want to do full case-insensitive libraries
40 $string = ctype_lower($string) ?
$string : strtolower($string);
42 $result = isset($this->valid_values
[$string]);
44 return $result ?
$string : false;
48 * @param $string In form of comma-delimited list of case-insensitive
49 * valid values. Example: "foo,bar,baz". Prepend "s:" to make
52 public function make($string) {
53 if (strlen($string) > 2 && $string[0] == 's' && $string[1] == ':') {
54 $string = substr($string, 2);
59 $values = explode(',', $string);
60 return new HTMLPurifier_AttrDef_Enum($values, $sensitive);