textwheelrule.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /*
  3. * TextWheel 0.1
  4. *
  5. * let's reinvent the wheel one last time
  6. *
  7. * This library of code is meant to be a fast and universal replacement
  8. * for any and all text-processing systems written in PHP
  9. *
  10. * It is dual-licensed for any use under the GNU/GPL2 and MIT licenses,
  11. * as suits you best
  12. *
  13. * (c) 2009 Fil - fil@rezo.net
  14. * Documentation & http://zzz.rezo.net/-TextWheel-
  15. *
  16. * Usage: $wheel = new TextWheel(); echo $wheel->text($text);
  17. *
  18. */
  19. if (!defined('_ECRIRE_INC_VERSION')) return;
  20. class TextWheelRule {
  21. ## rule description
  22. # optional
  23. var $priority = 0; # rule priority (rules are applied in ascending order)
  24. # -100 = application escape, +100 = application unescape
  25. var $name; # rule's name
  26. var $author; # rule's author
  27. var $url; # rule's homepage
  28. var $package; # rule belongs to package
  29. var $version; # rule version
  30. var $test; # rule test function
  31. var $disabled=false; # true if rule is disabled
  32. ## rule init checks
  33. ## the rule will be applied if the text...
  34. # optional
  35. var $if_chars; # ...contains one of these chars
  36. var $if_str; # ...contains this string (case sensitive)
  37. var $if_stri; # ...contains this string (case insensitive)
  38. var $if_match; # ...matches this simple expr
  39. ## rule effectors, matching
  40. # mandatory
  41. var $type; # 'preg' (default), 'str', 'all', 'split'...
  42. var $match; # matching string or expression
  43. # optional
  44. # var $limit; # limit number of applications (unused)
  45. ## rule effectors, replacing
  46. # mandatory
  47. var $replace; # replace match with this expression
  48. # optional
  49. var $is_callback=false; # $replace is a callback function
  50. var $is_wheel; # flag to create a sub-wheel from rules given as replace
  51. var $pick_match = 0; # item to pick for sub-wheel replace
  52. var $glue = null; # glue for implode ending split rule
  53. # optional
  54. # language specific
  55. var $require; # file to require_once
  56. var $create_replace; # do create_function('$m', %) on $this->replace, $m is the matched array
  57. # optimizations
  58. var $func_replace;
  59. /**
  60. * Rule constructor
  61. * @param <type> $args
  62. * @return <type>
  63. */
  64. public function TextWheelRule($args) {
  65. if (!is_array($args))
  66. return;
  67. foreach($args as $k=>$v)
  68. if (property_exists($this, $k))
  69. $this->$k = $args[$k];
  70. $this->checkValidity(); // check that the rule is valid
  71. }
  72. /**
  73. * Rule checker
  74. */
  75. protected function checkValidity(){
  76. if ($this->type=='split'){
  77. if (is_array($this->match))
  78. throw new InvalidArgumentException('match argument for split rule can\'t be an array');
  79. if (isset($this->glue) AND is_array($this->glue))
  80. throw new InvalidArgumentException('glue argument for split rule can\'t be an array');
  81. }
  82. }
  83. }