Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / lib / htmlpurifier / HTMLPurifier / AttrDef / Enum.php
blob011adda8bd2c30ce43fdfe56e482f7417cc27686
1 <?php
3 require_once 'HTMLPurifier/AttrDef.php';
5 // Enum = Enumerated
6 /**
7 * Validates a keyword against a list of valid values.
8 * @warning The case-insensitive compare of this function uses PHP's
9 * built-in strtolower and ctype_lower functions, which may
10 * cause problems with international comparisons
12 class HTMLPurifier_AttrDef_Enum extends HTMLPurifier_AttrDef
15 /**
16 * Lookup table of valid values.
18 var $valid_values = array();
20 /**
21 * Bool indicating whether or not enumeration is case sensitive.
22 * @note In general this is always case insensitive.
24 var $case_sensitive = false; // values according to W3C spec
26 /**
27 * @param $valid_values List of valid values
28 * @param $case_sensitive Bool indicating whether or not case sensitive
30 function HTMLPurifier_AttrDef_Enum(
31 $valid_values = array(), $case_sensitive = false
32 ) {
33 $this->valid_values = array_flip($valid_values);
34 $this->case_sensitive = $case_sensitive;
37 function validate($string, $config, &$context) {
38 $string = trim($string);
39 if (!$this->case_sensitive) {
40 // we may want to do full case-insensitive libraries
41 $string = ctype_lower($string) ? $string : strtolower($string);
43 $result = isset($this->valid_values[$string]);
45 return $result ? $string : false;
48 /**
49 * @param $string In form of comma-delimited list of case-insensitive
50 * valid values. Example: "foo,bar,baz". Prepend "s:" to make
51 * case sensitive
53 function make($string) {
54 if (strlen($string) > 2 && $string[0] == 's' && $string[1] == ':') {
55 $string = substr($string, 2);
56 $sensitive = true;
57 } else {
58 $sensitive = false;
60 $values = explode(',', $string);
61 return new HTMLPurifier_AttrDef_Enum($values, $sensitive);