You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ValidatorAtom.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. /**
  11. * @type string
  12. */
  13. protected $context;
  14. /**
  15. * @type object
  16. */
  17. protected $obj;
  18. /**
  19. * @type string
  20. */
  21. protected $member;
  22. /**
  23. * @type mixed
  24. */
  25. protected $contents;
  26. public function __construct($context, $obj, $member)
  27. {
  28. $this->context = $context;
  29. $this->obj = $obj;
  30. $this->member = $member;
  31. $this->contents =& $obj->$member;
  32. }
  33. /**
  34. * @return HTMLPurifier_ConfigSchema_ValidatorAtom
  35. */
  36. public function assertIsString()
  37. {
  38. if (!is_string($this->contents)) {
  39. $this->error('must be a string');
  40. }
  41. return $this;
  42. }
  43. /**
  44. * @return HTMLPurifier_ConfigSchema_ValidatorAtom
  45. */
  46. public function assertIsBool()
  47. {
  48. if (!is_bool($this->contents)) {
  49. $this->error('must be a boolean');
  50. }
  51. return $this;
  52. }
  53. /**
  54. * @return HTMLPurifier_ConfigSchema_ValidatorAtom
  55. */
  56. public function assertIsArray()
  57. {
  58. if (!is_array($this->contents)) {
  59. $this->error('must be an array');
  60. }
  61. return $this;
  62. }
  63. /**
  64. * @return HTMLPurifier_ConfigSchema_ValidatorAtom
  65. */
  66. public function assertNotNull()
  67. {
  68. if ($this->contents === null) {
  69. $this->error('must not be null');
  70. }
  71. return $this;
  72. }
  73. /**
  74. * @return HTMLPurifier_ConfigSchema_ValidatorAtom
  75. */
  76. public function assertAlnum()
  77. {
  78. $this->assertIsString();
  79. if (!ctype_alnum($this->contents)) {
  80. $this->error('must be alphanumeric');
  81. }
  82. return $this;
  83. }
  84. /**
  85. * @return HTMLPurifier_ConfigSchema_ValidatorAtom
  86. */
  87. public function assertNotEmpty()
  88. {
  89. if (empty($this->contents)) {
  90. $this->error('must not be empty');
  91. }
  92. return $this;
  93. }
  94. /**
  95. * @return HTMLPurifier_ConfigSchema_ValidatorAtom
  96. */
  97. public function assertIsLookup()
  98. {
  99. $this->assertIsArray();
  100. foreach ($this->contents as $v) {
  101. if ($v !== true) {
  102. $this->error('must be a lookup array');
  103. }
  104. }
  105. return $this;
  106. }
  107. /**
  108. * @param string $msg
  109. * @throws HTMLPurifier_ConfigSchema_Exception
  110. */
  111. protected function error($msg)
  112. {
  113. throw new HTMLPurifier_ConfigSchema_Exception(ucfirst($this->member) . ' in ' . $this->context . ' ' . $msg);
  114. }
  115. }
  116. // vim: et sw=4 sts=4