| <?php |
| |
| //============================================================= |
| //=== Class: Settings Bitwise |
| //=== Dependancies: none |
| //=== Author: VBAssassin ( www.coderprofile.com/coder/VBAssassin ) |
| //=== Version: 1.00.00 |
| //=== PHP version: 5.X |
| //=== License: GNU |
| //=== Copyright: 2009, Scott Thompson, All Rights Reserved |
| //=== Description: Specify an array of settings, and depending on the combination of settings will return a unique integer. This integer can then be used to workout the settings that were selected to produce that integer. |
| //=== Use Cases: A settings page for a users profile, or anywhere else where basic settings can be used such as "select the mailing lists you are interested in"... |
| //============================================================= |
| class settings_bitwise { |
| |
| //============================================================= |
| //=== store the lookup table that will be bitwised against |
| //============================================================= |
| private $lookup_table = array(); |
| |
| //============================================================= |
| //=== set the lookup table |
| //============================================================= |
| public function set_lookup_table(array $table) { |
| $this->lookup_table = $table; |
| } |
| |
| //============================================================= |
| //=== returns all possible numbers that could include the spefic setting |
| //============================================================= |
| public function get_all_combinations_of($specific_setting_value) { |
| |
| //contains a list of all possible integers that will contain those settings |
| $possible_values = array(); |
| |
| //maximum possible value |
| $max_value = 0; |
| foreach ($this->lookup_table as $number) { |
| $max_value += $number; |
| } |
| |
| //scan through every number and see which contain those specific bits |
| for ($counter = $specific_setting_value; $counter <= $max_value; $counter++) { |
| //when does this number contain these settings? |
| if ($specific_setting_value == ($specific_setting_value & $counter)) { |
| $possible_values[] = $counter; |
| } |
| } |
| |
| //send back an array of the possible values |
| return $possible_values; |
| |
| } |
| //============================================================= |
| //=== get the settings that are enabled/disabled by comparing the settings table with the integer $setting_value |
| //============================================================= |
| public function get_settings($setting_value) { |
| |
| //get a copy of the settings lookup table |
| $applied_settings = $this->lookup_table; |
| |
| //loop through the lookup table to see which settings are active |
| foreach ($this->lookup_table as $current_setting=>$value) { |
| |
| //check if the setting is enabled or not |
| if ($setting_value & $value) { |
| $applied_settings[$current_setting] = true; |
| } else { |
| $applied_settings[$current_setting] = false; |
| } |
| |
| } |
| |
| //return the new settings table, bit this time with true/false for settings that were enabled/disabled |
| return $applied_settings; |
| |
| } |
| |
| } |
| |
| //============================================================= |
| //=== Examples of how to use the class |
| //============================================================= |
| |
| //lets create an instance of the bitwise settings |
| $settings = new settings_bitwise(); |
| |
| //now we have 4 settings, each setting must stay in the same order, and each setting is given a value that is in par with the binary system |
| $settings->set_lookup_table( |
| array( |
| 'show_name' => 1, //0001 = 1 |
| 'show_age' => 2, //0010 = 2 |
| 'show_gender' => 4, //0100 = 4 |
| 'show_partner' => 8 //1000 = 8 |
| ) |
| ); |
| |
| //now the user has selected everything, so add up the numbers: 1 + 2 + 4 + 8 = 15. Store 15 in the database, since thats what the users settings are... and is used below to regenerate the same settings. |
| print_r($settings->get_settings(15)); |
| /* PRINTS |
| Array |
| ( |
| [show_name] => true |
| [show_age] => true |
| [show_gender] => true |
| [show_partner] => true |
| ) |
| */ |
| |
| //now the user has selected everything, so add up the numbers: 2 + 4 = 6. Store 6 in the database, since thats what the users settings are... and is used below to regenerate the same settings. |
| print_r($settings->get_settings(6)); |
| /* PRINTS |
| Array |
| ( |
| [show_name] => false |
| [show_age] => true |
| [show_gender] => true |
| [show_partner] => false |
| ) |
| */ |
| |
| //now the user has selected everything, so add up the numbers: 2 + 8 = 10. Store 0 in the database, since thats what the users settings are... and is used below to regenerate the same settings. |
| print_r($settings->get_settings(10)); |
| /* PRINTS |
| Array |
| ( |
| [show_name] => false |
| [show_age] => true |
| [show_gender] => false |
| [show_partner] => true |
| ) |
| */ |
| |
| //Now lets say we wanted to get every user out of a database that has a specific group of settings turned to on... then you use this to get every number that has those settings on :-) very usefull for a database search. The xample below returns every number that has the setting that corresponds to 4 in it. |
| |
| print_r($settings->get_all_combinations_of(4)); |
| /* PRINTS |
| Array |
| ( |
| [0] => 4 |
| [1] => 5 |
| [2] => 6 |
| [3] => 7 |
| [4] => 12 |
| [5] => 13 |
| [6] => 14 |
| [7] => 15 |
| ) |
| */ |
| ?> |