class-thelib-array.php
9 years ago
class-thelib-core.php
9 years ago
class-thelib-debug.php
9 years ago
class-thelib-html.php
9 years ago
class-thelib-net.php
9 years ago
class-thelib-session.php
9 years ago
class-thelib-ui.php
9 years ago
class-thelib-updates.php
9 years ago
class-thelib.php
9 years ago
class-thelib-core.php
221 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Main access to the Code-Library. |
| 4 | * Access via function `lib3()`. |
| 5 | * |
| 6 | * Inspired by Jigsaw plugin by Jared Novack (http://jigsaw.upstatement.com/) |
| 7 | * |
| 8 | * @since 1.0.0 |
| 9 | */ |
| 10 | class TheLib_Core extends TheLib { |
| 11 | |
| 12 | /** |
| 13 | * Interface to the array component. |
| 14 | * |
| 15 | * @since 1.1.5 |
| 16 | * @api |
| 17 | * |
| 18 | * @var TheLib_Array |
| 19 | */ |
| 20 | public $array = null; |
| 21 | |
| 22 | /** |
| 23 | * Interface to the Debug component. |
| 24 | * |
| 25 | * @since 1.1.0 |
| 26 | * @api |
| 27 | * |
| 28 | * @var TheLib_Debug |
| 29 | */ |
| 30 | public $debug = null; |
| 31 | |
| 32 | /** |
| 33 | * Interface to the HTML component. |
| 34 | * |
| 35 | * @since 1.1.0 |
| 36 | * @api |
| 37 | * |
| 38 | * @var TheLib_Html |
| 39 | */ |
| 40 | public $html = null; |
| 41 | |
| 42 | /** |
| 43 | * Interface to the Net component. |
| 44 | * |
| 45 | * @since 1.1.0 |
| 46 | * @api |
| 47 | * |
| 48 | * @var TheLib_Net |
| 49 | */ |
| 50 | public $net = null; |
| 51 | |
| 52 | /** |
| 53 | * Interface to the session component. |
| 54 | * |
| 55 | * @since 1.1.5 |
| 56 | * @api |
| 57 | * |
| 58 | * @var TheLib_Session |
| 59 | */ |
| 60 | public $session = null; |
| 61 | |
| 62 | /** |
| 63 | * Interface to the updates component. |
| 64 | * |
| 65 | * @since 1.1.5 |
| 66 | * @api |
| 67 | * |
| 68 | * @var TheLib_Updates |
| 69 | */ |
| 70 | public $updates = null; |
| 71 | |
| 72 | /** |
| 73 | * Interface to the UI component. |
| 74 | * |
| 75 | * @since 1.1.5 |
| 76 | * @api |
| 77 | * |
| 78 | * @var TheLib_Ui |
| 79 | */ |
| 80 | public $ui = null; |
| 81 | |
| 82 | /** |
| 83 | * Class constructor |
| 84 | * |
| 85 | * @since 1.0.0 |
| 86 | * @internal |
| 87 | */ |
| 88 | public function __construct() { |
| 89 | parent::__construct(); |
| 90 | |
| 91 | self::$core = $this; |
| 92 | |
| 93 | // A List of all components. |
| 94 | $components = array( |
| 95 | 'array', |
| 96 | 'debug', |
| 97 | 'html', |
| 98 | 'net', |
| 99 | 'session', |
| 100 | 'updates', |
| 101 | 'ui', |
| 102 | ); |
| 103 | |
| 104 | // Create instances of each component. |
| 105 | foreach ( $components as $component ) { |
| 106 | if ( ! property_exists( $this, $component ) ) { continue; } |
| 107 | |
| 108 | $class_name = 'TheLib_' . ucfirst( $component ); |
| 109 | $this->$component = new $class_name(); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Checks if the provided value evaluates to a boolean TRUE. |
| 115 | * |
| 116 | * Following values are considered true: |
| 117 | * - Boolean: true |
| 118 | * - Number: anything except 0 |
| 119 | * - Strings: true, yes, on (case insensitive) |
| 120 | * |
| 121 | * @since 1.1.0 |
| 122 | * @api |
| 123 | * |
| 124 | * @param mixed $value A value that will be evaluated as a boolean. |
| 125 | * @return bool True if the specified $value evaluated to TRUE. |
| 126 | */ |
| 127 | public function is_true( $value ) { |
| 128 | if ( false === $value || null === $value || '' === $value ) { |
| 129 | return false; |
| 130 | } elseif ( true === $value ) { |
| 131 | return true; |
| 132 | } elseif ( is_numeric( $value ) ) { |
| 133 | $value = intval( $value ); |
| 134 | return $value != 0; |
| 135 | } elseif ( is_string( $value ) ) { |
| 136 | $value = strtolower( trim( $value ) ); |
| 137 | return in_array( |
| 138 | $value, |
| 139 | array( 'true', 'yes', 'on', '1' ) |
| 140 | ); |
| 141 | } |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Opposite of the is_true() function. |
| 147 | * |
| 148 | * @since 3.0.0 |
| 149 | * @param mixed $value A value that will be evaluated as a boolean |
| 150 | * @return bool True if the speciefied value evals as FALSE |
| 151 | */ |
| 152 | public function is_false( $value ) { |
| 153 | return ! $this->is_true( $value ); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Converts a number from any base to another base. |
| 158 | * The from/to base values can even be non-numeric values. |
| 159 | * |
| 160 | * @since 2.0.2 |
| 161 | * @api |
| 162 | * |
| 163 | * @param string $number A number in the base_from base. |
| 164 | * @param string $base_from List of characters |
| 165 | * E.g. 0123456789 to convert from decimal. |
| 166 | * @param string $base_to List of characters to use as destination base. |
| 167 | * E.g. 0123456789ABCDEF to convert to hexadecimal. |
| 168 | * @return string The converted number |
| 169 | */ |
| 170 | public function convert( $number, $base_from = '0123456789', $base_to = '0123456789ABCDEF' ) { |
| 171 | if ( $base_from == $base_to ) { |
| 172 | // No conversion needed. |
| 173 | return $number; |
| 174 | } |
| 175 | |
| 176 | $retval = ''; |
| 177 | $number_len = strlen( $number ); |
| 178 | |
| 179 | if ( '0123456789' == $base_to ) { |
| 180 | // Convert a value to normal decimal base. |
| 181 | |
| 182 | $arr_base_from = str_split( $base_from, 1 ); |
| 183 | $arr_number = str_split( $number, 1 ); |
| 184 | $base_from_len = strlen( $base_from ); |
| 185 | $retval = 0; |
| 186 | for ( $i = 1; $i <= $number_len; $i += 1 ) { |
| 187 | $retval = bcadd( |
| 188 | $retval, |
| 189 | bcmul( |
| 190 | array_search( $arr_number[$i - 1], $arr_base_from ), |
| 191 | bcpow( $base_from_len, $number_len - $i ) |
| 192 | ) |
| 193 | ); |
| 194 | } |
| 195 | } else { |
| 196 | // Convert a value to a NON-decimal base. |
| 197 | |
| 198 | if ( '0123456789' != $base_from ) { |
| 199 | // Base value is non-decimal, convert it to decimal first. |
| 200 | $base10 = $this->convert( $number, $base_from, '0123456789' ); |
| 201 | } else { |
| 202 | // Base value is decimal. |
| 203 | $base10 = $number; |
| 204 | } |
| 205 | |
| 206 | $arr_base_to = str_split( $base_to, 1 ); |
| 207 | $base_to_len = strlen( $base_to ); |
| 208 | if ( $base10 < strlen( $base_to ) ) { |
| 209 | $retval = $arr_base_to[$base10]; |
| 210 | } else { |
| 211 | while ( 0 != $base10 ) { |
| 212 | $retval = $arr_base_to[bcmod( $base10, $base_to_len )] . $retval; |
| 213 | $base10 = bcdiv( $base10, $base_to_len, 0 ); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | return $retval; |
| 219 | } |
| 220 | |
| 221 | } |