Add vim modelines to all files.
[htmlpurifier.git] / library / HTMLPurifier / AttrDef / Enum.php
blob5d603ebcc67d51ddad1eb47010d702b157f7fa85
1 <?php
3 // Enum = Enumerated
4 /**
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
9 */
10 class HTMLPurifier_AttrDef_Enum extends HTMLPurifier_AttrDef
13 /**
14 * Lookup table of valid values.
15 * @todo Make protected
17 public $valid_values = array();
19 /**
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
25 /**
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
31 ) {
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;
47 /**
48 * @param $string In form of comma-delimited list of case-insensitive
49 * valid values. Example: "foo,bar,baz". Prepend "s:" to make
50 * case sensitive
52 public function make($string) {
53 if (strlen($string) > 2 && $string[0] == 's' && $string[1] == ':') {
54 $string = substr($string, 2);
55 $sensitive = true;
56 } else {
57 $sensitive = false;
59 $values = explode(',', $string);
60 return new HTMLPurifier_AttrDef_Enum($values, $sensitive);
65 // vim: et sw=4 sts=4