ValidatorAtom.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * Fluent interface for validating the contents of member variables.
  4. * This should be immutable. See HTMLPurifier_ConfigSchema_Validator for
  5. * use-cases. We name this an 'atom' because it's ONLY for validations that
  6. * are independent and usually scalar.
  7. */
  8. class HTMLPurifier_ConfigSchema_ValidatorAtom
  9. {
  10. protected $context, $obj, $member, $contents;
  11. public function __construct($context, $obj, $member) {
  12. $this->context = $context;
  13. $this->obj = $obj;
  14. $this->member = $member;
  15. $this->contents =& $obj->$member;
  16. }
  17. public function assertIsString() {
  18. if (!is_string($this->contents)) $this->error('must be a string');
  19. return $this;
  20. }
  21. public function assertIsBool() {
  22. if (!is_bool($this->contents)) $this->error('must be a boolean');
  23. return $this;
  24. }
  25. public function assertIsArray() {
  26. if (!is_array($this->contents)) $this->error('must be an array');
  27. return $this;
  28. }
  29. public function assertNotNull() {
  30. if ($this->contents === null) $this->error('must not be null');
  31. return $this;
  32. }
  33. public function assertAlnum() {
  34. $this->assertIsString();
  35. if (!ctype_alnum($this->contents)) $this->error('must be alphanumeric');
  36. return $this;
  37. }
  38. public function assertNotEmpty() {
  39. if (empty($this->contents)) $this->error('must not be empty');
  40. return $this;
  41. }
  42. public function assertIsLookup() {
  43. $this->assertIsArray();
  44. foreach ($this->contents as $v) {
  45. if ($v !== true) $this->error('must be a lookup array');
  46. }
  47. return $this;
  48. }
  49. protected function error($msg) {
  50. throw new HTMLPurifier_ConfigSchema_Exception(ucfirst($this->member) . ' in ' . $this->context . ' ' . $msg);
  51. }
  52. }
  53. // vim: et sw=4 sts=4