[SPIP] +spip v3.0.17
[lhc/web/clavette_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')) return;
22
23 class TextWheelRule {
24
25 ## rule description
26 # optional
27 var $priority = 0; # rule priority (rules are applied in ascending order)
28 # -100 = application escape, +100 = application unescape
29 var $name; # rule's name
30 var $author; # rule's author
31 var $url; # rule's homepage
32 var $package; # rule belongs to package
33 var $version; # rule version
34 var $test; # rule test function
35 var $disabled=false; # true if rule is disabled
36
37 ## rule init checks
38 ## the rule will be applied if the text...
39 # optional
40 var $if_chars; # ...contains one of these chars
41 var $if_str; # ...contains this string (case sensitive)
42 var $if_stri; # ...contains this string (case insensitive)
43 var $if_match; # ...matches this simple expr
44
45
46 ## rule effectors, matching
47 # mandatory
48 var $type; # 'preg' (default), 'str', 'all', 'split'...
49 var $match; # matching string or expression
50 # optional
51 # var $limit; # limit number of applications (unused)
52
53 ## rule effectors, replacing
54 # mandatory
55 var $replace; # replace match with this expression
56
57 # optional
58 var $is_callback=false; # $replace is a callback function
59 var $is_wheel; # flag to create a sub-wheel from rules given as replace
60 var $pick_match = 0; # item to pick for sub-wheel replace
61 var $glue = null; # glue for implode ending split rule
62
63 # optional
64 # language specific
65 var $require; # file to require_once
66 var $create_replace; # do create_function('$m', %) on $this->replace, $m is the matched array
67
68 # optimizations
69 var $func_replace;
70
71 /**
72 * Rule constructor
73 * @param <type> $args
74 * @return <type>
75 */
76 public function TextWheelRule($args) {
77 if (!is_array($args))
78 return;
79 foreach($args as $k=>$v)
80 if (property_exists($this, $k))
81 $this->$k = $args[$k];
82 $this->checkValidity(); // check that the rule is valid
83 }
84
85 /**
86 * Rule checker
87 */
88 protected function checkValidity(){
89 if ($this->type=='split'){
90 if (is_array($this->match))
91 throw new InvalidArgumentException('match argument for split rule can\'t be an array');
92 if (isset($this->glue) AND is_array($this->glue))
93 throw new InvalidArgumentException('glue argument for split rule can\'t be an array');
94 }
95 }
96
97 }