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