[SPIP][PLUGINS] v3.0-->v3.2
[lhc/web/www.git] / www / plugins-dist / textwheel / engine / textwheelruleset.php
index 01f145c..3f6c1f2 100644 (file)
  *
  */
 
-if (!defined('_ECRIRE_INC_VERSION')) return;
+if (!defined('_ECRIRE_INC_VERSION')) {
+       return;
+}
 
-require_once dirname(__FILE__)."/textwheelrule.php";
+require_once dirname(__FILE__) . "/textwheelrule.php";
 
 abstract class TextWheelDataSet {
        # list of data
@@ -34,57 +36,68 @@ abstract class TextWheelDataSet {
         * @param string $path
         * @return string
         */
-       protected function findFile(&$file, $path=''){
+       protected function findFile(&$file, $path = '') {
                static $default_path;
 
                // absolute file path ?
-               if (file_exists($file))
+               if (file_exists($file)) {
                        return $file;
+               }
 
                // file embed with texwheels, relative to calling ruleset
-               if ($path AND file_exists($f = $path.$file))
+               if ($path and file_exists($f = $path . $file)) {
                        return $f;
+               }
 
                // textwheel default path ?
-               if (!$default_path)
-                       $default_path = dirname(__FILE__).'/../wheels/';
-               if (file_exists($f = $default_path.$file))
+               if (!$default_path) {
+                       $default_path = dirname(__FILE__) . '/../wheels/';
+               }
+               if (file_exists($f = $default_path . $file)) {
                        return $f;
+               }
 
                return false;
        }
-       
+
        /**
         * Load a yaml file describing data
+        *
         * @param string $file
         * @param string $default_path
         * @return array
         */
-       protected function loadFile(&$file, $default_path='') {
-               if (!preg_match(',[.]yaml$,i',$file)
-                 // external rules
-                 OR !$file = $this->findFile($file,$default_path))
+       protected function loadFile(&$file, $default_path = '') {
+               if (!preg_match(',[.]yaml$,i', $file)
+                       // external rules
+                       or !$file = $this->findFile($file, $default_path)
+               ) {
                        return array();
+               }
 
                defined('_YAML_EVAL_PHP') || define('_YAML_EVAL_PHP', false);
                if (!function_exists('yaml_decode')) {
-                       if (function_exists('include_spip'))
+                       if (function_exists('include_spip')) {
                                include_spip('inc/yaml-mini');
-                       else
-                               require_once dirname(__FILE__).'/../inc/yaml.php';
+                       } else {
+                               require_once dirname(__FILE__) . '/../inc/yaml.php';
+                       }
                }
                $dataset = yaml_decode(file_get_contents($file));
 
-               if (is_null($dataset))
+               if (is_null($dataset)) {
                        $dataset = array();
+               }
 #                      throw new DomainException('yaml file is empty, unreadable or badly formed: '.$file.var_export($dataset,true));
 
                // if a php file with same name exists
                // include it as it contains callback functions
-               if ($f = preg_replace(',[.]yaml$,i','.php',$file)
-               AND file_exists($f)) {
+               if ($f = preg_replace(',[.]yaml$,i', '.php', $file)
+                       and file_exists($f)
+               ) {
                        $dataset[] = array('require' => $f, 'priority' => -1000);
-}
+               }
+
                return $dataset;
        }
 
@@ -100,9 +113,10 @@ class TextWheelRuleSet extends TextWheelDataSet {
         * @param array|string $ruleset
         * @param string $filepath
         */
-       public function TextWheelRuleSet($ruleset = array(), $filepath='') {
-               if ($ruleset)
+       public function __construct($ruleset = array(), $filepath = '') {
+               if ($ruleset) {
                        $this->addRules($ruleset, $filepath);
+               }
        }
 
        /**
@@ -114,33 +128,39 @@ class TextWheelRuleSet extends TextWheelDataSet {
         * @param string $class
         * @return class
         */
-       public static function &loader($ruleset, $callback='', $class='TextWheelRuleSet'){
+       public static function &loader($ruleset, $callback = '', $class = 'TextWheelRuleSet') {
 
                $ruleset = new $class($ruleset);
-               if ($callback)
+               if ($callback) {
                        $callback($ruleset);
+               }
 
                return $ruleset;
        }
+
        /**
         * Get an existing named rule in order to override it
         *
         * @param string $name
         * @return string
         */
-       public function &getRule($name){
-               if (isset($this->data[$name]))
+       public function &getRule($name) {
+               if (isset($this->data[$name])) {
                        return $this->data[$name];
+               }
                $result = null;
+
                return $result;
        }
-       
+
        /**
         * get sorted Rules
+        *
         * @return array
         */
-       public function &getRules(){
+       public function &getRules() {
                $this->sort();
+
                return $this->data;
        }
 
@@ -151,8 +171,9 @@ class TextWheelRuleSet extends TextWheelDataSet {
         */
        public function addRule($rule) {
                # cast array-rule to object
-               if (is_array($rule))
+               if (is_array($rule)) {
                        $rule = new TextWheelRule($rule);
+               }
                $this->data[] = $rule;
                $this->sorted = false;
        }
@@ -167,11 +188,13 @@ class TextWheelRuleSet extends TextWheelDataSet {
         * @param array|string $rules
         * @param string $filepath
         */
-       public function addRules($rules, $filepath='') {
+       public function addRules($rules, $filepath = '') {
                // rules can be an array of filename
-               if (is_array($rules) AND is_string(reset($rules))) {
-                       foreach($rules as $i=>$filename)
+               if (is_array($rules) and is_string(reset($rules))) {
+                       foreach ($rules as $i => $filename) {
                                $this->addRules($filename);
+                       }
+
                        return;
                }
 
@@ -179,17 +202,18 @@ class TextWheelRuleSet extends TextWheelDataSet {
                if (is_string($rules)) {
                        $file = $rules; // keep the real filename
                        $rules = $this->loadFile($file, $filepath);
-                       $filepath = dirname($file).'/';
+                       $filepath = dirname($file) . '/';
                }
 
                // rules can be an array of rules
-               if (is_array($rules) AND count($rules)){
+               if (is_array($rules) and count($rules)) {
                        # cast array-rules to objects
                        foreach ($rules as $i => $rule) {
-                               if (is_array($rule))
+                               if (is_array($rule)) {
                                        $rules[$i] = new TextWheelRule($rule);
+                               }
                                // load subwheels when necessary
-                               if ($rules[$i]->is_wheel){
+                               if ($rules[$i]->is_wheel) {
                                        // subruleset is of the same class as current ruleset
                                        $class = get_class($this);
                                        $rules[$i]->replace = new $class($rules[$i]->replace, $filepath);
@@ -208,13 +232,16 @@ class TextWheelRuleSet extends TextWheelDataSet {
        protected function sort() {
                if (!$this->sorted) {
                        $rulz = array();
-                       foreach($this->data as $index => $rule)
-                               if (!$rule->disabled)
+                       foreach ($this->data as $index => $rule) {
+                               if (!$rule->disabled) {
                                        $rulz[intval($rule->priority)][$index] = $rule;
+                               }
+                       }
                        ksort($rulz);
                        $this->data = array();
-                       foreach($rulz as $rules)
+                       foreach ($rulz as $rules) {
                                $this->data += $rules;
+                       }
 
                        $this->sorted = true;
                }