├── .gitignore ├── README.md ├── RFC.txt ├── composer.json ├── examples ├── arrays.php ├── bootstrap.php ├── numerics.php └── strings.php ├── phpunit.xml.dist ├── src ├── ArrayHandler.php ├── BooleanHandler.php ├── FloatHandler.php ├── IntegerHandler.php ├── NullHandler.php ├── NumberHandler.php ├── ScalarObjectHandler.php └── StringHandler.php └── tests ├── ArrayHandlerTest.php ├── BaseHandlerTest.php ├── BooleanHandlerTest.php ├── FloatHandlerTest.php ├── IntegerHandlerTest.php ├── NullHandlerTest.php ├── StringHandlerTest.php └── bootstrap.php /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_* 2 | vendor/ 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | PHP Scalar Object Handlers 2 | =========================== 3 | 4 | ## Wouldn't you prefer to write PHP like this? 5 | 6 | ```php 7 | $n = 12.5; 8 | echo $n->floor()->toString()->length(); // 2 9 | ``` 10 | 11 | Or deal with strings, without remembering the parameter order? 12 | 13 | ```php 14 | $str = "Hello World"; 15 | echo $str->replace("World","PHP"); // Hello PHP 16 | 17 | $str = "Hello World"; 18 | echo $str->length(); // 11 19 | ``` 20 | 21 | Or chain array operations, with a simpler syntax API? 22 | 23 | ```php 24 | $arr = ["k1"=>"val1","k2"=>"val2"]; 25 | print_r($arr->merge(["k3"=>"val3","k4"=>"val4"])->count()) // 4 26 | 27 | $array = [1,2,3,4,5]; 28 | $array->each(function(&$value) { 29 | $value *= 2; 30 | }); // [2,4,6,8,10] 31 | ``` 32 | 33 | PHP6 Gives us a chance to create a new API to streamline the language in a way that ensures all old code still runs in the same way. 34 | Here's how to see this in action right now! 35 | 36 | 37 | ### An Experimental Proposal for PHP Scalar Objects 38 | 39 | Based on the fantastic extension by nikic https://github.com/nikic/scalar_objects this project is designed to start the ball rolling on the future API for Scalar objects in PHP. 40 | 41 | Discussions, arguments and pull requests are welcome. Once we get some consensus, we can send a pull request up to nikic and hopefully get some discussion going on the internals list. Let's make PHP6 Awesome. 42 | 43 | Note, the handlers won't actually be implemented in PHP, but the handlers and methods can be built natively once the API is agreed. This is just a way to get contributions from the wider PHP community without the need to know the internals. 44 | 45 | #### Getting started 46 | 47 | The handlers are written in PHP so, you don't need to install the extension to contribute, however full instructions for getting started are on the extension readme file at: https://github.com/nikic/scalar_objects 48 | 49 | #### Supported types 50 | 51 | Object handlers are supported for `string`, `integer`, `array`, `float`, `null` and `resource` 52 | 53 | The handlers are PSR-4 loaded under the namespace Spl\Scalars in the `src` directory. 54 | 55 | ### Arrays 56 | 57 | Syntax available is as follows. 58 | 59 | #### chunk(chunk size) 60 | ```php 61 | $array = ["first", "second", "third", "fourth", "fifth"]; 62 | $array->chunk(2); // [["first","second"],["third","fourth"],["fifth"]] 63 | ``` 64 | 65 | #### column(column key) 66 | ```php 67 | $array = [["key1"=>"value","key2"=>"value2"],["key1"=>"value","key2"=>"value2"]]; 68 | $array->column("key2"); // ["value2","value2"] 69 | ``` 70 | 71 | #### combine(Array) 72 | ```php 73 | $array = ["k1","k2","k3","k4"]; 74 | $array2 = ["val","val2","val3","val4"]; 75 | $array->combine($array2); // ["k1"=>"val","k2"=>"val2","k3"=>"val3","k4"=>"val4"] 76 | ``` 77 | 78 | #### count() 79 | ```php 80 | $array = ["k1"=>"val","k2"=>"val2"]; 81 | $array->count(); // 2 82 | ``` 83 | 84 | #### diff(Array) 85 | ```php 86 | $array = ["k1"=>"val","k2"=>"val2"]; 87 | $array2 = ["k2"=>"val2","k3"=>"val3"]; 88 | $array->diff($array2); // ["k1" => "val"] 89 | ``` 90 | 91 | #### hasKey(keyname) 92 | ```php 93 | $array = ["k1"=>"val","k2"=>"val2"]; 94 | $array->hasKey("k2"); // true 95 | ``` 96 | 97 | #### keys() 98 | ```php 99 | $array = ["k1"=>"val","k2"=>"val2"]; 100 | $array->keys(); // ["k1","k2"] 101 | ``` 102 | 103 | #### merge(Array) 104 | ```php 105 | $array = ["k1"=>"val","k2"=>"val2"]; 106 | $array2 = ["k2"=>"val2","k3"=>"val3"]; 107 | $array->merge($array2); // ["k1"=>"val","k2"=>"val2","k3"=>"val3"] 108 | ``` 109 | 110 | #### push(value) 111 | ```php 112 | $array = ["k1"=>"val","k2"=>"val2"]; 113 | $array->push("val3"); // ["k1"=>"val","k2"=>"val2",0=>"val3"] 114 | ``` 115 | 116 | #### rand() 117 | ```php 118 | $array = ["k1"=>"val","k2"=>"val2","k3"=>"val3"]; 119 | $array->rand(); // "val3" 120 | ``` 121 | 122 | #### reverse() 123 | ```php 124 | $array = ["k1"=>"val","k2"=>"val2","k3"=>"val3"]; 125 | $array->reverse(); // ["k3"=>"va3","k2"=>"val2","k1"=>"val"] 126 | ``` 127 | 128 | #### reindex(null | function($row)) 129 | ```php 130 | $arr = [ 131 | ["id"=>5, "val"=>"first"],["id"=>6, "val"=>"second"],["id"=>7, "val"=>"third"] 132 | ]; 133 | $array->reindex(); // [0=>["id"=>5, "val"=>"first"],1=>["id"=>6, "val"=>"second"],2=>["id"=>7, "val"=>"third"]] 134 | $array->reindex(function($row){return $row["id"]}); // [5=>["id"=>5, "val"=>"first"],6=>["id"=>6, "val"=>"second"],7=>["id"=>7, "val"=>"third"]] 135 | ``` 136 | 137 | #### toJSON() 138 | ```php 139 | $array = ["k1"=>"val","k2"=>"val2","k3"=>"val3"]; 140 | $array->toJSON(); // {"k1":"val","k2":"val2","k3":"val3"} 141 | ``` 142 | 143 | ### Strings 144 | 145 | Syntax available is as follows. 146 | 147 | #### length() 148 | ```php 149 | $str = "Hello World"; 150 | $str->length(); // 11 151 | ``` 152 | 153 | #### slice() 154 | ```php 155 | $str = "Hello World"; 156 | $str->slice(0,4); // Hell 157 | ``` 158 | 159 | #### contains() 160 | ```php 161 | $str = "Hello World"; 162 | $str->contains("Hell"); // true 163 | ``` 164 | 165 | #### startsWith() 166 | ```php 167 | $str = "Hello World"; 168 | $str->startsWith("Help"); // false 169 | ``` 170 | 171 | #### endsWith() 172 | ```php 173 | $str = "Hello World"; 174 | $str->endsWith("rld"); // true 175 | ``` 176 | 177 | 178 | #### split() 179 | ```php 180 | $str = "Hello World"; 181 | $str->split(" "); // ["Hello", "World"] 182 | ``` 183 | 184 | 185 | #### repeat() 186 | ```php 187 | $str = "Hello World"; 188 | $str->repeat(3); // "Hello WorldHello WorldHello World" 189 | ``` 190 | 191 | 192 | #### reverse() 193 | ```php 194 | $str = "Hello World"; 195 | $str->reverse(); // "dlroW olleH"; 196 | ``` 197 | 198 | 199 | #### lower() 200 | ```php 201 | $str = "Hello World"; 202 | $str->lower(); // "hello world" 203 | ``` 204 | 205 | 206 | #### upper() 207 | ```php 208 | $str = "Hello World"; 209 | $str->upper(); // "HELLO WORLD" 210 | ``` 211 | 212 | 213 | #### toArray() 214 | ```php 215 | $str = "Hello World"; 216 | $str->toArray(); // [0=>"Hello World"] 217 | ``` 218 | 219 | 220 | ### Floats / Integers 221 | 222 | #### abs() 223 | ```php 224 | $n = -25; 225 | $n->abs(); // 25 226 | ``` 227 | 228 | #### ceil() 229 | ```php 230 | $n = 12.5; 231 | $n->ceil(); // 13 232 | ``` 233 | 234 | #### floor() 235 | ```php 236 | $n = 12.5; 237 | $n->floor(); // 12 238 | ``` 239 | 240 | #### toInt() 241 | ```php 242 | $n = 12.5; 243 | $n->toInt(); // 12 244 | ``` 245 | 246 | 247 | ### Integers 248 | 249 | 250 | #### even() 251 | ```php 252 | $n = 10; 253 | $n->even(); // true 254 | ``` 255 | 256 | #### odd() 257 | ```php 258 | $n = 10; 259 | $n->odd(); // false 260 | ``` 261 | 262 | 263 | ### Nulls 264 | 265 | Syntax available is as follows. 266 | 267 | ```php 268 | $a = null; 269 | $a->toJSON(); // null 270 | ``` 271 | -------------------------------------------------------------------------------- /RFC.txt: -------------------------------------------------------------------------------- 1 | 2 | ====== PHP RFC: Scalar Object Handlers ====== 3 | * Version: 0.1 4 | * Date: 2014-01-23 5 | * Authors: Ross Riley, ...., .... 6 | * Status: Draft 7 | * First Published at: http://wiki.php.net/rfc/scalar_object_handlers 8 | 9 | This is a proposal to introduce scalar object support to the next major version of PHP. 10 | 11 | Based on the concept introduced by nikic and built on top of the Scalar Object PHP extension. 12 | 13 | ===== Introduction ===== 14 | 15 | This proposal offers an alternative Object-Oriented way to interact with PHP scalar type. 16 | 17 | Each type is coerced into a Scalar Object and a Handler class defines the API for interacting with each type. 18 | 19 | Support is currently proposed for: 20 | 21 | *Arrays* 22 | $array = [1,2,3,4,5]; 23 | $array->count(); // 4 24 | 25 | *Booleans* 26 | $bool = true; 27 | $bool->isBool(); // true 28 | 29 | *Floats* 30 | $float = 123.456; 31 | $float->toInt(); // 123 32 | 33 | *Integers* 34 | $int = -100; 35 | $int->abs(); // 100 36 | 37 | *Null* 38 | $n = null; 39 | $n->isNull(); // true 40 | 41 | *Strings* 42 | $str = "Hello World"; 43 | $str->length(); // 11 44 | $str->upper(); // "HELLO WORLD" 45 | 46 | 47 | 48 | ===== Proposal ===== 49 | 50 | Features and examples of the proposal go here. 51 | 52 | ===== Backward Incompatible Changes ===== 53 | 54 | No backwards incompatible functionality is intended since all of the features represent new language functionality. 55 | 56 | 57 | ===== Proposed PHP Version(s) ===== 58 | 59 | Next major version, probably PHP 6.0 60 | 61 | ===== SAPIs Impacted ===== 62 | 63 | Describe the impact to CLI, Development web server, embedded PHP etc. 64 | 65 | ===== Impact to Existing Extensions ===== 66 | 67 | No known impact on existing extensions. 68 | 69 | ===== New Constants ===== 70 | 71 | No constants will be created in the global space. 72 | 73 | ===== php.ini Defaults ===== 74 | 75 | No new ini settings will need to be created. 76 | 77 | ===== Open Issues ===== 78 | 79 | Make sure there are no open issues when the vote starts! 80 | 81 | ===== Unaffected PHP Functionality ===== 82 | 83 | List existing areas/features of PHP that will not be changed by the RFC. 84 | 85 | This helps avoid any ambiguity, shows that you have thought deeply about the RFC's impact, and helps reduces mail list noise. 86 | 87 | ===== Future Scope ===== 88 | 89 | This sections details areas where the feature might be improved in future, but that are not currently proposed in this RFC. 90 | 91 | ===== Proposed Voting Choices ===== 92 | 93 | Include these so readers know where you are heading and can discuss the proposed voting options. 94 | 95 | ===== Patches and Tests ===== 96 | 97 | [[https://github.com/rossriley/php-scalar-objects|Project on Github]] 98 | 99 | This patch is intended to be a prototype. 100 | 101 | Work will commence on a native patch once the API has been carefully analysed tested and assuming there is general agreement from the internals list to explore further. 102 | 103 | ===== Implementation ===== 104 | 105 | After the project is implemented, this section should contain 106 | - the version(s) it was merged to 107 | - a link to the git commit(s) 108 | - a link to the PHP manual entry for the feature 109 | 110 | ===== References ===== 111 | 112 | WIP project: https://github.com/rossriley/php-scalar-objects 113 | The Scalar Objects Extension: https://github.com/nikic/scalar_objects 114 | 115 | 116 | ===== Rejected Features ===== 117 | 118 | Keep this updated with features that were discussed on the mail lists. 119 | 120 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rossriley/phpscalars", 3 | "type": "library", 4 | "description": "PHP Handlers for experimental Scalar Object Handlers", 5 | "license": "MIT", 6 | "authors": [ 7 | {"name": "Ross Riley","email": "riley.ross@gmail.com"} 8 | ], 9 | "require": { 10 | "php": ">=5.4.0" 11 | }, 12 | 13 | "autoload": { 14 | "psr-4": { 15 | "Spl\\Scalars\\": "src", 16 | "Spl\\Scalars\\Tests\\": "tests" 17 | } 18 | } 19 | 20 | 21 | 22 | 23 | } 24 | -------------------------------------------------------------------------------- /examples/arrays.php: -------------------------------------------------------------------------------- 1 | "first", 2=>"second", 3=>"third", 4=>"fourth", 5=>"fifth"]; 6 | showCommand("chunk", '$arr = [1=>"first", 2=>"second", 3=>"third", 4=>"fourth", 5=>"fifth"]', '$arr->chunk(2)', $arr->chunk(2)); 7 | 8 | $arr = [["key1"=>"value","key2"=>"value2"],["key1"=>"value","key2"=>"value2"]]; 9 | showCommand("column",'$arr = [["key1"=>"value","key2"=>"value2"],["key1"=>"value","key2"=>"value2"]]', '$arr->column("key2")', $arr->column("key2")); 10 | 11 | 12 | $array = ["k1"=>"val","k2"=>"val2"]; 13 | $array2 = ["k2"=>"val2","k3"=>"val3"]; 14 | showCommand("diff",'$array, $array2', '$array->diff($array2)', $array->diff($array2)); 15 | 16 | 17 | $array = ["k1"=>"val","k2"=>"val2"]; 18 | showCommand("hasKey",'$array', '$array->hasKey("k2")', $array->hasKey("k2")); 19 | 20 | 21 | $array = ["k1"=>"val","k2"=>"val2"]; 22 | showCommand("push",'$array', '$array->push("val3")', $array->push("val3")); 23 | 24 | 25 | $array = ["k1"=>"val","k2"=>"val2","k3"=>"val3"]; 26 | showCommand("rand",'', '$array->rand()', $array->rand()); 27 | 28 | $array = ["k1"=>"val","k2"=>"val2","k3"=>"val3"]; 29 | showCommand("reverse",'', '$array->reverse()', $array->reverse()); 30 | 31 | $array = ["k1"=>"val","k2"=>"val2","k3"=>"val3"]; 32 | showCommand("toJSON",'', '$array->toJSON()', $array->toJSON()); -------------------------------------------------------------------------------- /examples/bootstrap.php: -------------------------------------------------------------------------------- 1 | add(4)', $num->add(4)); 7 | 8 | $num = 12.5; 9 | showCommand("add", '$num', '$num->add(4.2)', $num->add(4.2)); 10 | 11 | $num = -25; 12 | showCommand("add", '$num', '$num->abs()', $num->abs()); 13 | 14 | 15 | $num = 12.5; 16 | showCommand("toInt", '$num = 12.5', '$num->toInt()', $num->toInt()); 17 | 18 | $num = 12.5; 19 | showCommand("toJSON", '$num = 12.5', '$num->toJSON()', $num->toJSON()); -------------------------------------------------------------------------------- /examples/strings.php: -------------------------------------------------------------------------------- 1 | length()', $str->length()); 7 | showCommand("slice", '$str = "Hello World"', '$str->slice(0,3)', $str->slice(0,3)); 8 | showCommand("split", '$str = "Hello World"', '$str->split(" ")', $str->split(" ")); 9 | showCommand("toArray", '$str = "Hello World"', '$str->toArray()', $str->toArray()); 10 | showCommand("toJSON", '$str = "Hello World"', '$str->toJSON()', $str->toJSON()); -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 8 | 9 | 10 | 11 | tests/ 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /src/ArrayHandler.php: -------------------------------------------------------------------------------- 1 | verifyInteger($size, "chunk"); 40 | return array_chunk($this, $size); 41 | } 42 | 43 | public function column($key) 44 | { 45 | $this->verifyString($key, "column"); 46 | return array_column($this, $key); 47 | } 48 | 49 | public function combine($arrayVals) 50 | { 51 | $this->verifyArray($arrayVals, "combine"); 52 | return array_combine($this,$arrayVals); 53 | } 54 | 55 | public function count() { 56 | return count($this); 57 | } 58 | 59 | public function countValues() 60 | { 61 | return array_count_values($this); 62 | } 63 | 64 | public function diff($array) 65 | { 66 | $this->verifyArray($array, "diff"); 67 | return array_diff($this, $array); 68 | } 69 | 70 | public function difference($array) 71 | { 72 | $this->verifyArray($array, "diff"); 73 | return array_diff($this, $array); 74 | } 75 | 76 | public function each($callback) 77 | { 78 | $this->verifyCallable($callback); 79 | array_walk_recursive($this, $callback); 80 | return $this; 81 | } 82 | 83 | public function filter($callback) 84 | { 85 | $this->verifyCallable($callback); 86 | return array_filter($this, $callback); 87 | } 88 | 89 | public function has($value) 90 | { 91 | return in_array($value, $this, true); 92 | } 93 | 94 | public function hasKey($key) 95 | { 96 | $this->verifyString($key, "hasKey"); 97 | return array_key_exists($key, $this); 98 | } 99 | 100 | public function indexOf($value) 101 | { 102 | return array_search($value, $this); 103 | } 104 | 105 | public function intersect($array) 106 | { 107 | $this->verifyArray($array, "intersect"); 108 | return array_intersect($this, $array); 109 | } 110 | 111 | public function intersperse($value) 112 | { 113 | $array = $this; 114 | $chunk = array_chunk($array, 1); 115 | 116 | $intersperser = function(&$row) {$row[1]="lalal";}; 117 | foreach($chunk as &$row) { 118 | $row[1] = $value; 119 | } 120 | $result = call_user_func_array('array_merge', $chunk); 121 | array_pop($result); 122 | return $result; 123 | } 124 | 125 | public function join($on="") 126 | { 127 | return implode($on, $this); 128 | } 129 | 130 | public function keys() 131 | { 132 | return array_keys($this); 133 | } 134 | 135 | public function keySort() 136 | { 137 | ksort($this); 138 | return $this; 139 | } 140 | 141 | public function map($callback, $arguments = null) 142 | { 143 | $array = $this; 144 | if(null !== $callback) { 145 | $this->verifyCallable($callback); 146 | } 147 | if(null === $arguments) { 148 | $result = array_map($callback, $array); 149 | } else { 150 | $args = func_get_args(); 151 | array_shift($args); 152 | array_unshift($args, $callback, $array); 153 | $result = call_user_func_array("array_map", $args); 154 | } 155 | 156 | return $result; 157 | 158 | } 159 | 160 | public function max() 161 | { 162 | return max($this); 163 | } 164 | 165 | public function merge($array) 166 | { 167 | $this->verifyArray($array, "merge"); 168 | return array_merge($this, $array); 169 | } 170 | 171 | public function min() 172 | { 173 | return min($this); 174 | } 175 | 176 | public function push($val) 177 | { 178 | array_push($this, $val); 179 | return $this; 180 | } 181 | 182 | public function rand($number = 1) 183 | { 184 | $r = array_rand($this, $number); 185 | return $this[$r]; 186 | } 187 | 188 | public function reduce($callback, $initial = null) 189 | { 190 | $this->verifyCallable($callback); 191 | return array_reduce($this, $callback, $initial); 192 | } 193 | 194 | public function reindex($by = null) 195 | { 196 | if(null === $by) return array_values($this); 197 | if(is_callable($by)) { 198 | $keys = array_map($by, $this); 199 | return array_combine($keys, array_values($this)); 200 | } 201 | } 202 | 203 | public function reverse() 204 | { 205 | return array_reverse($this); 206 | } 207 | 208 | public function reverseKeySort() 209 | { 210 | krsort($this); 211 | return $this; 212 | } 213 | 214 | public function slice($offset, $length = null, $preserve = false) 215 | { 216 | $this->verifyInteger($offset, "slice"); 217 | return array_slice($this, $offset, $length, $preserve); 218 | } 219 | 220 | public function splice($offset, $length = null, $replacement = null) 221 | { 222 | $this->verifyInteger($offset, "splice"); 223 | $array = $this; 224 | if(null === $length) { 225 | $extracted = array_splice($array, $offset); 226 | } else { 227 | $extracted = array_splice($array, $offset, $length, $replacement); 228 | } 229 | 230 | return $array; 231 | } 232 | 233 | public function sort($flags = null) 234 | { 235 | $array = $this; 236 | $result = sort($array, $flags); 237 | 238 | if ($result === false) { 239 | throw new \InvalidArgumentException("Array object could not be sorted"); 240 | } 241 | 242 | return $array; 243 | } 244 | 245 | public function sum() 246 | { 247 | return array_sum($this); 248 | } 249 | 250 | public function toArray() 251 | { 252 | $array = $this; 253 | return $array; 254 | } 255 | 256 | public function toFloat() 257 | { 258 | return float($this); 259 | } 260 | 261 | public function toInt() 262 | { 263 | return int($this); 264 | } 265 | 266 | public function toJSON() 267 | { 268 | return json_encode($this); 269 | } 270 | 271 | 272 | public function toString() 273 | { 274 | return string($this); 275 | } 276 | 277 | public function values() 278 | { 279 | return array_values($this); 280 | } 281 | 282 | 283 | 284 | 285 | protected function verifyInteger($input = null, $methodName = "") 286 | { 287 | if (false === is_int($input)) { 288 | throw new \InvalidArgumentException("Argument passed to $methodName has to be an integer"); 289 | } 290 | } 291 | 292 | protected function verifyString($input = null, $methodName = "") 293 | { 294 | if (false === is_string($input)) { 295 | throw new \InvalidArgumentException("Argument passed to $methodName has to be a string"); 296 | } 297 | } 298 | 299 | protected function verifyArray($input = null, $methodName = "") 300 | { 301 | if (false === is_array($input)) { 302 | throw new \InvalidArgumentException("Argument passed to $methodName has to be a array"); 303 | } 304 | } 305 | 306 | protected function verifyCallable($input = null, $methodName = "") 307 | { 308 | if (false === is_callable($input)) { 309 | throw new \InvalidArgumentException("Argument passed to $methodName needs to be callable"); 310 | } 311 | } 312 | 313 | 314 | } 315 | -------------------------------------------------------------------------------- /src/BooleanHandler.php: -------------------------------------------------------------------------------- 1 | toString(); 25 | } 26 | return $this->toString(); 27 | } 28 | 29 | public function toFixed ($int = null) { 30 | if ($int === null) { 31 | return $this->toString(); 32 | } 33 | return round($this, $int)->toString(); 34 | } 35 | 36 | 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/IntegerHandler.php: -------------------------------------------------------------------------------- 1 | prepareOffset($offset); 32 | $length = $this->prepareLength($offset, $length); 33 | 34 | if (0 === $length) { 35 | return ''; 36 | } 37 | 38 | return substr($this, $offset, $length); 39 | } 40 | 41 | public function replaceSlice($replacement, $offset, $length = null) 42 | { 43 | $offset = $this->prepareOffset($offset); 44 | $length = $this->prepareLength($offset, $length); 45 | 46 | return substr_replace($this, $replacement, $offset, $length); 47 | } 48 | 49 | /* 50 | * Search methods 51 | */ 52 | 53 | public function indexOf($string, $offset = 0) 54 | { 55 | $offset = $this->prepareOffset($offset); 56 | 57 | if ('' === $string) { 58 | return $offset; 59 | } 60 | 61 | return strpos($this, $string, $offset); 62 | } 63 | 64 | public function lastIndexOf($string, $offset = null) 65 | { 66 | if (null === $offset) { 67 | $offset = $this->length(); 68 | } else { 69 | $offset = $this->prepareOffset($offset); 70 | } 71 | 72 | if ('' === $string) { 73 | return $offset; 74 | } 75 | 76 | /* Converts $offset to a negative offset as strrpos has a different 77 | * behavior for positive offsets. */ 78 | return strrpos($this, $string, $offset - $this->length()); 79 | } 80 | 81 | public function capitalize() 82 | { 83 | return ucwords($this); 84 | } 85 | 86 | public function contains($string) 87 | { 88 | return false !== $this->indexOf($string); 89 | } 90 | 91 | public function lower() 92 | { 93 | return strtolower($this); 94 | } 95 | 96 | public function startsWith($string) 97 | { 98 | return 0 === $this->indexOf($string); 99 | } 100 | 101 | public function endsWith($string) 102 | { 103 | return $this->lastIndexOf($string) === $this->length() - $string->length(); 104 | } 105 | 106 | public function count($string, $offset = 0, $length = null) 107 | { 108 | $offset = $this->prepareOffset($offset); 109 | $length = $this->prepareLength($offset, $length); 110 | 111 | if ('' === $string) { 112 | return $length + 1; 113 | } 114 | 115 | return substr_count($this, $string, $offset, $length); 116 | } 117 | 118 | /* This function has two prototypes: 119 | * 120 | * replace(array(string $from => string $to) $replacements, int $limit = PHP_MAX_INT) 121 | * replace(string $from, string $to, int $limit = PHP_MAX_INT) 122 | */ 123 | public function replace($from, $to = null, $limit = null) 124 | { 125 | if (is_array($from)) { 126 | return $this->replacePairs($from, $to); 127 | } 128 | 129 | if (null === $limit) { 130 | return str_replace($from, $to, $this); 131 | } 132 | 133 | $this->verifyPositive($limit, 'Limit'); 134 | return $this->replaceWithLimit($this, $from, $to, $limit); 135 | } 136 | 137 | public function split($separator, $limit = PHP_INT_MAX) 138 | { 139 | return explode($separator, $this, $limit); 140 | } 141 | 142 | public function chunk($chunkLength = 1) 143 | { 144 | $this->verifyPositive($chunkLength, 'Chunk length'); 145 | return str_split($this, $chunkLength); 146 | } 147 | 148 | public function repeat($times) 149 | { 150 | $this->verifyNotNegative($times, 'Number of repetitions'); 151 | return str_repeat($this, $times); 152 | } 153 | 154 | public function reverse() 155 | { 156 | return strrev($this); 157 | } 158 | 159 | 160 | public function trim($characters = " \t\n\r\v\0") 161 | { 162 | return trim($this, $characters); 163 | } 164 | 165 | public function trimLeft($characters = " \t\n\r\v\0") 166 | { 167 | return ltrim($this, $characters); 168 | } 169 | 170 | public function trimRight($characters = " \t\n\r\v\0") 171 | { 172 | return rtrim($this, $characters); 173 | } 174 | 175 | public function padLeft($length, $padString = " ") 176 | { 177 | return str_pad($this, $length, $padString, STR_PAD_LEFT); 178 | } 179 | 180 | public function padRight($length, $padString = " ") 181 | { 182 | return str_pad($this, $length, $padString, STR_PAD_RIGHT); 183 | } 184 | 185 | 186 | public function toArray() 187 | { 188 | return [$this]; 189 | } 190 | 191 | public function toFloat() 192 | { 193 | return float($this); 194 | } 195 | 196 | public function toInt() 197 | { 198 | return int($this); 199 | } 200 | 201 | public function toJSON() 202 | { 203 | return json_encode($this); 204 | } 205 | 206 | 207 | public function toString() 208 | { 209 | return $this; 210 | } 211 | 212 | public function upper() 213 | { 214 | return strtoupper($this); 215 | } 216 | 217 | 218 | // Internal methods, not part of public API 219 | 220 | protected function prepareOffset($offset) 221 | { 222 | $len = $this->length(); 223 | if ($offset < -$len || $offset > $len) { 224 | throw new \InvalidArgumentException('Offset must be in range [-len, len]'); 225 | } 226 | 227 | if ($offset < 0) { 228 | $offset += $len; 229 | } 230 | 231 | return $offset; 232 | } 233 | 234 | protected function prepareLength($offset, $length) 235 | { 236 | if (null === $length) { 237 | return $this->length() - $offset; 238 | } 239 | 240 | if ($length < 0) { 241 | $length += $this->length() - $offset; 242 | 243 | if ($length < 0) { 244 | throw new \InvalidArgumentException('Length too small'); 245 | } 246 | } else { 247 | if ($offset + $length > $this->length()) { 248 | throw new \InvalidArgumentException('Length too large'); 249 | } 250 | } 251 | 252 | return $length; 253 | } 254 | 255 | protected function verifyPositive($value, $name) 256 | { 257 | if ($value <= 0) { 258 | throw new \InvalidArgumentException("$name has to be positive"); 259 | } 260 | } 261 | 262 | protected function verifyNotNegative($value, $name) 263 | { 264 | if ($value < 0) { 265 | throw new \InvalidArgumentException("$name can not be negative"); 266 | } 267 | } 268 | 269 | protected function replacePairs($replacements, $limit) 270 | { 271 | if (null === $limit) { 272 | return strtr($this, $replacements); 273 | } 274 | 275 | $this->verifyPositive($limit, 'Limit'); 276 | $str = $this; 277 | foreach ($replacements as $from => $to) { 278 | $str = $this->replaceWithLimit($str, $from, $to, $limit); 279 | if (0 === $limit) { 280 | break; 281 | } 282 | } 283 | return $str; 284 | } 285 | 286 | protected function replaceWithLimit($str, $from, $to, &$limit) 287 | { 288 | $lenDiff = $to->length() - $from->length(); 289 | $index = 0; 290 | 291 | while (false !== $index = $str->indexOf($from, $index)) { 292 | $str = $str->replaceSlice($to, $index, $to->length()); 293 | $index += $lenDiff; 294 | 295 | if (0 === --$limit) { 296 | break; 297 | } 298 | } 299 | 300 | return $str; 301 | } 302 | } 303 | -------------------------------------------------------------------------------- /tests/ArrayHandlerTest.php: -------------------------------------------------------------------------------- 1 | simpleArray; 15 | $this->assertEquals($arr->chunk(2), [["first","second"],["third","fourth"],["fifth"]]); 16 | } 17 | 18 | public function test_column() { 19 | $arr = [["key1"=>"value","key2"=>"value2"],["key1"=>"value","key2"=>"value2"]]; 20 | $this->assertEquals($arr->column("key2"), ["value2","value2"]); 21 | } 22 | 23 | public function testCompact() 24 | { 25 | $arr = [0=>"val", 1=>false, 2=>"val", 3=>null]; 26 | $this->assertEquals($arr->compact(), [0=>"val", 2=>"val"]); 27 | } 28 | 29 | public function test_combine() { 30 | $keys = ["k1","k2","k3","k4"]; 31 | $vals = ["val1","val2","val3","val4"]; 32 | $this->assertEquals($keys->combine($vals), ["k1"=>"val1","k2"=>"val2","k3"=>"val3","k4"=>"val4"]); 33 | 34 | } 35 | 36 | public function test_each() { 37 | $arr = [1,2,3,4,5,6,7,8,9,10]; 38 | $arr->each(function(&$value){ $value *=2;}); 39 | $this->assertEquals($arr, [2,4,6,8,10,12,14,16,18,20]); 40 | } 41 | 42 | 43 | public function test_has() { 44 | $arr = $this->simpleArray; 45 | $this->assertEquals($arr->has("second"), true); 46 | $this->assertEquals($arr->has("tenth"), false); 47 | 48 | } 49 | 50 | public function test_indexOf() { 51 | $arr = $this->simpleArray; 52 | $this->assertEquals($arr->indexOf("second"), 1); 53 | $this->assertEquals($arr->indexOf("tenth"), false); 54 | $keyarray = ['a' => 1, 'b' => 2, 'c' => 3]; 55 | $this->assertEquals($keyarray->indexOf(2), "b"); 56 | } 57 | 58 | public function test_join() { 59 | $arr = ["these","are","some","words"]; 60 | $this->assertEquals($arr->join(" "), "these are some words"); 61 | $this->assertEquals($arr->join(), "thesearesomewords"); 62 | } 63 | 64 | public function test_max() { 65 | $arr = [2,4,6,8,10]; 66 | $this->assertEquals($arr->max(), 10); 67 | 68 | $arr = [2,4, 11, 6,8,10]; 69 | $this->assertEquals($arr->max(), 11); 70 | } 71 | 72 | public function test_merge_chain() { 73 | $arr1 = ["k1"=>"val1","k2"=>"val2"]; 74 | $arr2 = ["k3"=>"val3","k4"=>"val4"]; 75 | $this->assertEquals($arr1->merge($arr2)->count(), 4); 76 | } 77 | 78 | public function test_min() { 79 | $arr = [2, 4, 6, 8, 10]; 80 | $this->assertEquals($arr->min(), 2); 81 | 82 | $arr = [2, 4, 6, -2, 8, 10]; 83 | $this->assertEquals($arr->min(), -2); 84 | } 85 | 86 | public function test_reindex() { 87 | $arr = [ 88 | ["id"=>5, "val"=>"first"], 89 | ["id"=>6, "val"=>"second"], 90 | ["id"=>7, "val"=>"third"] 91 | ]; 92 | 93 | $expected = [ 94 | 0=>["id"=>5, "val"=>"first"], 95 | 1=>["id"=>6, "val"=>"second"], 96 | 2=>["id"=>7, "val"=>"third"] 97 | ]; 98 | 99 | $this->assertEquals($arr->reindex(), $expected ); 100 | 101 | $expected = [ 102 | 5=>["id"=>5, "val"=>"first"], 103 | 6=>["id"=>6, "val"=>"second"], 104 | 7=>["id"=>7, "val"=>"third"] 105 | ]; 106 | 107 | $this->assertEquals($arr->reindex(function($row){return $row["id"];}), $expected); 108 | 109 | } 110 | 111 | public function test_reindex_object() { 112 | $arr = [ 113 | (object)["id"=>5, "val"=>"first"], 114 | (object)["id"=>6, "val"=>"second"], 115 | (object)["id"=>7, "val"=>"third"] 116 | ]; 117 | 118 | $expected = [ 119 | 5=>(object)["id"=>5, "val"=>"first"], 120 | 6=>(object)["id"=>6, "val"=>"second"], 121 | 7=>(object)["id"=>7, "val"=>"third"] 122 | ]; 123 | 124 | $this->assertEquals($arr->reindex(function($row){return $row->id;}), $expected); 125 | 126 | } 127 | 128 | public function test_sum() { 129 | $arr = [2,4,6,8,10]; 130 | $this->assertEquals($arr->sum(), 30); 131 | 132 | $arr = [-2,4,6,8,10]; 133 | $this->assertEquals($arr->sum(), 26); 134 | } 135 | 136 | public function test_any() { 137 | $arr = [true, false]; 138 | $this->assertEquals($arr->any(), true); 139 | 140 | $arr = [false, false]; 141 | $this->assertEquals($arr->any(), false); 142 | 143 | $arr = [false, true, false]; 144 | $this->assertEquals($arr->any(), true); 145 | 146 | $arr = [1, 0, false]; 147 | $this->assertEquals($arr->any(), true); 148 | 149 | $arr = [false, 0, '', []]; 150 | $this->assertEquals($arr->any(), false); 151 | 152 | $arr = [false, 0, '', [1]]; 153 | $this->assertEquals($arr->any(), true); 154 | } 155 | 156 | public function test_all() { 157 | $arr = [2,4,6,8,10]; 158 | $this->assertEquals($arr->all(), true); 159 | 160 | $arr = [true]; 161 | $this->assertEquals($arr->all(), true); 162 | 163 | $arr = [1]; 164 | $this->assertEquals($arr->all(), true); 165 | 166 | $arr = ['asdf']; 167 | $this->assertEquals($arr->all(), true); 168 | 169 | $arr = [[1]]; 170 | $this->assertEquals($arr->all(), true); 171 | 172 | $arr = [true, 1, 'asdf', [1]]; 173 | $this->assertEquals($arr->all(), true); 174 | 175 | $arr = [false, 1, 'asdf', [1]]; 176 | $this->assertEquals($arr->all(), false); 177 | 178 | $arr = [true, 0, 'asdf', [1]]; 179 | $this->assertEquals($arr->all(), false); 180 | 181 | $arr = [true, 1, '', [1]]; 182 | $this->assertEquals($arr->all(), false); 183 | 184 | $arr = [true, 1, 'asdf', []]; 185 | $this->assertEquals($arr->all(), false); 186 | 187 | $arr = [false, 0, '', []]; 188 | $this->assertEquals($arr->all(), false); 189 | } 190 | 191 | public function test_intersect() { 192 | $arr1 = ['a' => 'green', 'red', 'blue']; 193 | $arr2 = ['b' => 'green', 'yellow', 'red']; 194 | $this->assertEquals($arr1->intersect($arr2), ['a' => 'green', 0 => 'red']); 195 | } 196 | 197 | public function test_reduce() { 198 | 199 | $rsum = function($accumulator, $w) { 200 | $accumulator += $w; 201 | return $accumulator; 202 | }; 203 | 204 | $rmul = function($accumulator, $w) { 205 | $accumulator *= $w; 206 | return $accumulator; 207 | }; 208 | 209 | $mappish_cube = function ($accumulator, $w) { 210 | $accumulator[] = $w * $w; 211 | return $accumulator; 212 | }; 213 | 214 | $arr_num = [1,2,3,4,5]; 215 | $arr_emp = []; 216 | 217 | $this->assertEquals($arr_num->reduce($rsum), 15); 218 | $this->assertEquals($arr_num->reduce($rmul, 10), 1200); 219 | $this->assertEquals($arr_num->reduce($mappish_cube, []), [1, 4, 9, 16, 25]); 220 | $this->assertEquals($arr_emp->reduce($rsum, 'No data to reduce'), 'No data to reduce'); 221 | } 222 | 223 | public function test_map() { 224 | // Test #1 225 | $cube = function($n) { 226 | return($n * $n * $n); 227 | }; 228 | 229 | $arr = [1,2,3,4,5]; 230 | $this->assertEquals($arr->map($cube), [1,8,27,64,125]); 231 | 232 | // Test #2 233 | $func = function($value) { 234 | return $value * 2; 235 | }; 236 | 237 | $arr = [1,2,3,4,5]; 238 | $this->assertEquals($arr->map($func), [2,4,6,8,10]); 239 | 240 | // Test #3 241 | $sSpan = function($n, $m) { 242 | return "The number $n is called $m in Spanish"; 243 | }; 244 | 245 | $mSpan = function($n, $m) { 246 | return [$n => $m]; 247 | }; 248 | 249 | $arr_num = [1,2,3,4,5]; 250 | $arr_txt = ['uno','dos','tres','cuatro','cinco']; 251 | 252 | 253 | $this->assertEquals ( 254 | $arr_num->map($sSpan, $arr_txt), 255 | [ 256 | 'The number 1 is called uno in Spanish', 257 | 'The number 2 is called dos in Spanish', 258 | 'The number 3 is called tres in Spanish', 259 | 'The number 4 is called cuatro in Spanish', 260 | 'The number 5 is called cinco in Spanish', 261 | ] 262 | ); 263 | 264 | 265 | $this->assertEquals ( 266 | $arr_num->map($mSpan, $arr_txt), 267 | [[1=>'uno'],[2=>'dos'],[3=>'tres'],[4=>'cuatro'],[5=>'cinco']] 268 | ); 269 | 270 | // Test #4 271 | $arr_num = [1,2,3,4,5]; 272 | $arr_eng = ['one','two','three','four','five']; 273 | $arr_esp = ['uno','dos','tres','cuatro','cinco']; 274 | 275 | $this->assertEquals( 276 | $arr_num->map(null, $arr_eng, $arr_esp), 277 | [ 278 | [1,'one','uno'], 279 | [2,'two','dos'], 280 | [3,'three','tres'], 281 | [4,'four','cuatro'], 282 | [5,'five','cinco'] 283 | ] 284 | ); 285 | 286 | // Test #5 287 | $cb1 = function($a) { 288 | return [$a]; 289 | }; 290 | 291 | $cb2 = function($a,$b) { 292 | return [$a,$b]; 293 | }; 294 | 295 | $arr = ['stringkey'=>'value']; 296 | $this->assertEquals($arr->map($cb1), ["stringkey"=>['value']]); 297 | $this->assertEquals($arr->map($cb2,$arr), [['value','value']]); 298 | $this->assertEquals($arr->map(null), ['stringkey'=>'value']); 299 | $this->assertEquals($arr->map(null,$arr), [['value','value']]); 300 | } 301 | 302 | public function test_filter() { 303 | $odd = function($var){ 304 | return($var & 1); 305 | }; 306 | 307 | $even = function($var){ 308 | return(!($var & 1)); 309 | }; 310 | 311 | $arr1 = ['a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5]; 312 | $arr2 = [6,7,8,9,10,11,12]; 313 | 314 | $this->assertEquals($arr1->filter($odd), ['a'=>1,'c'=>3,'e'=>5] ); 315 | $this->assertEquals($arr2->filter($even), [0=>6, 2=>8, 4=>10, 6=>12]); 316 | } 317 | 318 | public function test_intersperse() { 319 | $this->assertEquals($this->simpleArray->intersperse('foo'), ["first", "foo", "second", "foo", "third", "foo", "fourth", "foo", "fifth"]); 320 | } 321 | 322 | public function test_difference() { 323 | $arr1 = ['a'=>'green','red','blue','red']; 324 | $arr2 = ['b'=>'green','yellow','red']; 325 | $this->assertEquals($arr1->difference($arr2), [1=>'blue']); 326 | } 327 | 328 | public function test_slice() { 329 | $arr = ['a','b','c','d','e']; 330 | 331 | $this->assertEquals($arr->slice(2), ['c','d','e']); 332 | $this->assertEquals($arr->slice(-2, 1), ['d']); 333 | $this->assertEquals($arr->slice(0, 3), ['a','b','c']); 334 | 335 | $this->assertEquals($arr->slice(2, -1), [0=>'c',1=>'d']); 336 | $this->assertEquals($arr->slice(2, -1, true), [2=>'c',3=>'d']); 337 | } 338 | 339 | public function test_splice() { 340 | $arr = ['red','green','blue','yellow']; 341 | $this->assertEquals($arr->splice(2),['red','green']); 342 | $this->assertEquals($arr->splice(1, -1),['red','yellow']); 343 | $this->assertEquals($arr->splice(1, count($arr), 'orange'),['red','orange']); 344 | $this->assertEquals($arr->splice(-1, 1, ['black','maroon']),['red','green','blue','black','maroon']); 345 | $this->assertEquals($arr->splice(3, 0, 'purple'),['red','green','blue', 'purple', 'yellow']); 346 | } 347 | 348 | public function test_repeat () { 349 | //$this->assertEquals(ArrayHandler::repeat(6, 'foo'), ['foo', 'foo', 'foo', 'foo', 'foo', 'foo']); 350 | //$this->assertEquals(ArrayHandler::repeat(2, 1), [1, 1]); 351 | } 352 | 353 | public function test_range () { 354 | //$this->assertEquals(ArrayHandler::range(0, 10), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); 355 | //$this->assertEquals(ArrayHandler::range(0, 10, 2), [0, 2, 4, 6, 8, 10]); 356 | } 357 | } 358 | -------------------------------------------------------------------------------- /tests/BaseHandlerTest.php: -------------------------------------------------------------------------------- 1 | assertEquals($array->isArray(), true); 15 | $this->assertEquals($array->isBool(), false); 16 | $this->assertEquals($array->isFloat(), false); 17 | $this->assertEquals($array->isInt(), false); 18 | $this->assertEquals($array->isNull(), false); 19 | $this->assertEquals($array->isResource(), false); 20 | $this->assertEquals($array->isString(), false); 21 | } 22 | 23 | public function testBooleans() 24 | { 25 | $bool = true; 26 | $this->assertEquals($bool->isArray(), false); 27 | $this->assertEquals($bool->isBool(), true); 28 | $this->assertEquals($bool->isFloat(), false); 29 | $this->assertEquals($bool->isInt(), false); 30 | $this->assertEquals($bool->isNull(), false); 31 | $this->assertEquals($bool->isResource(), false); 32 | $this->assertEquals($bool->isString(), false); 33 | } 34 | 35 | public function testFloat() 36 | { 37 | $float = 123.45678; 38 | $this->assertEquals($float->isArray(), false); 39 | $this->assertEquals($float->isBool(), false); 40 | $this->assertEquals($float->isFloat(), true); 41 | $this->assertEquals($float->isInt(), false); 42 | $this->assertEquals($float->isNull(), false); 43 | $this->assertEquals($float->isResource(), false); 44 | $this->assertEquals($float->isString(), false); 45 | } 46 | 47 | public function testInt() 48 | { 49 | $int = 100; 50 | $this->assertEquals($int->isArray(), false); 51 | $this->assertEquals($int->isBool(), false); 52 | $this->assertEquals($int->isFloat(), false); 53 | $this->assertEquals($int->isInt(), true); 54 | $this->assertEquals($int->isNull(), false); 55 | $this->assertEquals($int->isResource(), false); 56 | $this->assertEquals($int->isString(), false); 57 | } 58 | 59 | public function testNull() 60 | { 61 | $null = null; 62 | $this->assertEquals($null->isArray(), false); 63 | $this->assertEquals($null->isBool(), false); 64 | $this->assertEquals($null->isFloat(), false); 65 | $this->assertEquals($null->isInt(), false); 66 | $this->assertEquals($null->isNull(), true); 67 | $this->assertEquals($null->isResource(), false); 68 | $this->assertEquals($null->isString(), false); 69 | } 70 | 71 | public function testString() 72 | { 73 | $str = "Hello World"; 74 | $this->assertEquals($str->isArray(), false); 75 | $this->assertEquals($str->isBool(), false); 76 | $this->assertEquals($str->isFloat(), false); 77 | $this->assertEquals($str->isInt(), false); 78 | $this->assertEquals($str->isNull(), false); 79 | $this->assertEquals($str->isResource(), false); 80 | $this->assertEquals($str->isString(), true); 81 | } 82 | 83 | } -------------------------------------------------------------------------------- /tests/BooleanHandlerTest.php: -------------------------------------------------------------------------------- 1 | assertTrue($true->isBool()); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /tests/FloatHandlerTest.php: -------------------------------------------------------------------------------- 1 | assertEquals($float->round(),1); 15 | $this->assertEquals($float->round(1),1.5); 16 | $this->assertEquals($float->round(2),1.46); 17 | 18 | } 19 | 20 | public function test_toFixed() { 21 | $f = 5.123456; 22 | $this->assertEquals($f->toFixed(), "5.123456"); 23 | $this->assertEquals($f->toFixed(1), "5.1"); 24 | $this->assertEquals($f->toFixed(2), "5.12"); 25 | $this->assertEquals($f->toFixed(3), "5.123"); 26 | $this->assertEquals($f->toFixed(4), "5.1235"); 27 | $this->assertEquals($f->toFixed(5), "5.12346"); 28 | $this->assertEquals($f->toFixed(6), "5.123456"); 29 | $this->assertEquals($f->toFixed(10), "5.1234560000"); 30 | } 31 | 32 | public function test_toPrecision() { 33 | $this->markTestSkipped('must be revisited'); 34 | $f = 5.123456; 35 | $this->assertEquals($f->toPrecision(), "5.123456"); 36 | $this->assertEquals($f->toPrecision(5), "5.1235"); 37 | $this->assertEquals($f->toPrecision(2), "5.1"); 38 | $this->assertEquals($f->toPrecision(1), "5"); 39 | 40 | $f = 10.123; 41 | $this->assertEquals($f->toPrecision(), "10.123"); 42 | $this->assertEquals($f->toPrecision(2), "10"); 43 | $this->assertEquals($f->toPrecision(3), "10.1"); 44 | $this->assertEquals($f->toPrecision(1), "1e1"); 45 | 46 | $f = 10234.4; 47 | $this->assertEquals($f->toPrecision(), "10234.4"); 48 | $this->assertEquals($f->toPrecision(6), "10234.4"); 49 | $this->assertEquals($f->toPrecision(5), "10234"); 50 | $this->assertEquals($f->toPrecision(4), "1023e1"); 51 | $this->assertEquals($f->toPrecision(3), "102e2"); 52 | $this->assertEquals($f->toPrecision(2), "10e3"); 53 | $this->assertEquals($f->toPrecision(1), "1e4"); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /tests/IntegerHandlerTest.php: -------------------------------------------------------------------------------- 1 | assertEquals($int->toInt(), $int); 15 | } 16 | 17 | public function testEven() { 18 | $int = 2; 19 | $this->assertTrue($int->even()); 20 | $this->assertFalse($int->odd()); 21 | } 22 | 23 | public function testOdd() { 24 | $int = 7; 25 | $this->assertTrue($int->odd()); 26 | $this->assertFalse($int->even()); 27 | } 28 | 29 | public function testSqrt() 30 | { 31 | $int = 64; 32 | $this->assertEquals($int->sqrt(), 8); 33 | } 34 | 35 | } -------------------------------------------------------------------------------- /tests/NullHandlerTest.php: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rossriley/php-scalar-objects/918390ef6bc68a5de4201d2e60b9425fb1d4351c/tests/NullHandlerTest.php -------------------------------------------------------------------------------- /tests/StringHandlerTest.php: -------------------------------------------------------------------------------- 1 | assertEquals($str->capitalize(), "Hello World"); 16 | } 17 | 18 | public function testCaseCompare() 19 | { 20 | $str = "hello world"; 21 | $this->assertEquals($str->caseCompare("Hello World"), 0); 22 | } 23 | 24 | public function testHash() { 25 | $str = "testing"; 26 | $this->assertEquals($str->hash()->length(), 60 ); 27 | } 28 | 29 | public function testLength() { 30 | $str = "testing"; 31 | $this->assertEquals($str->length(),7); 32 | } 33 | 34 | public function testLower() 35 | { 36 | $str = "Hello World"; 37 | $this->assertEquals($str->lower(), "hello world"); 38 | } 39 | 40 | public function testPadLeft() 41 | { 42 | $str = "Hello World"; 43 | $this->assertEquals($str->padLeft(12), " Hello World"); 44 | $this->assertEquals($str->padLeft(12, "-"), "-Hello World"); 45 | } 46 | 47 | public function testPadRight() 48 | { 49 | $str = "Hello World"; 50 | $this->assertEquals($str->padRight(12), "Hello World "); 51 | $this->assertEquals($str->padRight(12, "-"), "Hello World-"); 52 | } 53 | 54 | public function testRepeat() { 55 | $str = "testing"; 56 | $this->assertEquals($str->repeat(3), "testingtestingtesting"); 57 | } 58 | 59 | public function testReverse() { 60 | $str = "testing"; 61 | $this->assertEquals($str->reverse(), "gnitset"); 62 | } 63 | 64 | public function testTrims() { 65 | $str = " testing "; 66 | $this->assertEquals($str->trim(), "testing"); 67 | $this->assertEquals($str->trimRight(), " testing"); 68 | $this->assertEquals($str->trimLeft(), "testing "); 69 | 70 | $str = "testing"; 71 | $this->assertEquals($str->trim("ing"), "test"); 72 | } 73 | 74 | public function testUpper() 75 | { 76 | $str = "Hello World"; 77 | $this->assertEquals($str->upper(), "HELLO WORLD"); 78 | } 79 | 80 | } -------------------------------------------------------------------------------- /tests/bootstrap.php: -------------------------------------------------------------------------------- 1 |