]>
Commit | Line | Data |
---|---|---|
d4949327 NL |
1 | <?php\r |
2 | \r | |
3 | // Enum = Enumerated\r | |
4 | /**\r | |
5 | * Validates a keyword against a list of valid values.\r | |
6 | * @warning The case-insensitive compare of this function uses PHP's\r | |
7 | * built-in strtolower and ctype_lower functions, which may\r | |
8 | * cause problems with international comparisons\r | |
9 | */\r | |
10 | class HTMLPurifier_AttrDef_Enum extends HTMLPurifier_AttrDef\r | |
11 | {\r | |
12 | \r | |
13 | /**\r | |
14 | * Lookup table of valid values.\r | |
15 | * @type array\r | |
16 | * @todo Make protected\r | |
17 | */\r | |
18 | public $valid_values = array();\r | |
19 | \r | |
20 | /**\r | |
21 | * Bool indicating whether or not enumeration is case sensitive.\r | |
22 | * @note In general this is always case insensitive.\r | |
23 | */\r | |
24 | protected $case_sensitive = false; // values according to W3C spec\r | |
25 | \r | |
26 | /**\r | |
27 | * @param array $valid_values List of valid values\r | |
28 | * @param bool $case_sensitive Whether or not case sensitive\r | |
29 | */\r | |
30 | public function __construct($valid_values = array(), $case_sensitive = false)\r | |
31 | {\r | |
32 | $this->valid_values = array_flip($valid_values);\r | |
33 | $this->case_sensitive = $case_sensitive;\r | |
34 | }\r | |
35 | \r | |
36 | /**\r | |
37 | * @param string $string\r | |
38 | * @param HTMLPurifier_Config $config\r | |
39 | * @param HTMLPurifier_Context $context\r | |
40 | * @return bool|string\r | |
41 | */\r | |
42 | public function validate($string, $config, $context)\r | |
43 | {\r | |
44 | $string = trim($string);\r | |
45 | if (!$this->case_sensitive) {\r | |
46 | // we may want to do full case-insensitive libraries\r | |
47 | $string = ctype_lower($string) ? $string : strtolower($string);\r | |
48 | }\r | |
49 | $result = isset($this->valid_values[$string]);\r | |
50 | \r | |
51 | return $result ? $string : false;\r | |
52 | }\r | |
53 | \r | |
54 | /**\r | |
55 | * @param string $string In form of comma-delimited list of case-insensitive\r | |
56 | * valid values. Example: "foo,bar,baz". Prepend "s:" to make\r | |
57 | * case sensitive\r | |
58 | * @return HTMLPurifier_AttrDef_Enum\r | |
59 | */\r | |
60 | public function make($string)\r | |
61 | {\r | |
62 | if (strlen($string) > 2 && $string[0] == 's' && $string[1] == ':') {\r | |
63 | $string = substr($string, 2);\r | |
64 | $sensitive = true;\r | |
65 | } else {\r | |
66 | $sensitive = false;\r | |
67 | }\r | |
68 | $values = explode(',', $string);\r | |
69 | return new HTMLPurifier_AttrDef_Enum($values, $sensitive);\r | |
70 | }\r | |
71 | }\r | |
72 | \r | |
73 | // vim: et sw=4 sts=4\r |