|
1 | 1 | php-bitmap
|
2 | 2 | ==========
|
3 | 3 |
|
4 |
| -Operations with bitmap |
| 4 | +Bitmap, also called bit array is a data structure that |
| 5 | +compactly store set of values as bits of integer. |
| 6 | +More data can be read at http://en.wikipedia.org/wiki/Bit_array. |
| 7 | + |
| 8 | +It is useful when required compact way to represent combination |
| 9 | +of values and simple manipulations with them. One byte can |
| 10 | +represent eight independent values. |
| 11 | + |
| 12 | +lets see example. Errors in PHP represents as constants: |
| 13 | + |
| 14 | +``` |
| 15 | +E_ERROR = 1 (0); |
| 16 | +E_WARNING = 2 (1); |
| 17 | +E_PARSE = 4 (2); |
| 18 | +E_NOTICE = 8 (3); |
| 19 | +E_CORE_ERROR = 16 (4); |
| 20 | +E_CORE_WARNING = 32 (5); |
| 21 | +E_COMPILE_ERROR = 64 (6); |
| 22 | +E_COMPILE_WARNING = 128 (7); |
| 23 | +E_USER_ERROR = 256 (8); |
| 24 | +E_USER_WARNING = 512 (9); |
| 25 | +E_USER_NOTICE = 1024 (10); |
| 26 | +E_STRICT = 2048 (11); |
| 27 | +E_RECOVERABLE_ERROR = 4096 (12); |
| 28 | +E_DEPRECATED = 8192 (13); |
| 29 | +E_USER_DEPRECATED = 16384 (14); |
| 30 | +E_ALL = 32767 (15); |
| 31 | +``` |
| 32 | + |
| 33 | +Every error level represent logical "1", and combination of all this |
| 34 | +values may be represent only by two bytes. E_ERROR represent first bit of byte, |
| 35 | +E_WARNING - second, etc. |
| 36 | + |
| 37 | +Combination of E_WARNING and E_NOTICE in binary system is "1010" or 10 in decimal system. |
| 38 | + |
| 39 | +Class that represents bitmap of PHP errors: |
| 40 | + |
| 41 | +```php |
| 42 | +class PhpError extends \Sokil\Bitmap |
| 43 | +{ |
| 44 | + /** |
| 45 | + * Show errors |
| 46 | + * Set first bit, which represents E_ERROR, to "1" |
| 47 | + */ |
| 48 | + public function showErrors() |
| 49 | + { |
| 50 | + $this->setBit(0); |
| 51 | + return $this; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Hide errors |
| 56 | + * Set first bit, which represents E_ERROR, to "0" |
| 57 | + */ |
| 58 | + public function showErrors() |
| 59 | + { |
| 60 | + $this->unsetBit(0); |
| 61 | + return $this; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Check if errors shown |
| 66 | + * Check if first bit set to 1 |
| 67 | + */ |
| 68 | + public function isErrorsShown() |
| 69 | + { |
| 70 | + return $this->isBitSet(0); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Show warnings |
| 75 | + * Set second bit, which represents E_WARNING, to "1" |
| 76 | + */ |
| 77 | + public function showWarnings() |
| 78 | + { |
| 79 | + $this->setBit(1); |
| 80 | + return $this; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Hide warnings |
| 85 | + * Set second bit, which represents E_WARNING, to "0" |
| 86 | + */ |
| 87 | + public function showWarnings() |
| 88 | + { |
| 89 | + $this->unsetBit(1); |
| 90 | + return $this; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Check if warnings shown |
| 95 | + * Check if second bit set to 1 |
| 96 | + */ |
| 97 | + public function isWarningsShown() |
| 98 | + { |
| 99 | + return $this->isBitSet(1); |
| 100 | + } |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +Now we can easely manipulate with errors: |
| 105 | +```php |
| 106 | + |
| 107 | +// load current error levels |
| 108 | +$error = new PhpError(error_reporting()); |
| 109 | + |
| 110 | +// enable errors and warnings |
| 111 | +$error->showErrors()->showWarnings(); |
| 112 | + |
| 113 | +// set error reporting |
| 114 | +error_reporting($error->toInt()); |
| 115 | + |
| 116 | +// check if warnings shown |
| 117 | +var_dump($error->isWarningsShown)); |
| 118 | + |
| 119 | +// value may be set by mask |
| 120 | +// E_USER_ERROR | E_USER_WARNING is 256 + 512; |
| 121 | +$error->setBitsByMask(E_USER_ERROR + E_USER_WARNING); |
| 122 | +``` |
0 commit comments