PSR-2 reformatting PHPDoc corrections
[htmlpurifier.git] / library / HTMLPurifier / AttrDef / Enum.php
blob8abda7f6e2a55e12c25b14507e3966c05096ebe5
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 * @type array
16 * @todo Make protected
18 public $valid_values = array();
20 /**
21 * Bool indicating whether or not enumeration is case sensitive.
22 * @note In general this is always case insensitive.
24 protected $case_sensitive = false; // values according to W3C spec
26 /**
27 * @param array $valid_values List of valid values
28 * @param bool $case_sensitive Whether or not case sensitive
30 public function __construct($valid_values = array(), $case_sensitive = false)
32 $this->valid_values = array_flip($valid_values);
33 $this->case_sensitive = $case_sensitive;
36 /**
37 * @param string $string
38 * @param HTMLPurifier_Config $config
39 * @param HTMLPurifier_Context $context
40 * @return bool|string
42 public function validate($string, $config, $context)
44 $string = trim($string);
45 if (!$this->case_sensitive) {
46 // we may want to do full case-insensitive libraries
47 $string = ctype_lower($string) ? $string : strtolower($string);
49 $result = isset($this->valid_values[$string]);
51 return $result ? $string : false;
54 /**
55 * @param string $string In form of comma-delimited list of case-insensitive
56 * valid values. Example: "foo,bar,baz". Prepend "s:" to make
57 * case sensitive
58 * @return HTMLPurifier_AttrDef_Enum
60 public function make($string)
62 if (strlen($string) > 2 && $string[0] == 's' && $string[1] == ':') {
63 $string = substr($string, 2);
64 $sensitive = true;
65 } else {
66 $sensitive = false;
68 $values = explode(',', $string);
69 return new HTMLPurifier_AttrDef_Enum($values, $sensitive);
73 // vim: et sw=4 sts=4