Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

131 lines
2.8KB

  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