Switch.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. /**
  3. * Decorator that, depending on a token, switches between two definitions.
  4. */
  5. class HTMLPurifier_AttrDef_Switch
  6. {
  7. /**
  8. * @type string
  9. */
  10. protected $tag;
  11. /**
  12. * @type HTMLPurifier_AttrDef
  13. */
  14. protected $withTag;
  15. /**
  16. * @type HTMLPurifier_AttrDef
  17. */
  18. protected $withoutTag;
  19. /**
  20. * @param string $tag Tag name to switch upon
  21. * @param HTMLPurifier_AttrDef $with_tag Call if token matches tag
  22. * @param HTMLPurifier_AttrDef $without_tag Call if token doesn't match, or there is no token
  23. */
  24. public function __construct($tag, $with_tag, $without_tag)
  25. {
  26. $this->tag = $tag;
  27. $this->withTag = $with_tag;
  28. $this->withoutTag = $without_tag;
  29. }
  30. /**
  31. * @param string $string
  32. * @param HTMLPurifier_Config $config
  33. * @param HTMLPurifier_Context $context
  34. * @return bool|string
  35. */
  36. public function validate($string, $config, $context)
  37. {
  38. $token = $context->get('CurrentToken', true);
  39. if (!$token || $token->name !== $this->tag) {
  40. return $this->withoutTag->validate($string, $config, $context);
  41. } else {
  42. return $this->withTag->validate($string, $config, $context);
  43. }
  44. }
  45. }
  46. // vim: et sw=4 sts=4