Added ability to compare two option values
authorAlexander Ebert <ebert@woltlab.com>
Tue, 27 May 2014 18:09:39 +0000 (20:09 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Tue, 27 May 2014 18:09:39 +0000 (20:09 +0200)
13 files changed:
wcfsetup/install/files/lib/system/option/AbstractOptionType.class.php
wcfsetup/install/files/lib/system/option/BooleanOptionType.class.php
wcfsetup/install/files/lib/system/option/DateOptionType.class.php
wcfsetup/install/files/lib/system/option/FileSizeOptionType.class.php
wcfsetup/install/files/lib/system/option/FloatOptionType.class.php
wcfsetup/install/files/lib/system/option/IOptionType.class.php
wcfsetup/install/files/lib/system/option/IntegerOptionType.class.php
wcfsetup/install/files/lib/system/option/TextareaOptionType.class.php
wcfsetup/install/files/lib/system/option/user/group/BBCodeSelectUserGroupOptionType.class.php
wcfsetup/install/files/lib/system/option/user/group/InfiniteIntegerUserGroupOptionType.class.php
wcfsetup/install/files/lib/system/option/user/group/InfiniteInverseIntegerUserGroupOptionType.class.php
wcfsetup/install/files/lib/system/option/user/group/InverseIntegerUserGroupOptionType.class.php
wcfsetup/install/files/lib/system/option/user/group/UserGroupsUserGroupOptionType.class.php

index a8356ab368ad9b8a3977eb402e66e827cdc51217..2725a8b9630b240a5f7cf702808f9018db21781b 100644 (file)
@@ -44,4 +44,11 @@ abstract class AbstractOptionType implements IOptionType {
        public function supportI18n() {
                return $this->supportI18n;
        }
+       
+       /**
+        * @see \wcf\system\option\IOptionType::compare()
+        */
+       public function compare() {
+               return 0;
+       }
 }
index cab4b4ae4ef9b96eff9a4afa594b50e7c38e7e3d..7c8fb32b3a84cdd197581e7abe1dce931832d0c3 100644 (file)
@@ -90,4 +90,15 @@ class BooleanOptionType extends AbstractOptionType implements ISearchableUserOpt
        public function getConditionData(Option $option, $newValue) {
                return $newValue;
        }
+       
+       /**
+        * @see \wcf\system\option\IOptionType::compare()
+        */
+       public function compare($value1, $value2) {
+               if ($value1 == $value2) {
+                       return 0;
+               }
+               
+               return ($value1 === true) ? 1 : -1;
+       }
 }
index 46fc3bb97246103cb1ab34a974c713ff9d94dd8f..1af53100a7ce3d105c4aa9b01057283905e7d389 100644 (file)
@@ -38,4 +38,15 @@ class DateOptionType extends TextOptionType {
                        throw new UserInputException($option->optionName, 'validationFailed');
                }
        }
+       
+       /**
+        * @see \wcf\system\option\IOptionType::compare()
+        */
+       public function compare($value1, $value2) {
+               if ($value1 == $value2) {
+                       return 0;
+               }
+               
+               return (strtotime($value1) > strtotime($value2)) ? 1 : -1;
+       }
 }
index 034f94c721e283a3e1b48158c6fe4f76fb8f6936..cf85e6fc4f833b9908ce4565fda8dfed5f7d5fa2 100644 (file)
@@ -63,4 +63,15 @@ class FileSizeOptionType extends TextOptionType {
                $value = FileUtil::formatFileSize($value);
                return parent::getFormElement($option, $value);
        }
+       
+       /**
+        * @see \wcf\system\option\IOptionType::compare()
+        */
+       public function compare($value1, $value2) {
+               if ($value1 == $value2) {
+                       return 0;
+               }
+               
+               return ($value1 > $value2) ? 1 : -1;
+       }
 }
index abfc82fa47f189b07ddbc60b8fefa29ab75463a6..c9153f18b4e9b4695ec0caf7155aec61e0d4a020 100644 (file)
@@ -37,4 +37,15 @@ class FloatOptionType extends TextOptionType {
                $newValue = str_replace(WCF::getLanguage()->get('wcf.global.decimalPoint'), '.', $newValue);
                return floatval($newValue);
        }
+       
+       /**
+        * @see \wcf\system\option\IOptionType::compare()
+        */
+       public function compare($value1, $value2) {
+               if ($value1 == $value2) {
+                       return 0;
+               }
+               
+               return ($value1 > $value2) ? 1 : -1;
+       }
 }
index 23ae61c63181fd3e9a9fa351151a48dcf3d2279a..a7dbfb9473a8c05f6458189f66cc31191bc54aef 100644 (file)
@@ -55,4 +55,18 @@ interface IOptionType {
         * @return      boolean
         */
        public function supportI18n();
+       
+       /**
+        * Compares two values and returns a PHP-like comparison result.
+        * 
+        *   $value1 < $value2  => -1
+        *   $value1 == $value2 => 0
+        *   $value1 > $value2  => 1
+        * 
+        * 
+        * @param       mixed           $value1
+        * @param       mixed           $value2
+        * @return      integer
+        */
+       public function compare($value1, $value2);
 }
index 51cbd764688228b085e61ea67651997fa467f3a1..e74b297d24dfb09fc1f46be2faa0cd2368703140 100644 (file)
@@ -51,4 +51,15 @@ class IntegerOptionType extends TextOptionType {
                        throw new UserInputException($option->optionName, 'tooHigh');
                }
        }
