Enum.php 1.9 KB

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