[SPIP] ~maj v3.2.9-->v3.2.11
[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 # DEPRECATED : create_function deprecated a partir de PHP 7.2, ne plus utiliser
69 public $create_replace; # do create_function('$m', %) on $this->replace, $m is the matched array
70
71 # optimizations
72 public $func_replace;
73 public $compilereplace;
74
75 /**
76 * Rule constructor
77 *
78 * @param <type> $args
79 * @return <type>
80 */
81 public function __construct($args) {
82 if (!is_array($args)) {
83 return;
84 }
85 foreach ($args as $k => $v) {
86 if (property_exists($this, $k)) {
87 $this->$k = $args[$k];
88 }
89 }
90 $this->checkValidity(); // check that the rule is valid
91 }
92
93 /**
94 * Rule checker
95 */
96 protected function checkValidity() {
97 if ($this->type == 'split') {
98 if (is_array($this->match)) {
99 throw new InvalidArgumentException('match argument for split rule can\'t be an array');
100 }
101 if (isset($this->glue) and is_array($this->glue)) {
102 throw new InvalidArgumentException('glue argument for split rule can\'t be an array');
103 }
104 }
105 }
106
107 }