+       
+       /**
+        * @see \wcf\system\option\IOptionType::compare()
+        */
+       public function compare($value1, $value2) {
+               if ($value1 == $value2) {
+                       return 0;
+               }
+               
+               return ($value1 > $value2) ? 1 : -1;
+       }
 }
index 26cf6231ab682cf7891f17f068f19e4522d872b5..73b55bde4250bf603c54a707c5981ed68d8642ab 100644 (file)
@@ -54,4 +54,27 @@ class TextareaOptionType extends TextOptionType {
                
                return $newValue;
        }
+       
+       /**
+        * @see \wcf\system\option\IOptionType::compare()
+        */
+       public function compare($value1, $value2) {
+               $value1 = explode("\n", StringUtil::unifyNewlines($value1));
+               $value2 = explode("\n", StringUtil::unifyNewlines($value2));
+               
+               // check if value1 contains more elements than value2
+               $diff = array_diff($value1, $value2);
+               if (!empty($diff)) {
+                       return 1;
+               }
+               
+               // check if value1 contains less elements than value2
+               $diff = array_diff($value2, $value1);
+               if (!empty($diff)) {
+                       return -1;
+               }
+               
+               // both lists are equal
+               return 0;
+       }
 }
index 89e010e629654ac92df2c0d50712db1fa0940974..1b4b6f9c7f51e6fab85562c8fb8da71613277726 100644 (file)
@@ -121,4 +121,40 @@ class BBCodeSelectUserGroupOptionType extends AbstractOptionType implements IUse
                        }
                }
        }
+       
+       /**
+        * @see \wcf\system\option\IOptionType::compare()
+        */
+       public function compare($value1, $value2) {
+               $value1 = explode(',', $value1);
+               $value2 = explode(',', $value2);
+               
+               // handle special 'all' value
+               if (in_array('all', $value1)) {
+                       if (in_array('all', $value2)) {
+                               return 0;
+                       }
+                       else {
+                               return 1;
+                       }
+               }
+               else if (in_array('all', $value2)) {
+                       return -1;
+               }
+               
+               // check if value1 contains more BBCodes than value2
+               $diff = array_diff($value1, $value2);
+               if (!empty($diff)) {
+                       return 1;
+               }
+               
+               // check if value1 contains less BBCodes than value2
+               $diff = array_diff($value2, $value1);
+               if (!empty($diff)) {
+                       return -1;
+               }
+               
+               // both lists of BBCodes are equal
+               return 0;
+       }
 }
index a9e455cf29462c75618151ec1707d41db4893b6f..9205d748357a7c6a168cc49b5a70cf0c4fb16f0b 100644 (file)
@@ -30,4 +30,22 @@ class InfiniteIntegerUserGroupOptionType extends IntegerUserGroupOptionType {
                        return parent::merge($defaultValue, $groupValue);
                }
        }
+       
+       /**
+        * @see \wcf\system\option\IOptionType::compare()
+        */
+       public function compare($value1, $value2) {
+               if ($value1 == $value2) {
+                       return 0;
+               }
+               
+               if ($value1 == -1) {
+                       return 1;
+               }
+               else if ($value2 == -1) {
+                       return -1;
+               }
+               
+               return parent::compare($value1, $value2);
+       }
 }
index 8f7742dc4174284909e7efdc184817d4467792d3..a6e615b84cdc111d6560464f2a5649637b105639 100644 (file)
@@ -29,4 +29,22 @@ class InfiniteInverseIntegerUserGroupOptionType extends InverseIntegerUserGroupO
                
                return min($defaultValue, $groupValue);
        }
+       
+       /**
+        * @see \wcf\system\option\IOptionType::compare()
+        */
+       public function compare($value1, $value2) {
+               if ($value1 == $value2) {
+                       return 0;
+               }
+               
+               if ($value1 == -1) {
+                       return 1;
+               }
+               else if ($value2 == -1) {
+                       return -1;
+               }
+               
+               return ($value1 < $value2) ? 1 : -1;
+       }
 }
index 42e89e82ba932bb3e343bbb22a69e8bdcdbbb6dd..2ac21bd667ec2d4cbfc7a9976eb2dd7c51bf4228 100644 (file)
@@ -25,4 +25,15 @@ class InverseIntegerUserGroupOptionType extends IntegerOptionType implements IUs
                
                return $groupValue;
        }
+       
+       /**
+        * @see \wcf\system\option\IOptionType::compare()
+        */
+       public function compare($value1, $value2) {
+               if ($value1 == $value2) {
+                       return 0;
+               }
+               
+               return ($value1 < $value2) ? 1 : -1;
+       }
 }
index a9da44301b16e007803299a427b6fdc9d6a7b4fe..2c0843c803c0c8a94da340d67f453786dd08b7c9 100644 (file)
@@ -75,4 +75,27 @@ class UserGroupsUserGroupOptionType extends AbstractOptionType implements IUserG
                
                return implode(',', array_unique(array_merge($defaultValue, $groupValue)));
        }
+       
+       /**
+        * @see \wcf\system\option\IOptionType::compare()
+        */
+       public function compare($value1, $value2) {
+               $value1 = explode(',', $value1);
+               $value2 = explode(',', $value2);
+               
+               // check if value1 contains more elements than value2
+               $diff = array_diff($value1, $value2);
+               if (!empty($diff)) {
+                       return 1;
+               }
+               
+               // check if value1 contains less elements than value2
+               $diff = array_diff($value2, $value1);
+               if (!empty($diff)) {
+                       return -1;
+               }
+               
+               // both lists are equal
+               return 0;
+       }
 }