Switch.php 972 B

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