Yaffs site version 1.1
[yaffs-website] / vendor / ezyang / htmlpurifier / library / HTMLPurifier / AttrDef / Enum.php
1 <?php
2
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
11 {
12
13     /**
14      * Lookup table of valid values.
15      * @type array
16      * @todo Make protected
17      */
18     public $valid_values = array();
19
20     /**
21      * Bool indicating whether or not enumeration is case sensitive.
22      * @note In general this is always case insensitive.
23      */
24     protected $case_sensitive = false; // values according to W3C spec
25
26     /**
27      * @param array $valid_values List of valid values
28      * @param bool $case_sensitive Whether or not case sensitive
29      */
30     public function __construct($valid_values = array(), $case_sensitive = false)
31     {
32         $this->valid_values = array_flip($valid_values);
33         $this->case_sensitive = $case_sensitive;
34     }
35
36     /**
37      * @param string $string
38      * @param HTMLPurifier_Config $config
39      * @param HTMLPurifier_Context $context
40      * @return bool|string
41      */
42     public function validate($string, $config, $context)
43     {
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);
48         }
49         $result = isset($this->valid_values[$string]);
50
51         return $result ? $string : false;
52     }
53
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
59      */
60     public function make($string)
61     {
62         if (strlen($string) > 2 && $string[0] == 's' && $string[1] == ':') {
63             $string = substr($string, 2);
64             $sensitive = true;
65         } else {
66             $sensitive = false;
67         }
68         $values = explode(',', $string);
69         return new HTMLPurifier_AttrDef_Enum($values, $sensitive);
70     }
71 }
72
73 // vim: et sw=4 sts=4