├── CHANGELOG.md ├── CONTRIBUTORS.md ├── LICENSE ├── README.md ├── composer.json └── src ├── Collection.php ├── General.php ├── Invoke.php ├── Object.php └── Strings.php /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | All notable changes to this project will be documented in this file. 3 | This project adheres to [Semantic Versioning](http://semver.org/). 4 | 5 | ## [Unreleased] 6 | 7 | ## [1.3.0] - 2016-02-01 8 | ### Added 9 | - Collection\toJson from [chapeupreto] 10 | - Object\assignIfIsset from [gcds] 11 | 12 | ### Fixed 13 | - Copyright years from [gcds] 14 | 15 | ## [1.2.0] - 2015-12-24 16 | ### Added 17 | - Collection\size from [chapeupreto] 18 | 19 | ### Changed 20 | - Funct\ArrayKeyNotExists additional test cases from [Newman101] 21 | - String\truncate optimization from [Newman101] 22 | - Collection\some optimization from [zhikiri] 23 | 24 | ### Fixed 25 | - Readme file from [dfridrich] 26 | - Readme file from [chapeupreto] 27 | - PSR-2 code fix from [gcds] 28 | 29 | ## [1.1.0] - 2015-12-11 30 | ### Added 31 | - Strings\reverse from [chapeupreto] 32 | - Strings\length,len from [chapeupreto] 33 | - Collection\reverse from [chapeupreto] 34 | 35 | ### Changed 36 | - Strings\latinize bigger char mapping map from [gcds] 37 | 38 | ### Fixed 39 | - Readme file from [hansott] 40 | - Readme file from [chapeupreto] 41 | 42 | ## [1.0.0] - 2015-12-02 43 | ### Added 44 | - Initial stable version code 45 | 46 | [gcds]: https://github.com/gcds 47 | [hansott]: https://github.com/hansott 48 | [zhikiri]: https://github.com/zhikiri 49 | [dfridrich]: https://github.com/dfridrich 50 | [Newman101]: https://github.com/Newman101 51 | [chapeupreto]: https://github.com/chapeupreto 52 | 53 | 54 | [Unreleased]: https://github.com/phpfunct/funct/compare/1.3.0...HEAD 55 | [1.3.0]: https://github.com/phpfunct/funct/compare/1.2.0...1.3.0 56 | [1.2.0]: https://github.com/phpfunct/funct/compare/1.1.0...1.2.0 57 | [1.1.0]: https://github.com/phpfunct/funct/compare/1.0.0...1.1.0 58 | [1.0.0]: https://github.com/phpfunct/funct/compare/0.2.0...1.0.0 59 | [0.2.0]: https://github.com/phpfunct/funct/compare/0.1.0...0.2.0 60 | -------------------------------------------------------------------------------- /CONTRIBUTORS.md: -------------------------------------------------------------------------------- 1 | CONTRIBUTORS 2 | ============ 3 | 4 | The list of people who contributed to this library. 5 | 6 | - Aurimas Niekis 7 | - Ernestas Kvedaras 8 | - Lucantis Swann 9 | - Rod Elias 10 | - Hans Ott -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Aurimas Niekis 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![Funct](http://i.imgur.com/jtKjWfv.png) 2 | 3 | A PHP library with commonly used code blocks for faster development 4 | 5 | ```PHP 6 | Funct\firstValueNotEmpty($a, $b, $c) 7 | ``` 8 | 9 | [![Latest Version](https://img.shields.io/github/release/phpfunct/funct.svg?style=flat-square)](https://github.com/phpfunct/funct/releases) 10 | [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE) 11 | [![Build Status](https://img.shields.io/travis/phpfunct/funct.svg?style=flat-square)](https://travis-ci.org/phpfunct/funct) 12 | [![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/phpfunct/funct.svg?style=flat-square)](https://scrutinizer-ci.com/g/phpfunct/funct) 13 | [![Quality Score](https://img.shields.io/scrutinizer/g/phpfunct/funct.svg?style=flat-square)](https://scrutinizer-ci.com/g/phpfunct/funct) 14 | [![Total Downloads](https://img.shields.io/packagist/dt/funct/funct.svg?style=flat-square)](https://packagist.org/packages/funct/funct) 15 | 16 | [![Email](https://img.shields.io/badge/email-aurimas@niekis.lt-blue.svg?style=flat-square)](mailto:aurimas@niekis.lt) 17 | 18 | 19 | * [Requirements](#requirements) 20 | * [Installation](#installation) 21 | * [Usage](#usage) 22 | * [Library](#general) 23 | * [General](#general) 24 | * [arrayKeyNotExists](#arraykeynotexistskey-array-array) 25 | * [false](#falsevalue) 26 | * [firstValue](#firstvaluevaluea) 27 | * [firstValueNotEmpty](#firstvaluenotemptyvaluea-valueb) 28 | * [ifSetOr](#ifsetorvalue-default) 29 | * [notEmpty](#notemptyvalue) 30 | * [notInArray](#notinarrayneedle-haystack-strict--null) 31 | * [notNull](#notnullvalue) 32 | * [null](#nullvalue) 33 | * [tempFile](#tempfileprefix--php) 34 | * [true](#truevalue) 35 | * [Collection](#collection) 36 | * [compact](#compactcollection) 37 | * [countBy](#countbycollection-callback) 38 | * [every](#everycollection-callable-callback--null) 39 | * [findWhere](#findwherecollection-value) 40 | * [first](#firstcollection) 41 | * [firstN](#firstncollection-n--1) 42 | * [flatten](#flattencollection-depth--1) 43 | * [flattenAll](#flattenallcollection) 44 | * [forEvery](#foreverycollection-callable) 45 | * [get](#getcollection-key-default--null) 46 | * [groupBy](#groupbycollection-callback) 47 | * [initial](#initialcollection-n--1) 48 | * [intersection](#intersectioncollectionfirst-collectionsecond) 49 | * [invoke](#invokecollection-callable-callback) 50 | * [last](#lastcollection) 51 | * [lastIndexOf](#lastindexofcollection-value) 52 | * [lastN](#lastncollection-n--1) 53 | * [maxValue](#maxvaluecollection-callable-callback) 54 | * [merge](#mergea-b) 55 | * [minValue](#minvaluecollection-callable-callback) 56 | * [pairs](#pairscollection) 57 | * [partition](#partitioncollection-callable-callback) 58 | * [pluck](#pluckcollection-key) 59 | * [reject](#rejectcollection-callable-callback) 60 | * [rest](#restcollection-from--1) 61 | * [reverse](#reversecollection-preservenumerickeys) 62 | * [size](#sizecollection-countrecursive) 63 | * [some](#somecollection-callable-callback--null) 64 | * [sortBy](#sortbycollection-sortby-sortfunction--asort) 65 | * [tail](#tailcollection-from--1) 66 | * [toJson](#tojsoncollection) 67 | * [union](#unioncollectionfirst-collectionsecond) 68 | * [unzip](#unzipcollection) 69 | * [where](#wherecollection-value) 70 | * [without](#withoutcollection-without) 71 | * [zip](#zipcollectionfirst-collectionsecond) 72 | * [String](#string) 73 | * [between](#betweeninput-left-right) 74 | * [camelize](#camelizeinput-firstletteruppercase--false) 75 | * [chompLeft](#chompleftinput-prefix) 76 | * [chompRight](#chomprightinput-suffix) 77 | * [classify](#classifystring) 78 | * [collapseWhitespace](#collapsewhitespaceinput) 79 | * [contains](#containsinput-substring) 80 | * [countOccurrences](#countoccurrencesinput-substring) 81 | * [dasherize](#dasherizestring) 82 | * [endsWith](#endswithinput-substring) 83 | * [includes](#includesinput-substring) 84 | * [isAlpha](#isalphainput) 85 | * [isAlphaNumeric](#isalphanumericinput) 86 | * [isLower](#islowerinput-mb--false) 87 | * [isNumeric](#isnumericinput) 88 | * [isUpper](#isupperinput-mb--false) 89 | * [latinize](#latinizeinput) 90 | * [left](#leftstring-n) 91 | * [len](#leninput-mb--false) 92 | * [length](#lengthinput-mb--false) 93 | * [lines](#linesstring) 94 | * [lowerCaseFirst](#lowercasefirstinput) 95 | * [pad](#padstring-length-char---) 96 | * [padLeft](#padleftinput-length-char---) 97 | * [padRight](#padrightinput-length-char---) 98 | * [repeat](#repeatinput-n) 99 | * [reverse](#reverseinput) 100 | * [right](#rightstring-n) 101 | * [slugify](#slugifystring) 102 | * [startsWith](#startswithinput-substring) 103 | * [strip](#stripstring-string1) 104 | * [stripPunctuation](#strippunctuationstring) 105 | * [swapCase](#swapcasestring-mb--false) 106 | * [times](#timesinput-n) 107 | * [titleize](#tosentencearray-delimiter----lastdelimiter---and-) 108 | * [toSentence](#tosentencearray-delimiter----lastdelimiter---and-) 109 | * [toSentenceSerial](#tosentenceserialarray-delimiter----lastdelimiter--and-) 110 | * [toLower](#tolowerinput-mb--false) 111 | * [toUpper](#toupperinput-mb--false) 112 | * [truncate](#truncateinput-length-chars--) 113 | * [underscore](#underscorestring) 114 | * [upperCaseFirst](#uppercasefirstinput) 115 | * [Invoke](#invoke) 116 | * [ifCondition](#ifconditioncallable-callable-methodarguments---condition) 117 | * [ifIsset](#ifissetcallable-callable-values-key) 118 | * [ifNotEmpty](#ifnotemptycallable-callable-var) 119 | * [Object](#object) 120 | * [toArray](#toarrayobjects-valuemethod-keymethod--null) 121 | * [assignIfIsset](#assignifissetobject-property-array-key) 122 | * [Testing](#testing) 123 | * [Contributing](#contributing) 124 | * [License](#license) 125 | 126 | 127 | ## Requirements 128 | 129 | * PHP >= 5.5 130 | 131 | 132 | ## Installation 133 | 134 | Via Composer 135 | 136 | ``` bash 137 | $ composer require funct/funct 138 | ``` 139 | 140 | 141 | ## Usage 142 | 143 | The library consist of five groups: *Collection*, *Invoke*, *Object*, *Strings* and *General*. Each group has it's own namespace 144 | suffix (Only *General* uses root namespace). 145 | 146 | To include all group functions just include root namespace at the top of the file: 147 | 148 | ```PHP 149 | use Funct; 150 | ``` 151 | 152 | For single group functions you have two options. One is to include root namespace and call directly with full namespace for e.g.: 153 | 154 | ```PHP 155 | use Funct; 156 | 157 | Funct\Strings\classify('hello world'); 158 | ``` 159 | 160 | or to include only single group for e.g.: 161 | 162 | ```PHP 163 | use Funct\Strings; 164 | 165 | Strings\classify('hello world'); 166 | ``` 167 | 168 | If you are using PHP >=5.6 you can include only single function. For e.g.: 169 | 170 | ```PHP 171 | use function Funct\Strings\classify; 172 | 173 | classify('hello world'); 174 | ``` 175 | 176 | 177 | ## General 178 | 179 | ### arrayKeyNotExists($key, array $array) 180 | 181 | Checks if the given key or index exists in the array 182 | 183 | ```PHP 184 | Funct\arrayKeyNotExists(2, [1, 2]); // => true 185 | Funct\arrayKeyNotExists(1, [1, 2]); // => false 186 | ``` 187 | 188 | 189 | ### false($value) 190 | 191 | Returns true if value is false 192 | 193 | ```PHP 194 | Funct\false(false); // => true 195 | Funct\false(true); // => false 196 | ``` 197 | 198 | 199 | ### firstValue($valueA) 200 | 201 | Returns a first non null value from function arguments 202 | 203 | ```PHP 204 | Funct\firstValue('foo_bar'); // => 'foo_bar' 205 | Funct\firstValue(null, 'foo_bar'); // => 'foo_bar' 206 | Funct\firstValue(null, null, 'foo_bar'); // => 'foo_bar' 207 | ``` 208 | 209 | 210 | ### firstValueNotEmpty($valueA, $valueB) 211 | 212 | Returns a first not empty value from function arguments 213 | 214 | ```PHP 215 | Funct\firstValueNotEmpty('foo_bar'); // => 'foo_bar' 216 | Funct\firstValueNotEmpty('', 'foo_bar'); // => 'foo_bar' 217 | Funct\firstValueNotEmpty('', null, 'foo_bar'); // => 'foo_bar' 218 | ``` 219 | 220 | ### ifSetOr($value, $default) 221 | Return the first param if isset or the second one or null if it doesn't 222 | 223 | ```PHP 224 | $bar = 'bar'; 225 | Funct\ifSetOr($foo); // => 'NULL' 226 | Funct\ifSetOr($foo, 'foo_bar'); // => 'foo_bar' 227 | Funct\ifSetOr($bar, 'foo_bar'); // => 'bar' ($bar value) 228 | ``` 229 | 230 | ### notEmpty($value) 231 | 232 | Returns true if value is not empty 233 | 234 | ```PHP 235 | Funct\notEmpty('fooBar'); // => true 236 | Funct\notEmpty(''); // => false 237 | ``` 238 | 239 | 240 | ### notInArray($needle, $haystack, $strict = null) 241 | 242 | Checks if needle is not in array 243 | 244 | ```PHP 245 | Funct\notInArray(3, [0, 1, 2]); // => true 246 | Funct\notInArray(2, [0, 1, 2]); // => false 247 | ``` 248 | 249 | 250 | ### notNull($value) 251 | 252 | Returns true if value is not null 253 | 254 | ```PHP 255 | Funct\notNull('fooBar'); // => true 256 | Funct\notNull(null); // => false 257 | ``` 258 | 259 | 260 | ### null($value) 261 | 262 | Returns true if value is null 263 | 264 | 265 | ```PHP 266 | Funct\null(null); // => true 267 | Funct\null('fooBar'); // => false 268 | ``` 269 | 270 | 271 | ### tempFile($prefix = 'php') 272 | 273 | Generates temp file on systems temp folder with prefix 274 | 275 | 276 | ```PHP 277 | Funct\tempFile('php'); // => /tmp/someFile.php 278 | ``` 279 | 280 | 281 | ### true($value) 282 | 283 | Returns true if value is true 284 | 285 | 286 | ```PHP 287 | Funct\true(true); // => true 288 | Funct\true(false); // => false 289 | ``` 290 | 291 | ## Collection 292 | 293 | ### compact($collection) 294 | 295 | Returns a copy of the array with all falsy values removed 296 | 297 | 298 | ```PHP 299 | Collection\compact([0, 1, false, 2, '', 3]); // => [1, 2, 3] 300 | ``` 301 | 302 | 303 | 304 | ### countBy($collection, $callback) 305 | 306 | Sorts a array into groups and returns a count for the number of objects in each group. Similar to groupBy, but instead of 307 | returning a array of values, returns a count for the number of values in that group 308 | 309 | ```PHP 310 | Collection\countBy( 311 | [1, 2, 3, 4, 5], 312 | function ($value) { 313 | return $value % 2 == 0 ? 'even': 'odd'; 314 | } 315 | ); // => ['odd' => 3, 'even' => 2] 316 | Collection\countBy( 317 | [ 318 | ['color' => 'red', 'title' => 'Foo'], 319 | ['color' => 'red', 'title' => 'Foo'], 320 | ['color' => 'red', 'title' => 'Foo'], 321 | ['color' => 'blue', 'title' => 'Bar'], 322 | ['color' => 'blue', 'title' => 'Bar'] 323 | ], 324 | 'color' 325 | ); // => ['red' => 3, 'blue => 2] 326 | ``` 327 | 328 | 329 | ### every($collection, callable $callback = null) 330 | 331 | Returns true if all of the values in the array pass the callback truth test. 332 | 333 | ```PHP 334 | Collection\every([true, 1, null, 'yes']); // => false 335 | Collection\every([true, 1, 'yes']); // => true 336 | Collection\every( 337 | [2, 4, 6], 338 | function ($value) { 339 | return ($value % 2) === 0; 340 | } 341 | ); // => true 342 | ``` 343 | 344 | 345 | ### findWhere($collection, $value) 346 | 347 | Looks through the array and returns the first value that matches all of the key-value pairs listed in properties. 348 | 349 | 350 | ```PHP 351 | Collection\findWhere( 352 | [ 353 | ['title' => 'Book of Fooos', 'author' => 'FooBar', 'year' => 1111], 354 | ['title' => 'Cymbeline', 'author' => 'Shakespeare', 'year' => 1611], 355 | ['title' => 'The Tempest', 'author' => 'Shakespeare', 'year' => 1611], 356 | ['title' => 'Book of Foos Barrrs', 'author' => 'FooBar', 'year' => 2222], 357 | ['title' => 'Still foooing', 'author' => 'FooBar', 'year' => 3333], 358 | ['title' => 'Happy Foo', 'author' => 'FooBar', 'year' => 4444], 359 | ], 360 | ['author' => 'Shakespeare', 'year' => 1611] 361 | ); // => ['title' => 'Cymbeline', 'author' => 'Shakespeare', 'year' => 1611] 362 | ``` 363 | 364 | ### first($collection) 365 | 366 | First value of collection 367 | 368 | ```PHP 369 | Collection\first([1, 2, 3]); // => 1 370 | ``` 371 | 372 | 373 | ### firstN($collection, $n = 1) 374 | 375 | ```PHP 376 | Collection\firstN([1, 2, 3]); // => [1] 377 | Collection\firstN([1, 2, 3], 2); // => [1, 2] 378 | ``` 379 | 380 | 381 | ### flatten($collection, $depth = 1) 382 | 383 | Flattens a nested array by depth. 384 | 385 | ```PHP 386 | Collection\flatten(['a', ['b', ['c', ['d']]]]); // => ['a', 'b', ['c', ['d']]] 387 | Collection\flatten(['a', ['b', ['c', ['d']]]], 2); // => ['a', 'b', 'c', ['d']] 388 | Collection\flatten(['a', ['b', ['c', ['d']]]], 3); // => ['a', 'b', 'c', 'd'] 389 | ``` 390 | 391 | 392 | ### flattenAll($collection) 393 | 394 | Flattens all arrays to single level 395 | 396 | ```PHP 397 | Collection\flattenAll(['a', ['b', ['c', ['d']]]]); // => ['a', 'b', 'c', 'd'] 398 | ``` 399 | 400 | 401 | ### forEvery($collection, $callable) 402 | 403 | Alias of invoke($collection, $callable) 404 | 405 | 406 | ### get($collection, $key, $default = null) 407 | 408 | Returns item from collection if exists otherwise null or default value 409 | 410 | ```PHP 411 | $collection = ['red' => []]; 412 | 413 | $collection['blue'] = Collection\get($collection, 'blue', []); 414 | $collection['blue'][] = 'Hello World'; 415 | 416 | Collection\get($collection, 'red', ['empty']); 417 | ``` 418 | 419 | 420 | ### groupBy($collection, $callback) 421 | 422 | Splits a collection into sets, grouped by the result of running each value through callback. If callback is a string 423 | 424 | ```PHP 425 | Collection\groupBy([1.3, 2.1, 2.4], function($num) { return floor($num); }); // => [1 => [1.3], 2 => [2.1, 2.4]] 426 | Collection\groupBy(['one', 'two', 'three'], 'strlen'); // => [3 => ["one", "two"], 5 => ["three"]] 427 | ``` 428 | 429 | ### initial($collection, $n = 1) 430 | 431 | Returns everything but the last entry of the array. Especially useful on the arguments object. Pass n to exclude the 432 | 433 | ```PHP 434 | Collection\initial([5, 4, 3, 2, 1]); // => [5, 4, 3, 2] 435 | Collection\initial([5, 4, 3, 2, 1], 2); // => [5, 4, 3] 436 | ``` 437 | 438 | 439 | ### intersection($collectionFirst, $collectionSecond) 440 | 441 | Computes the list of values that are the intersection of all the arrays. Each value in the result is present in each 442 | 443 | ```PHP 444 | Collection\intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]); // => [1, 2] 445 | ``` 446 | 447 | ### invoke($collection, callable $callback) 448 | 449 | Invokes callback on each value in the list. Any extra arguments passed will be forwarded on to the method invocation. 450 | 451 | ```PHP 452 | Collection\invoke(['a', 'b', 'c'], 'strtoupper'); // => ['A', 'B', 'C'] 453 | ``` 454 | 455 | 456 | ### last($collection) 457 | 458 | Returns last element of array 459 | 460 | ```PHP 461 | Collection\last([1, 2, 3]); // => 3 462 | ``` 463 | 464 | ### lastIndexOf($collection, $value) 465 | 466 | Returns the index of the last occurrence of value in the array, or false if value is not present 467 | 468 | ```PHP 469 | Collecton\lastIndexOf([1, 2, 3, 1, 2, 3], 2); // => 4 470 | ``` 471 | 472 | 473 | ### lastN($collection, $n = 1) 474 | 475 | Returns the last element of an array. Passing n will return the last n elements of the array. 476 | 477 | ```PHP 478 | Collection\lastN([1, 2, 3]); // => [3] 479 | Collection\lastN([1, 2, 3], 2); // => [2, 3] 480 | ``` 481 | 482 | 483 | ### maxValue($collection, callable $callback) 484 | 485 | Returns the maximum value in collection using callback method 486 | 487 | 488 | ```PHP 489 | Collection\maxValue( 490 | [ 491 | 10 => [ 492 | 'title' => 'a', 493 | 'size' => 1 494 | ], 495 | 20 => [ 496 | 'title' => 'b', 497 | 'size' => 2 498 | ], 499 | 30 => [ 500 | 'title' => 'c', 501 | 'size' => 3 502 | ] 503 | ], 504 | function ($item) { 505 | return $item['size']; 506 | } 507 | ); // => [ 508 | 'title' => 'c', 509 | 'size' => 3 510 | ] 511 | ``` 512 | 513 | ### merge(&$a, $b) 514 | 515 | Merges all arrays to first array 516 | 517 | ```PHP 518 | $array = [1, 2]; 519 | Collection\merge($array, [3, 4], [5, 6]); 520 | 521 | $array // => [1, 2, 3, 4, 5, 6]; 522 | ``` 523 | 524 | ### minValue($collection, callable $callback) 525 | 526 | Returns the minimum value in collection using callback method 527 | 528 | ```PHP 529 | Collection\minValue( 530 | [ 531 | 10 => [ 532 | 'title' => 'a', 533 | 'size' => 1 534 | ], 535 | 20 => [ 536 | 'title' => 'b', 537 | 'size' => 2 538 | ], 539 | 30 => [ 540 | 'title' => 'c', 541 | 'size' => 3 542 | ] 543 | ], 544 | function ($item) { 545 | return $item['size']; 546 | } 547 | ); // => [ 548 | 'title' => 'a', 549 | 'size' => 1 550 | ] 551 | ``` 552 | 553 | 554 | ### pairs($collection) 555 | 556 | Convert an array into a list of [key, value] pairs. 557 | 558 | ```PHP 559 | Collection\pairs([1, 2, 3]); // => [[0, 1], [1, 2], [2, 3]] 560 | ``` 561 | 562 | 563 | ### partition($collection, callable $callback) 564 | 565 | Split array into two arrays: one whose elements all satisfy callback and one whose elements all do not satisfy 566 | 567 | 568 | ```PHP 569 | Collection\partition([1, 2, 3, 4, 5, 6, 7, 8, 9], function ($num) { return $num % 2 === 0; }); // => [[0, 2, 4, 6, 8], [1, 3, 5, 7, 9]] 570 | ``` 571 | 572 | ### pluck($collection, $key) 573 | 574 | Extract single property from array of arrays 575 | 576 | ```PHP 577 | Collection\pluck( 578 | [ 579 | [1, 2, 3], 580 | [4, 5, 6], 581 | [7, 8, 9] 582 | ], 583 | 0 584 | ); // => [1, 4, 7] 585 | ``` 586 | 587 | ### reject($collection, callable $callback) 588 | 589 | Returns the values in array without the elements that the truth test callback passes. The opposite of array_filter. 590 | 591 | ```PHP 592 | Collection\reject([1, 2, 3, 4, 5, 6], function($num) { return $num % 2 == 0; }); // => [1, 3, 5] 593 | ``` 594 | 595 | 596 | ### rest($collection, $from = 1) 597 | 598 | Returns the rest of the elements in an array. Pass an from to return the values of the array from that index onward. 599 | 600 | ```PHP 601 | Collection\rest([5, 4, 3, 2, 1]); // => [4, 3, 2, 1] 602 | ``` 603 | 604 | ### reverse($collection, $preserveNumericKeys) 605 | 606 | Reverses an array. 607 | 608 | ```PHP 609 | Collection\reverse(['a', 'b', 'c']); // ['c', 'b', 'a'] 610 | 611 | Collection\reverse(['php', 7.0, ['green', 'red']], true); // [2 => [0 => 'green', 1 => 'red'], 1 => 7.0, 0 => 'php'] 612 | 613 | ``` 614 | 615 | ### size($collection, $countRecursive) 616 | 617 | Computes the size of a collection, i.e., count all elements in a collection 618 | 619 | ```PHP 620 | Collection\size(['a', 'b', 'c']); // 3 621 | Collection\size(['a', 'b', 'c', ['d', 'e']], true); // 6 622 | ``` 623 | 624 | 625 | ### some($collection, callable $callback = null) 626 | 627 | Returns true if any of the values in the array pass the callback truth test. 628 | 629 | ```PHP 630 | Collection\some([null, 0, 'yes', false]); // => true 631 | ``` 632 | 633 | 634 | ### sortBy($collection, $sortBy, $sortFunction = 'asort') 635 | 636 | Returns a sorted array by callback function which should return value to which sort 637 | 638 | ```PHP 639 | Collection\sortBy([1, 2, 3, 4, 5, 6], function ($num) { return sin($num); }); // => [5, 4, 6, 3, 1, 2] 640 | ``` 641 | 642 | ### tail($collection, $from = 1) 643 | 644 | Alias of rest($collection, $from = 1) 645 | 646 | ### toJson($collection) 647 | 648 | Returns the JSON representation of a collection 649 | 650 | ```PHP 651 | Collection\toJson(['a' => 1, 'b' => 2, 'c' => 3]); // {"a":1,"b":2,"c":3} 652 | ``` 653 | 654 | ### union($collectionFirst, $collectionSecond) 655 | 656 | Computes the union of the passed-in arrays: the list of unique items, in order, that are present in one or more of 657 | 658 | ```PHP 659 | Collection\union([1, 2, 3], [101, 2, 1, 10], [2, 1]); // => [1, 2, 3, 101, 10] 660 | ``` 661 | 662 | ### unzip($collection) 663 | 664 | The opposite of zip. Given a number of arrays, returns a series of new arrays, the first of which contains all of 665 | 666 | ```PHP 667 | Collection\unzip([['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]]); // => ["moe", 30, true], ["larry", 40, false], ["curly", 50, false] 668 | ``` 669 | 670 | 671 | ### where($collection, $value) 672 | 673 | Looks through each value in the array, returning an array of all the values that contain all of the key-value pairs 674 | 675 | ```PHP 676 | Collection\findWhere( 677 | [ 678 | ['title' => 'Book of Fooos', 'author' => 'FooBar', 'year' => 1111], 679 | ['title' => 'Cymbeline', 'author' => 'Shakespeare', 'year' => 1611], 680 | ['title' => 'The Tempest', 'author' => 'Shakespeare', 'year' => 1611], 681 | ['title' => 'Book of Foos Barrrs', 'author' => 'FooBar', 'year' => 2222], 682 | ['title' => 'Still foooing', 'author' => 'FooBar', 'year' => 3333], 683 | ['title' => 'Happy Foo', 'author' => 'FooBar', 'year' => 4444], 684 | ], 685 | ['author' => 'Shakespeare', 'year' => 1611] 686 | ); // => [ 687 | 1 => ['title' => 'Cymbeline', 'author' => 'Shakespeare', 'year' => 1611], 688 | 2 => ['title' => 'The Tempest', 'author' => 'Shakespeare', 'year' => 1611] 689 | ] 690 | ``` 691 | 692 | 693 | ### without($collection, $without) 694 | 695 | Returns a copy of the array with all instances of the values removed. 696 | 697 | ```PHP 698 | Collection\without([1, 2, 1, 0, 3, 1, 4], 0, 1); // => [2, 3, 4] 699 | ``` 700 | 701 | 702 | ### zip($collectionFirst, $collectionSecond) 703 | 704 | Merges together the values of each of the arrays with the values at the corresponding position. 705 | 706 | ```PHP 707 | Collection\zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]); // => [["moe", 30, true], ["larry", 40, false], ["curly", 50, false]] 708 | ``` 709 | 710 | ## String 711 | 712 | ### between($input, $left, $right) 713 | 714 | Extracts the string between two substrings 715 | 716 | ```PHP 717 | Strings\between('foo', '', ''); // => 'foo' 718 | Strings\between('foo', '', ''); // => 'foo' 719 | Strings\between('foo', '', ''); // => 'foo' 720 | Strings\between('foo', '', ''); // => '' 721 | Strings\between('Some strings } are very {weird}, dont you think?', '{', '}'); // => 'weird' 722 | Strings\between('This is a test string', 'test'); // => ' string' 723 | Strings\between('This is a test string', '', 'test'); // => 'This is a ' 724 | ``` 725 | 726 | ### camelize($input, $firstLetterUppercase = false) 727 | 728 | Camelizes string 729 | 730 | ```PHP 731 | Strings\camelize('data_rate'); //'dataRate' 732 | Strings\camelize('background-color'); //'backgroundColor' 733 | Strings\camelize('-moz-something'); //'mozSomething' 734 | Strings\camelize('_car_speed_'); //'carSpeed' 735 | Strings\camelize('yes_we_can'); //'yesWeCan'Strings\camelize( 736 | ``` 737 | 738 | ### chompLeft($input, $prefix) 739 | 740 | Removes prefix from start of string 741 | 742 | ```PHP 743 | Strings\chompLeft('foobar', 'foo'); //'bar' 744 | Strings\chompLeft('foobar', 'bar'); //'foobar' 745 | ``` 746 | 747 | ### chompRight($input, $suffix) 748 | 749 | Removes suffix from end of string 750 | 751 | ```PHP 752 | Strings\chompRight('foobar', 'bar'); // => 'foo' 753 | Strings\chompRight('foobar', 'foo'); // => 'foobar' 754 | ``` 755 | 756 | ### classify($string) 757 | 758 | Converts string to camelized class name. First letter is always upper case 759 | 760 | ```PHP 761 | Strings\classify('className'); // => ClassName 762 | ``` 763 | 764 | 765 | ### collapseWhitespace($input) 766 | 767 | Collapse multiple spaces 768 | 769 | ```PHP 770 | Strings\collapseWhitespace(" String \t libraries are \n\n\t fun\n! "); // => 'String libraries are fun !' 771 | ``` 772 | 773 | 774 | ### contains($input, $substring) 775 | 776 | Check if string contains substring 777 | 778 | ```PHP 779 | Strings\contains('PHP is one of the best languages!', 'one'); // => true 780 | ``` 781 | 782 | ### countOccurrences($input, $substring) 783 | 784 | Count the occurrences of substring in string 785 | 786 | ```PHP 787 | Strings\countOccurrences('AN likes to program. AN does not play in the NBA.', "AN"); // => 2 788 | Strings\countOccurrences('Does not exist.', "Flying Spaghetti Monster"); // => 0 789 | Strings\countOccurrences('Does not exist.', "Bigfoot"); // => 0 790 | Strings\countOccurrences('PHP is fun, therefore Node.js is fun', "fun"); // => 2 791 | Strings\countOccurrences('funfunfun', "fun"); // => 3 792 | ``` 793 | 794 | ### dasherize($string) 795 | 796 | Converts hyphens and camel casing to dashes 797 | 798 | ```PHP 799 | Strings\dasherize('dataRate'); // => 'data-rate' 800 | Strings\dasherize('CarSpeed'); // => 'car-speed' 801 | Strings\dasherize('yesWeCan'); // => 'yes-we-can' 802 | Strings\dasherize('backgroundColor'); // => 'background-color' 803 | ``` 804 | 805 | ### endsWith($input, $substring) 806 | 807 | Check if string ends with substring 808 | 809 | ```PHP 810 | Strings\endsWith("hello jon", 'jon'); // => true 811 | ``` 812 | 813 | ### includes($input, $substring) 814 | 815 | Alias of contains 816 | 817 | ### isAlpha($input) 818 | 819 | Check if string contains only letters 820 | 821 | ```PHP 822 | Strings\isAlpha("afaf"); // => true 823 | Strings\isAlpha('fdafaf3'); // => false 824 | Strings\isAlpha('dfdf--dfd'); // => false 825 | ``` 826 | 827 | ### isAlphaNumeric($input) 828 | 829 | Check if string contains only alphanumeric 830 | 831 | ```PHP 832 | Strings\isAlphaNumeric("afaf35353afaf"); // => true 833 | Strings\isAlphaNumeric("FFFF99fff"); // => true 834 | Strings\isAlphaNumeric("99"); // => true 835 | Strings\isAlphaNumeric("afff"); // => true 836 | Strings\isAlphaNumeric("Infinity"); // => true 837 | Strings\isAlphaNumeric("-Infinity"); // => false 838 | Strings\isAlphaNumeric("-33"); // => false 839 | Strings\isAlphaNumeric("aaff.."); // => false 840 | ``` 841 | 842 | ### isLower($input, $mb = false) 843 | 844 | Checks if letters in given string are all lowercase. 845 | 846 | ```PHP 847 | Strings\isLower('a'); // => true 848 | Strings\isLower('z'); // => true 849 | Strings\isLower('B'); // => false 850 | Strings\isLower('hiAN'); // => true 851 | Strings\isLower('hi AN'); // => false 852 | Strings\isLower('HelLO'); // => false 853 | ``` 854 | 855 | ### isNumeric($input) 856 | 857 | Check if string contains only digits 858 | 859 | ```PHP 860 | Strings\isNumeric("3"); // => true 861 | Strings\isNumeric("34.22"); // => false 862 | Strings\isNumeric("-22.33"); // => false 863 | Strings\isNumeric("NaN"); // => false 864 | Strings\isNumeric("Infinity"); // => false 865 | Strings\isNumeric("-Infinity"); // => false 866 | Strings\isNumeric("AN"); // => false 867 | Strings\isNumeric("-5"); // => false 868 | Strings\isNumeric("000992424242"); // => true 869 | ``` 870 | 871 | ### isUpper($input, $mb = false) 872 | 873 | Checks if letters in given string are all uppercase. 874 | 875 | ```PHP 876 | Strings\isUpper('a'); // => false 877 | Strings\isUpper('z'); // => false 878 | Strings\isUpper('B'); // => true 879 | Strings\isUpper('HIAN'); // => true 880 | Strings\isUpper('HI AN'); // => false 881 | Strings\isUpper('HelLO'); // => true 882 | ``` 883 | 884 | ### latinize($input) 885 | 886 | Remove accents from latin characters 887 | 888 | ```PHP 889 | Strings\latinize('crème brûlée'); // => 'creme brulee' 890 | ``` 891 | 892 | ### left($string, $n) 893 | 894 | Return the substring denoted by n positive left-most characters 895 | 896 | ```PHP 897 | Strings\left('My name is AN', 2); // => 'My' 898 | Strings\left('Hi', 0); // => '' 899 | Strings\left('My name is AN', -2); // => 'AN', same as right(2) 900 | ``` 901 | 902 | ### len($input, $mb = false) 903 | 904 | Alias of length($input, $mb = false); 905 | 906 | 907 | ### length($input, $mb = false) 908 | 909 | Get string length. 910 | 911 | ```PHP 912 | Strings\length('rod'); // 3 913 | Strings\length('marçal'); // 7 914 | Strings\length('marçal', true); // 6 915 | ``` 916 | 917 | ### lines($string) 918 | 919 | Returns an array with the lines. Cross-platform compatible 920 | 921 | 922 | ```PHP 923 | Strings\lines("My name is AN\nPHP is my fav language\r\nWhat is your fav language?"); // => [ 'My name is AN', 924 | 'PHP is my fav language', 925 | 'What is your fav language?' ] 926 | ``` 927 | 928 | ### lowerCaseFirst($input) 929 | 930 | Converts string first char to lowercase 931 | 932 | ```PHP 933 | Strings\lowerCaseFirst('HelloWorld'); // => 'helloWorld 934 | ``` 935 | 936 | ### pad($string, $length, $char = ' ') 937 | 938 | Pads the string in the center with specified character. char may be a string or a number, defaults is a space 939 | 940 | ```PHP 941 | Strings\pad('hello', 5); // 'hello' 942 | Strings\pad('hello', 10); // ' hello ' 943 | Strings\pad('hey', 7); // ' hey ' 944 | Strings\pad('hey', 5); // ' hey ' 945 | Strings\pad('hey', 4); // ' hey' 946 | Strings\pad('hey', 7, '-');// '--hey--' 947 | ``` 948 | 949 | ### padLeft($input, $length, $char = ' ') 950 | 951 | Left pads the string 952 | 953 | ```PHP 954 | Strings\padLeft('hello', 5); // => 'hello' 955 | Strings\padLeft('hello', 10); // => ' hello' 956 | Strings\padLeft('hello', 7); // => ' hello' 957 | Strings\padLeft('hello', 6); // => ' hello' 958 | Strings\padLeft('hello', 10, '.'); // => '.....hello' 959 | ``` 960 | 961 | ### padRight($input, $length, $char = ' ') 962 | 963 | Right pads the string 964 | 965 | ```PHP 966 | Strings\padRight('hello', 5); // => 'hello' 967 | Strings\padRight('hello', 10); // => 'hello ' 968 | Strings\padRight('hello', 7); // => 'hello ' 969 | Strings\padRight('hello', 6); // => 'hello ' 970 | Strings\padRight('hello', 10, '.'); // => 'hello.....' 971 | ``` 972 | 973 | ### repeat($input, $n) 974 | 975 | Alias times($input, $n) 976 | 977 | 978 | ### reverse($input) 979 | 980 | Reverses a string 981 | 982 | ```PHP 983 | Strings\reverse('hello world'); // => dlrow olleh 984 | ``` 985 | 986 | ### right($string, $n) 987 | 988 | Return the substring denoted by n positive right-most characters 989 | 990 | ```PHP 991 | Strings\right('I AM CRAZY', 2); // => 'ZY' 992 | Strings\right('Does it work? ', 4); // => 'k? ' 993 | Strings\right('Hi', 0); // => '' 994 | Strings\right('My name is AN', -2); // => 'My', same as left(2) 995 | ``` 996 | 997 | ### slugify($string) 998 | 999 | Converts the text into a valid url slug. Removes accents from Latin characters 1000 | 1001 | ```PHP 1002 | Strings\slugify('Global Thermonuclear Warfare'); // => 'global-thermonuclear-warfare' 1003 | Strings\slugify('Crème brûlée'); // => 'creme-brulee' 1004 | ``` 1005 | 1006 | ### startsWith($input, $substring) 1007 | 1008 | Check if string starts with substring 1009 | 1010 | ```PHP 1011 | Strings\startsWith("AN is a software engineer", "AN"); // => true 1012 | Strings\startsWith('wants to change the world', "politicians"); // => false 1013 | ``` 1014 | 1015 | ### strip($string, $string1) 1016 | 1017 | Returns a new string with all occurrences of [string1],[string2],... removed. 1018 | 1019 | ```PHP 1020 | Strings\strip(' 1 2 3--__--4 5 6-7__8__9--0', ' ', '_', '-'); // => '1234567890' 1021 | Strings\strip('can words also be stripped out?', 'words', 'also', 'be'); // => 'can stripped out?' 1022 | ``` 1023 | 1024 | ### stripPunctuation($string) 1025 | 1026 | Strip all of the punctuation 1027 | 1028 | ```PHP 1029 | Strings\stripPunctuation('My, st[ring] *full* of %punct)'); // => 'My string full of punct' 1030 | ``` 1031 | 1032 | ### swapCase($string, $mb = false) 1033 | 1034 | Returns a case swapped version of the string 1035 | 1036 | ```PHP 1037 | Strings\swapCase('RoD eLIas'); // rOd EliAS 1038 | ``` 1039 | 1040 | ### times($input, $n) 1041 | 1042 | Repeat the string n times 1043 | 1044 | ```PHP 1045 | Strings\times(' ', 3); // => ' ' 1046 | Strings\times('*', 3); // => '***' 1047 | ``` 1048 | 1049 | ### titleize($string, array $ignore = []) 1050 | 1051 | Creates a title version of the string. Capitalizes all the words and replaces some characters in the string to 1052 | 1053 | ```PHP 1054 | Strings\titleize('hello world'); // => 'Hello World' 1055 | ``` 1056 | 1057 | ### toSentence($array, $delimiter = ', ', $lastDelimiter = ' and ') 1058 | 1059 | Join an array into a human readable sentence 1060 | 1061 | ```PHP 1062 | Strings\toSentence(["A", "B", "C"]); // => "A, B and C"; 1063 | Strings\toSentence(["A", "B", "C"], ", ", " ir "); // => "A, B ir C"; 1064 | ``` 1065 | 1066 | ### toSentenceSerial($array, $delimiter = ', ', $lastDelimiter = 'and ') 1067 | 1068 | The same as string_to_sentence, but adjusts delimeters to use Serial comma) 1069 | 1070 | ```PHP 1071 | Strings\toSentenceSerial(["A", "B"]); // => "A and B" 1072 | Strings\toSentenceSerial(["A", "B", "C"]); // => "A, B, and C" 1073 | Strings\toSentenceSerial(["A", "B", "C"], ", ", " unt "); // => "jQuery, Mootools, unt Prototype" 1074 | ``` 1075 | 1076 | ### toLower($input, $mb = false) 1077 | 1078 | Makes a string lowercase; 1079 | 1080 | ```PHP 1081 | Strings\toLower('ROD ELIAS'); // rod elias 1082 | ``` 1083 | 1084 | ### toUpper($input, $mb = false) 1085 | 1086 | Makes a string uppercase; 1087 | 1088 | ```PHP 1089 | Strings\toUpper('rod elias'); // ROD ELIAS 1090 | ``` 1091 | 1092 | ### truncate($input, $length, $chars = '…') 1093 | 1094 | Truncate string accounting for word placement and character count 1095 | 1096 | ```PHP 1097 | Strings\truncate('this is some long text', 3); // => '...' 1098 | Strings\truncate('this is some long text', 7); // => 'this is...' 1099 | Strings\truncate('this is some long text', 11); // => 'this is...' 1100 | Strings\truncate('this is some long text', 12); // => 'this is some...' 1101 | Strings\truncate('this is some long text', 11); // => 'this is...' 1102 | Strings\truncate('this is some long text', 14, ' read more'); // => 'this is some read more' 1103 | ``` 1104 | 1105 | ### underscore($string) 1106 | 1107 | Converts hyphens and camel casing to underscores 1108 | 1109 | ```PHP 1110 | Strings\underscore('dataRate'); // => 'data_rate' 1111 | Strings\underscore('CarSpeed'); // => 'car_speed' 1112 | Strings\underscore('yesWeCan'); // => 'yes_we_can' 1113 | ``` 1114 | 1115 | ### upperCaseFirst($input) 1116 | 1117 | Converts string first char to uppercase 1118 | 1119 | ```PHP 1120 | Strings\upperCaseFirst('helloWorld'); // => 'HelloWorld 1121 | ``` 1122 | 1123 | 1124 | ## Invoke 1125 | 1126 | ### ifCondition(callable $callable, $methodArguments = [], $condition) 1127 | 1128 | Invoke a method if condition is true 1129 | 1130 | ```PHP 1131 | Invoke\ifCondition(function () { echo 'Hello World'; }, [], Funct\notEmpty('Hello?')); // => Hello World 1132 | ``` 1133 | 1134 | ### ifIsset(callable $callable, $values, $key) 1135 | 1136 | Invoke a method if value isset 1137 | 1138 | ```PHP 1139 | Invoke\ifIsset(function () { echo 'Hello World'; }, ['Hello' = > 1000], 'Hello'); // => Hello World 1140 | ``` 1141 | 1142 | 1143 | ### ifNotEmpty(callable $callable, $var) 1144 | 1145 | Invoke a method if value is not empty 1146 | 1147 | ```PHP 1148 | Invoke\ifNotEmpty(function () { echo 'Hello World'; }, 'Hello'); // => Hello World 1149 | ``` 1150 | 1151 | ## Object 1152 | 1153 | ### toArray($objects, $valueMethod, $keyMethod = null) 1154 | 1155 | Creates array from objects using valueMethod as value and with/without keyMethod as key 1156 | 1157 | ```PHP 1158 | Object\toArray($objects, 'getValue', 'getkey'); // => ['key' => 'value'] 1159 | ``` 1160 | 1161 | ### assignIfIsset($object, $property, $array, $key) 1162 | 1163 | Assign value to object from array if key exists 1164 | 1165 | ```PHP 1166 | $array = ['bar' => 'foobar']; 1167 | 1168 | Object\assignIfIsset($object, 'foo', $array, 'bar'); // => $object->foo = 'foobar' 1169 | ``` 1170 | 1171 | ## Testing 1172 | 1173 | ``` bash 1174 | $ composer test 1175 | ``` 1176 | 1177 | 1178 | ## Contributing 1179 | 1180 | Please see [CONTRIBUTING](CONTRIBUTING.md) and [CONDUCT](CONDUCT.md) for details. 1181 | 1182 | 1183 | ## License 1184 | 1185 | Please see [License File](LICENSE) for more information. 1186 | 1187 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "funct/funct", 3 | "description": "A PHP library with commonly used code blocks", 4 | "authors": [ 5 | { 6 | "name": "Aurimas Niekis", 7 | "email": "aurimas@niekis.lt" 8 | } 9 | ], 10 | "type": "library", 11 | "license": "MIT", 12 | "require-dev": { 13 | "phpunit/phpunit": "^4.0" 14 | }, 15 | "autoload": { 16 | "psr-4": { 17 | "Funct\\": "src" 18 | }, 19 | "files": [ 20 | "src/Collection.php", 21 | "src/General.php", 22 | "src/Invoke.php", 23 | "src/Object.php", 24 | "src/Strings.php" 25 | ] 26 | }, 27 | "scripts": { 28 | "test": "vendor/bin/phpunit", 29 | "test-ci": "vendor/bin/phpunit -c phpunit.xml.ci" 30 | }, 31 | "minimum-stability": "dev", 32 | "extra": { 33 | "branch-alias": { 34 | "dev-master": "1.0-dev" 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/Collection.php: -------------------------------------------------------------------------------- 1 | 14 | */ 15 | function compact($collection) 16 | { 17 | return array_filter($collection); 18 | } 19 | 20 | /** 21 | * Sorts a array into groups and returns a count for the number of objects in each group. Similar to groupBy, but 22 | * instead of returning a array of values, returns a count for the number of values in that group. 23 | * 24 | * @param array $collection 25 | * @param callable|string $callback 26 | * 27 | * @return array 28 | * @author Aurimas Niekis 29 | */ 30 | function countBy($collection, $callback) 31 | { 32 | $result = []; 33 | 34 | foreach ($collection as $key => $value) { 35 | if (is_callable($callback)) { 36 | $groupName = call_user_func($callback, $value); 37 | } else { 38 | $groupName = $value[$callback]; 39 | } 40 | 41 | $result[$groupName] = get($result, $groupName, 0); 42 | $result[$groupName]++; 43 | } 44 | 45 | return $result; 46 | } 47 | 48 | 49 | /** 50 | * Returns true if all of the values in the array pass the callback truth test. 51 | * 52 | * @author Aurimas Niekis 53 | * 54 | * @param array $collection 55 | * @param callable $callback 56 | * 57 | * @return bool 58 | */ 59 | function every($collection, callable $callback = null) 60 | { 61 | if (null === $callback) { 62 | $callback = function ($item) use ($callback) { 63 | return (true == $item); 64 | }; 65 | } 66 | 67 | return count( 68 | array_filter( 69 | $collection, 70 | function ($item) use ($callback) { 71 | return false === call_user_func($callback, $item); 72 | } 73 | ) 74 | ) < 1; 75 | } 76 | 77 | 78 | /** 79 | * Looks through the array and returns the first value that matches all of the key-value pairs listed in properties. 80 | * 81 | * @param array $collection 82 | * @param array $value 83 | * 84 | * @return array 85 | * @author Aurimas Niekis 86 | */ 87 | function findWhere($collection, $value) 88 | { 89 | foreach ($collection as $key => $item) { 90 | $diff = array_diff_assoc($value, $item); 91 | 92 | if (count($diff) < 1) { 93 | return $item; 94 | } 95 | } 96 | } 97 | 98 | /** 99 | * @param array|Traversable $collection 100 | * 101 | * @return array 102 | */ 103 | function first($collection) 104 | { 105 | return reset($collection); 106 | } 107 | 108 | /** 109 | * @param array|Traversable $collection 110 | * @param int $n First n elements of array 111 | * 112 | * @return array 113 | */ 114 | function firstN($collection, $n = 1) 115 | { 116 | return array_slice($collection, 0, $n); 117 | } 118 | 119 | /** 120 | * Flattens a nested array by depth. 121 | * 122 | * @param array $collection 123 | * @param int $depth 124 | * 125 | * @return array 126 | * @author Aurimas Niekis 127 | */ 128 | function flatten($collection, $depth = 1) 129 | { 130 | $result = []; 131 | 132 | foreach ($collection as $value) { 133 | if (is_array($value) && $depth > 0) { 134 | $result = array_merge($result, flatten($value, $depth - 1)); 135 | } else { 136 | $result[] = $value; 137 | } 138 | } 139 | 140 | return $result; 141 | } 142 | 143 | /** 144 | * Flattens all arrays to single level 145 | * 146 | * @param array $collection 147 | * 148 | * @return array 149 | * @author Aurimas Niekis 150 | */ 151 | function flattenAll($collection) 152 | { 153 | $result = []; 154 | 155 | foreach ($collection as $value) { 156 | if (is_array($value)) { 157 | $result = array_merge($result, flattenAll($value)); 158 | } else { 159 | $result[] = $value; 160 | } 161 | } 162 | 163 | return $result; 164 | } 165 | 166 | 167 | /** 168 | * Runs a callback for every value in collection with value as first argument and additional arguments and returns 169 | * array of results 170 | * 171 | * @param array|Traversable $collection 172 | * @param callable $callable 173 | * 174 | * @return array 175 | * @author Aurimas Niekis 176 | */ 177 | function forEvery($collection, $callable) 178 | { 179 | return call_user_func_array('Funct\\Collection\\invoke', func_get_args()); 180 | } 181 | 182 | 183 | /** 184 | * Returns item from collection if exists otherwise null or default value 185 | * 186 | * @author Aurimas Niekis 187 | * 188 | * @param array|Traversable $collection 189 | * @param string $key 190 | * @param mixed $default 191 | * 192 | * @return mixed 193 | */ 194 | function get($collection, $key, $default = null) 195 | { 196 | if (isset($collection[$key])) { 197 | return $collection[$key]; 198 | } else { 199 | return $default; 200 | } 201 | } 202 | 203 | 204 | /** 205 | * Splits a collection into sets, grouped by the result of running each value through callback. If callback is a string 206 | * instead of a function, groups by the property named by callback on each of the values. 207 | * 208 | * @param array $collection 209 | * @param callable|string $callback 210 | * 211 | * @return array 212 | * @author Aurimas Niekis 213 | */ 214 | function groupBy($collection, $callback) 215 | { 216 | $result = []; 217 | 218 | foreach ($collection as $key => $value) { 219 | if (is_callable($callback)) { 220 | $groupName = call_user_func($callback, $value); 221 | } else { 222 | $groupName = $value[$callback]; 223 | } 224 | 225 | $result[$groupName] = get($result, $groupName, []); 226 | $result[$groupName][$key] = $value; 227 | } 228 | 229 | return $result; 230 | } 231 | 232 | 233 | /** 234 | * Returns everything but the last entry of the array. Especially useful on the arguments object. Pass n to exclude the 235 | * last n elements from the result. 236 | * 237 | * @param array $collection 238 | * @param int $n 239 | * 240 | * @return array 241 | * @author Aurimas Niekis 242 | */ 243 | function initial($collection, $n = 1) 244 | { 245 | return array_slice($collection, 0, -$n); 246 | } 247 | 248 | 249 | /** 250 | * Computes the list of values that are the intersection of all the arrays. Each value in the result is present in each 251 | * of the arrays. 252 | * 253 | * @param array $collectionFirst 254 | * @param array $collectionSecond 255 | * 256 | * @return array 257 | * @author Aurimas Niekis 258 | */ 259 | function intersection($collectionFirst, $collectionSecond) 260 | { 261 | return call_user_func_array('array_intersect', func_get_args()); 262 | } 263 | 264 | 265 | /** 266 | * Invokes callback on each value in the list. Any extra arguments passed will be forwarded on to the method invocation. 267 | * 268 | * @param array $collection 269 | * @param callable $callback 270 | * 271 | * @return array 272 | * @author Aurimas Niekis 273 | */ 274 | function invoke($collection, callable $callback) 275 | { 276 | $arguments = func_get_args(); 277 | 278 | return array_map( 279 | function ($item) use ($callback, $arguments) { 280 | $arguments = array_merge([$item], array_slice($arguments, 2)); 281 | 282 | return call_user_func_array($callback, $arguments); 283 | }, 284 | $collection 285 | ); 286 | } 287 | 288 | 289 | /** 290 | * Returns last element of array 291 | * 292 | * @param array|Traversable $collection 293 | * 294 | * @return array 295 | */ 296 | function last($collection) 297 | { 298 | return end($collection); 299 | } 300 | 301 | 302 | /** 303 | * Returns the index of the last occurrence of value in the array, or false if value is not present 304 | * 305 | * @param array $collection 306 | * @param mixed $value 307 | * 308 | * @return int|bool 309 | * @author Aurimas Niekis 310 | */ 311 | function lastIndexOf($collection, $value) 312 | { 313 | $result = array_keys($collection, $value); 314 | 315 | if (count($result) < 1) { 316 | return false; 317 | } 318 | 319 | return end($result); 320 | } 321 | 322 | 323 | /** 324 | * Returns the last element of an array. Passing n will return the last n elements of the array. 325 | * 326 | * @param array|Traversable $collection 327 | * @param int $n Last n elements of array 328 | * 329 | * @return array 330 | */ 331 | function lastN($collection, $n = 1) 332 | { 333 | return array_slice($collection, (-1 * $n)); 334 | } 335 | 336 | 337 | /** 338 | * Returns the maximum value in collection using callback method 339 | * 340 | * @param array $collection 341 | * @param callable $callback 342 | * 343 | * @return array 344 | * @author Aurimas Niekis 345 | */ 346 | function maxValue($collection, callable $callback) 347 | { 348 | $values = array_map($callback, $collection); 349 | $keys = array_flip($values); 350 | 351 | return $collection[$keys[max($values)]]; 352 | } 353 | 354 | 355 | /** 356 | * Merges all arrays to first array 357 | * 358 | * @param array|Traversable $a 359 | * @param array|Traversable $b 360 | * 361 | * @author Aurimas Niekis 362 | */ 363 | function merge(&$a, $b) 364 | { 365 | $a = call_user_func_array('array_merge', func_get_args()); 366 | } 367 | 368 | 369 | /** 370 | * Returns the minimum value in collection using callback method 371 | * 372 | * @param array $collection 373 | * @param callable $callback 374 | * 375 | * @return array 376 | * @author Aurimas Niekis 377 | */ 378 | function minValue($collection, callable $callback) 379 | { 380 | $values = array_map($callback, $collection); 381 | $keys = array_flip($values); 382 | 383 | return $collection[$keys[min($values)]]; 384 | } 385 | 386 | 387 | /** 388 | * Convert an array into a list of [key, value] pairs. 389 | * 390 | * @param array $collection 391 | * 392 | * @return array 393 | * @author Aurimas Niekis 394 | */ 395 | function pairs($collection) 396 | { 397 | return array_map( 398 | function ($key, $value) { 399 | return [$key, $value]; 400 | }, 401 | array_keys($collection), 402 | $collection 403 | ); 404 | } 405 | 406 | 407 | /** 408 | * Split array into two arrays: one whose elements all satisfy callback and one whose elements all do not satisfy 409 | * callback. 410 | * 411 | * @param array $collection 412 | * @param callable $callback 413 | * 414 | * @return array 415 | * @author Aurimas Niekis 416 | */ 417 | function partition($collection, callable $callback) 418 | { 419 | $resultA = []; 420 | $resultB = []; 421 | 422 | foreach ($collection as $key => $value) { 423 | if (call_user_func($callback, $value, $key)) { 424 | $resultA[$key] = $value; 425 | } else { 426 | $resultB[$key] = $value; 427 | } 428 | } 429 | 430 | return [$resultA, $resultB]; 431 | } 432 | 433 | 434 | /** 435 | * Extract single property from array of arrays 436 | * 437 | * @param array $collection 438 | * @param string $key 439 | * 440 | * @return array 441 | * @author Aurimas Niekis 442 | */ 443 | function pluck($collection, $key) 444 | { 445 | return array_map( 446 | function ($item) use ($key) { 447 | return get($item, $key); 448 | }, 449 | $collection 450 | ); 451 | } 452 | 453 | /** 454 | * Returns the values in array without the elements that the truth test callback passes. The opposite of array_filter. 455 | * 456 | * @param array $collection 457 | * @param callable $callback 458 | * 459 | * @return array 460 | * @author Aurimas Niekis 461 | */ 462 | function reject($collection, callable $callback) 463 | { 464 | return array_filter($collection, function ($item) use ($callback) { 465 | return false === call_user_func($callback, $item); 466 | }); 467 | } 468 | 469 | /** 470 | * Returns the rest of the elements in an array. Pass an from to return the values of the array from that index onward. 471 | * 472 | * @param array $collection 473 | * @param int $from 474 | * 475 | * @return array 476 | * @author Aurimas Niekis 477 | */ 478 | function rest($collection, $from = 1) 479 | { 480 | return array_slice($collection, $from); 481 | } 482 | 483 | /** 484 | * Returns a reversed array 485 | * 486 | * @param array $collection 487 | * @param boolean $preserveNumericKeys 488 | * 489 | * @return array 490 | * @author Rod Elias 491 | */ 492 | function reverse($collection, $preserveNumericKeys = false) 493 | { 494 | return array_reverse($collection, $preserveNumericKeys); 495 | } 496 | 497 | /** 498 | * Computes the size of a collection 499 | * 500 | * @param array $collection collection 501 | * @param boolean $countRecursive count or not to count recursively 502 | * 503 | * @return int number of elements in a collection 504 | * @author Rod Elias 505 | */ 506 | function size($collection, $countRecursive = false) 507 | { 508 | return \count($collection, $countRecursive); 509 | } 510 | 511 | /** 512 | * Returns true if any of the values in the array pass the callback truth test. 513 | * 514 | * @author Aurimas Niekis 515 | * 516 | * @param array $collection 517 | * @param callable $callback 518 | * 519 | * @return bool 520 | */ 521 | function some($collection, callable $callback = null) 522 | { 523 | if (null === $callback) { 524 | $callback = function ($item) use ($callback) { 525 | return (true == $item); 526 | }; 527 | } 528 | 529 | foreach ($collection as $item) { 530 | if (call_user_func($callback, $item)) { 531 | return true; 532 | } 533 | } 534 | 535 | return false; 536 | } 537 | 538 | 539 | /** 540 | * Returns a sorted array by callback function which should return value to which sort 541 | * 542 | * @param array $collection 543 | * @param callable|string $sortBy 544 | * @param string $sortFunction 545 | * 546 | * @return array 547 | * @author Aurimas Niekis 548 | */ 549 | function sortBy($collection, $sortBy, $sortFunction = 'asort') 550 | { 551 | if (false === is_callable($sortBy)) { 552 | $sortBy = function ($item) use ($sortBy) { 553 | return $item[$sortBy]; 554 | }; 555 | } 556 | 557 | $values = array_map($sortBy, $collection); 558 | $sortFunction($values); 559 | 560 | $result = []; 561 | foreach ($values as $key => $value) { 562 | $result[$key] = $collection[$key]; 563 | } 564 | 565 | return $result; 566 | } 567 | 568 | /** 569 | * @see collection_rest 570 | * 571 | * @param array $collection 572 | * @param int $from 573 | * 574 | * @return array 575 | * @author Aurimas Niekis 576 | */ 577 | function tail($collection, $from = 1) 578 | { 579 | return rest($collection, $from); 580 | } 581 | 582 | /** 583 | * Returns the JSON representation of a collection 584 | * @param array $collection 585 | * @return string string containing the JSON representation of $collection 586 | * @author Rod Elias 587 | */ 588 | function toJson($collection) 589 | { 590 | return json_encode($collection); 591 | } 592 | 593 | /** 594 | * Computes the union of the passed-in arrays: the list of unique items, in order, that are present in one or more of 595 | * the arrays. 596 | * 597 | * @param array $collectionFirst 598 | * @param array $collectionSecond 599 | * 600 | * @return array 601 | * @author Aurimas Niekis 602 | */ 603 | function union($collectionFirst, $collectionSecond) 604 | { 605 | $result = call_user_func_array('array_merge', func_get_args()); 606 | 607 | return array_unique($result); 608 | } 609 | 610 | 611 | /** 612 | * The opposite of zip. Given a number of arrays, returns a series of new arrays, the first of which contains all of 613 | * the first elements in the input arrays, the second of which contains all of the second elements, and so on. 614 | * 615 | * @param array $collection 616 | * 617 | * @return array 618 | * @author Aurimas Niekis 619 | */ 620 | function unzip($collection) 621 | { 622 | $length = count(max($collection, 'count')); 623 | $result = []; 624 | 625 | for ($i = 0; $i < $length; $i++) { 626 | $result[$i] = pluck($collection, $i); 627 | } 628 | 629 | return $result; 630 | } 631 | 632 | 633 | /** 634 | * Looks through each value in the array, returning an array of all the values that contain all of the key-value pairs 635 | * listed in properties. 636 | * 637 | * @param array $collection 638 | * @param array $value 639 | * 640 | * @return array 641 | * @author Aurimas Niekis 642 | */ 643 | function where($collection, $value) 644 | { 645 | $result = []; 646 | 647 | foreach ($collection as $key => $item) { 648 | $diff = array_diff_assoc($value, $item); 649 | 650 | if (count($diff) < 1) { 651 | $result[$key] = $item; 652 | } 653 | } 654 | 655 | return $result; 656 | } 657 | 658 | 659 | /** 660 | * Returns a copy of the array with all instances of the values removed. 661 | * 662 | * 663 | * @param array $collection 664 | * @param array $without 665 | * 666 | * @return array 667 | * @author Aurimas Niekis 668 | */ 669 | function without($collection, $without) 670 | { 671 | $without = func_get_args(); 672 | array_shift($without); 673 | 674 | return array_diff($collection, $without); 675 | } 676 | 677 | 678 | /** 679 | * Merges together the values of each of the arrays with the values at the corresponding position. 680 | * 681 | * @param array $collectionFirst 682 | * @param array $collectionSecond 683 | * 684 | * @return array 685 | * @author Aurimas Niekis 686 | */ 687 | function zip($collectionFirst, $collectionSecond) 688 | { 689 | return unzip(func_get_args()); 690 | } 691 | -------------------------------------------------------------------------------- /src/General.php: -------------------------------------------------------------------------------- 1 | 14 | */ 15 | function arrayKeyNotExists($key, array $array) 16 | { 17 | return false === array_key_exists($key, $array); 18 | } 19 | 20 | 21 | /** 22 | * Returns true if value is false 23 | * 24 | * @param mixed $value 25 | * 26 | * @return bool 27 | * @author Aurimas Niekis 28 | */ 29 | function false($value) 30 | { 31 | return false === $value; 32 | } 33 | 34 | 35 | /** 36 | * Returns a first non null value from function arguments 37 | * 38 | * @param mixed $valueA 39 | * 40 | * @return mixed 41 | * @author Aurimas Niekis 42 | */ 43 | function firstValue($valueA) 44 | { 45 | foreach (func_get_args() as $arg) { 46 | if (null !== $arg) { 47 | return $arg; 48 | } 49 | } 50 | } 51 | 52 | 53 | /** 54 | * Returns a first not empty value from function arguments 55 | * 56 | * @param mixed $valueA 57 | * @param mixed $valueB 58 | * 59 | * @return mixed 60 | * @author Aurimas Niekis 61 | */ 62 | function firstValueNotEmpty($valueA, $valueB) 63 | { 64 | foreach (func_get_args() as $arg) { 65 | if (notEmpty($arg)) { 66 | return $arg; 67 | } 68 | } 69 | } 70 | 71 | 72 | /** 73 | * Returns true if var is not empty 74 | * 75 | * @param mixed $value 76 | * 77 | * @return bool 78 | * @author Aurimas Niekis 79 | */ 80 | function notEmpty($value) 81 | { 82 | return false === empty($value); 83 | } 84 | 85 | 86 | /** 87 | * Checks if needle is not in array 88 | * 89 | * @param $needle 90 | * @param $haystack 91 | * @param null $strict 92 | * 93 | * @return bool 94 | * @author Aurimas Niekis 95 | */ 96 | function notInArray($needle, $haystack, $strict = null) 97 | { 98 | return false === in_array($needle, $haystack, $strict); 99 | } 100 | 101 | 102 | /** 103 | * Returns true if var is not null 104 | * 105 | * @param mixed $value 106 | * 107 | * @return bool 108 | * @author Aurimas Niekis 109 | */ 110 | function notNull($value) 111 | { 112 | return null !== $value; 113 | } 114 | 115 | 116 | /** 117 | * Returns true if value is null 118 | * 119 | * @param mixed $value 120 | * 121 | * @return bool 122 | * @author Aurimas Niekis 123 | */ 124 | function null($value) 125 | { 126 | return null === $value; 127 | } 128 | 129 | 130 | /** 131 | * Generates temp file on systems temp folder with prefix 132 | * 133 | * @param string $prefix 134 | * 135 | * @return string 136 | * @author Aurimas Niekis 137 | */ 138 | function tempFile($prefix = 'php') 139 | { 140 | return tempnam(sys_get_temp_dir(), $prefix); 141 | } 142 | 143 | 144 | /** 145 | * Returns true if value is true 146 | * 147 | * @param mixed $value 148 | * 149 | * @return bool 150 | * @author Aurimas Niekis 151 | */ 152 | function true($value) 153 | { 154 | return true === $value; 155 | } 156 | 157 | 158 | /** 159 | * Returns the first param if isset or the second one or null if it doesn't 160 | * 161 | * @param mixed $value 162 | * @param mixed $default 163 | * @return mixed 164 | * @author Christophe Jean 165 | */ 166 | function ifSetOr(&$value, $default = null) 167 | { 168 | if (isset($value)) { 169 | return $value; 170 | } 171 | return $default; 172 | } 173 | -------------------------------------------------------------------------------- /src/Invoke.php: -------------------------------------------------------------------------------- 1 | 56 | */ 57 | function ifNotEmpty(callable $callable, $var) 58 | { 59 | return ifCondition($callable, [$var], Funct\notEmpty($var)); 60 | } 61 | -------------------------------------------------------------------------------- /src/Object.php: -------------------------------------------------------------------------------- 1 | 17 | */ 18 | function toArray($objects, $valueMethod, $keyMethod = null) 19 | { 20 | $results = []; 21 | 22 | foreach ($objects as $object) { 23 | $value = call_user_func([$object, $valueMethod]); 24 | if (null !== $keyMethod) { 25 | $key = call_user_func([$object, $keyMethod]); 26 | 27 | $results[$key] = $value; 28 | } else { 29 | $results[] = $value; 30 | } 31 | } 32 | 33 | return $results; 34 | } 35 | 36 | /** 37 | * Assign value to object from array if key exists 38 | * 39 | * @param object $object 40 | * @param string $property 41 | * @param array $array 42 | * @param string $key 43 | * 44 | * @return array 45 | * 46 | * @author Aurimas Niekis 47 | */ 48 | function assignIfIsset($object, $property, $array, $key) 49 | { 50 | if (array_key_exists($key, $array)) { 51 | $object->$property = $array[$key]; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/Strings.php: -------------------------------------------------------------------------------- 1 | 15 | */ 16 | function between($input, $left, $right) 17 | { 18 | $input = ' ' . $input; 19 | $ini = strpos($input, $left); 20 | 21 | if ($ini == 0) { 22 | return ''; 23 | } 24 | 25 | $ini += strlen($left); 26 | $len = strpos($input, $right, $ini) - $ini; 27 | 28 | return substr($input, $ini, $len); 29 | } 30 | 31 | 32 | /** 33 | * Camelizes string 34 | * 35 | * @param string $input 36 | * @param bool $firstLetterUppercase 37 | * 38 | * @return string 39 | * 40 | * @author Aurimas Niekis 41 | */ 42 | function camelize($input, $firstLetterUppercase = false) 43 | { 44 | $input = trim($input); 45 | 46 | if ($firstLetterUppercase) { 47 | $input = upperCaseFirst($input); 48 | } else { 49 | $input = lowerCaseFirst($input); 50 | } 51 | 52 | $input = preg_replace('/^[-_]+/', '', $input); 53 | 54 | $input = preg_replace_callback( 55 | '/[-_\s]+(.)?/u', 56 | function ($match) { 57 | if (isset($match[1])) { 58 | return strtoupper($match[1]); 59 | } else { 60 | return ''; 61 | } 62 | }, 63 | $input 64 | ); 65 | 66 | $input = preg_replace_callback( 67 | '/[\d]+(.)?/u', 68 | function ($match) { 69 | return strtoupper($match[0]); 70 | }, 71 | $input 72 | ); 73 | 74 | return $input; 75 | } 76 | 77 | 78 | /** 79 | * Removes prefix from start of string 80 | * 81 | * @param string $input 82 | * @param string $prefix 83 | * 84 | * @return string 85 | * 86 | * @author Lucantis Swann 87 | */ 88 | function chompLeft($input, $prefix) 89 | { 90 | if (startsWith($input, $prefix)) { 91 | return mb_substr($input, mb_strlen($prefix)); 92 | } 93 | 94 | return $input; 95 | } 96 | 97 | /** 98 | * Removes suffix from end of string 99 | * 100 | * @param string $input 101 | * @param string $suffix 102 | * 103 | * @return string 104 | * 105 | * @author Lucantis Swann 106 | */ 107 | function chompRight($input, $suffix) 108 | { 109 | if (endsWith($input, $suffix)) { 110 | 111 | return mb_substr($input, 0, mb_strlen($input) - mb_strlen($suffix)); 112 | } 113 | 114 | return $input; 115 | } 116 | 117 | /** 118 | * Converts string to camelized class name. First letter is always upper case 119 | * 120 | * @param string $string 121 | * 122 | * @return string 123 | * @author Aurimas Niekis 124 | */ 125 | function classify($string) 126 | { 127 | return camelize($string, true); 128 | } 129 | 130 | 131 | /** 132 | * Collapse multiple spaces 133 | * 134 | * @param string $input 135 | * 136 | * @return string 137 | * 138 | * @author Lucantis Swann 139 | */ 140 | function collapseWhitespace($input) 141 | { 142 | return preg_replace('/\s+/u', ' ', $input); 143 | } 144 | 145 | /** 146 | * Check if string contains substring 147 | * 148 | * @param string $input 149 | * @param string $substring 150 | * 151 | * @return bool 152 | * 153 | * @author Lucantis Swann 154 | */ 155 | function contains($input, $substring) 156 | { 157 | return mb_strpos($input, $substring) !== false; 158 | } 159 | 160 | /** 161 | * Count the occurrences of substring in string 162 | * 163 | * @param string $input 164 | * @param string $substring 165 | * 166 | * @return int 167 | * 168 | * @author Lucantis Swann 169 | */ 170 | function countOccurrences($input, $substring) 171 | { 172 | return mb_substr_count($input, $substring); 173 | } 174 | 175 | 176 | /** 177 | * Converts hyphens and camel casing to underscores 178 | * 179 | * @param string $string 180 | * 181 | * @return string 182 | * @author Aurimas Niekis 183 | */ 184 | function dasherize($string) 185 | { 186 | return strtolower(preg_replace('/(? 199 | */ 200 | function endsWith($input, $substring) 201 | { 202 | return mb_substr($input, -strlen($substring)) === $substring; 203 | } 204 | 205 | /** 206 | * Alias of contains 207 | * 208 | * @param string $input 209 | * @param string $substring 210 | * 211 | * @return bool 212 | * 213 | * @author Lucantis Swann 214 | */ 215 | function includes($input, $substring) 216 | { 217 | return contains($input, $substring); 218 | } 219 | 220 | 221 | /** 222 | * Check if string contains only letters 223 | * 224 | * @param string $input 225 | * 226 | * @return bool 227 | * 228 | * @author Lucantis Swann 229 | */ 230 | function isAlpha($input) 231 | { 232 | return ctype_alpha($input); 233 | } 234 | 235 | /** 236 | * Check if string contains only alphanumeric 237 | * 238 | * @param string $input 239 | * 240 | * @return bool 241 | * 242 | * @author Lucantis Swann 243 | */ 244 | function isAlphaNumeric($input) 245 | { 246 | return ctype_alnum($input); 247 | } 248 | 249 | /** 250 | * Checks if letters in given string are all lowercase. 251 | * 252 | * @param string $input 253 | * @param bool $mb 254 | * 255 | * @return bool 256 | * 257 | * @author Ernestas Kvedaras 258 | */ 259 | function isLower($input, $mb = false) 260 | { 261 | return $mb 262 | ? mb_strtolower($input, mb_detect_encoding($input, 'auto')) === $input 263 | : strtolower($input) === $input; 264 | } 265 | 266 | 267 | /** 268 | * Check if string contains only digits 269 | * 270 | * @param string $input 271 | * 272 | * @return bool 273 | * 274 | * @author Lucantis Swann 275 | */ 276 | function isNumeric($input) 277 | { 278 | return ctype_digit($input); 279 | } 280 | 281 | /** 282 | * Checks if letters in given string are all uppercase. 283 | * 284 | * @param string $input 285 | * @param bool $mb 286 | * 287 | * @return bool 288 | * 289 | * @author Ernestas Kvedaras 290 | */ 291 | function isUpper($input, $mb = false) 292 | { 293 | return $mb 294 | ? mb_strtoupper($input, mb_detect_encoding($input, 'auto')) === $input 295 | : strtoupper($input) === $input; 296 | } 297 | 298 | 299 | /** 300 | * Remove accents from latin characters 301 | * 302 | * @param string $input 303 | * 304 | * @return string 305 | * 306 | * @author Lucantis Swann 307 | */ 308 | function latinize($input) 309 | { 310 | $table = [ 311 | 'أ' => 'a', 'ب' => 'b', 'ت' => 't', 'ث' => 'th', 'ج' => 'g', 'ح' => 'h', 'خ' => 'kh', 'د' => 'd', 'ذ' => 'th', 312 | 'ر' => 'r', 'ز' => 'z', 'س' => 's', 'ش' => 'sh', 'ص' => 's', 'ض' => 'd', 'ط' => 't', 'ظ' => 'th', 'ع' => 'aa', 313 | 'غ' => 'gh', 'ف' => 'f', 'ق' => 'k', 'ك' => 'k', 'ل' => 'l', 'م' => 'm', 'ن' => 'n', 'ه' => 'h', 'و' => 'o', 314 | 'ي' => 'y', 'Ä' => 'A', 'Ö' => 'O', 'Ü' => 'U', 'ß' => 'ss', 'ä' => 'a', 'ö' => 'o', 'ü' => 'u', 'က' => 'k', 315 | 'ခ' => 'kh', 'ဂ' => 'g', 'ဃ' => 'ga', 'င' => 'ng', 'စ' => 's', 'ဆ' => 'sa', 'ဇ' => 'z', 'စျ' => 'za', 316 | 'ည' => 'ny', 'ဋ' => 't', 'ဌ' => 'ta', 'ဍ' => 'd', 'ဎ' => 'da', 'ဏ' => 'na', 'တ' => 't', 'ထ' => 'ta', 'ဒ' => 'd', 317 | 'ဓ' => 'da', 'န' => 'n', 'ပ' => 'p', 'ဖ' => 'pa', 'ဗ' => 'b', 'ဘ' => 'ba', 'မ' => 'm', 'ယ' => 'y', 'ရ' => 'ya', 318 | 'လ' => 'l', 'ဝ' => 'w', 'သ' => 'th', 'ဟ' => 'h', 'ဠ' => 'la', 'အ' => 'a', 'ြ' => 'y', 'ျ' => 'ya', 'ွ' => 'w', 319 | 'ြွ' => 'yw', 'ျွ' => 'ywa', 'ှ' => 'h', 'ဧ' => 'e', '၏' => '-e', 'ဣ' => 'i', 'ဤ' => '-i', 'ဉ' => 'u', 320 | 'ဦ' => '-u', 'ဩ' => 'aw', 'သြော' => 'aw', 'ဪ' => 'aw', '၍' => 'ywae', '၌' => 'hnaik', '၀' => '0', '၁' => '1', 321 | '၂' => '2', '၃' => '3', '၄' => '4', '၅' => '5', '၆' => '6', '၇' => '7', '၈' => '8', '၉' => '9', '္' => '', 322 | '့' => '', 'း' => '', 'ာ' => 'a', 'ါ' => 'a', 'ေ' => 'e', 'ဲ' => 'e', 'ိ' => 'i', 'ီ' => 'i', 'ို' => 'o', 323 | 'ု' => 'u', 'ူ' => 'u', 'ေါင်' => 'aung', 'ော' => 'aw', 'ော်' => 'aw', 'ေါ' => 'aw', 'ေါ်' => 'aw', '်' => 'at', 324 | 'က်' => 'et', 'ိုက်' => 'aik', 'ောက်' => 'auk', 'င်' => 'in', 'ိုင်' => 'aing', 'ောင်' => 'aung', 'စ်' => 'it', 325 | 'ည်' => 'i', 'တ်' => 'at', 'ိတ်' => 'eik', 'ုတ်' => 'ok', 'ွတ်' => 'ut', 'ေတ်' => 'it', 'ဒ်' => 'd', 326 | 'ိုဒ်' => 'ok', 'ုဒ်' => 'ait', 'န်' => 'an', 'ာန်' => 'an', 'ိန်' => 'ein', 'ုန်' => 'on', 'ွန်' => 'un', 327 | 'ပ်' => 'at', 'ိပ်' => 'eik', 'ုပ်' => 'ok', 'ွပ်' => 'ut', 'န်ုပ်' => 'nub', 'မ်' => 'an', 'ိမ်' => 'ein', 328 | 'ုမ်' => 'on', 'ွမ်' => 'un', 'ယ်' => 'e', 'ိုလ်' => 'ol', 'ဉ်' => 'in', 'ံ' => 'an', 'ိံ' => 'ein', 329 | 'ုံ' => 'on', 'Č' => 'C', 'Ď' => 'D', 'Ě' => 'E', 'Ň' => 'N', 'Ř' => 'R', 'Š' => 'S', 'Ť' => 'T', 'Ů' => 'U', 330 | 'Ž' => 'Z', 'č' => 'c', 'ď' => 'd', 'ě' => 'e', 'ň' => 'n', 'ř' => 'r', 'š' => 's', 'ť' => 't', 'ů' => 'u', 331 | 'ž' => 'z', '°' => 0, '¹' => 1, '²' => 2, '³' => 3, '⁴' => 4, '⁵' => 5, '⁶' => 6, '⁷' => 7, '⁸' => 8, '⁹' => 9, 332 | '₀' => 0, '₁' => 1, '₂' => 2, '₃' => 3, '₄' => 4, '₅' => 5, '₆' => 6, '₇' => 7, '₈' => 8, '₉' => 9, 'æ' => 'ae', 333 | 'ǽ' => 'ae', 'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Å' => 'AA', 'Ǻ' => 'A', 'Ă' => 'A', 'Ǎ' => 'A', 334 | 'Æ' => 'AE', 'Ǽ' => 'AE', 'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'å' => 'aa', 'ǻ' => 'a', 'ă' => 'a', 335 | 'ǎ' => 'a', 'ª' => 'a', '@' => 'at', 'Ĉ' => 'C', 'Ċ' => 'C', 'ĉ' => 'c', 'ċ' => 'c', '©' => 'c', 'Ð' => 'Dj', 336 | 'Đ' => 'D', 'ð' => 'dj', 'đ' => 'd', 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', 'Ĕ' => 'E', 'Ė' => 'E', 337 | 'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e', 'ĕ' => 'e', 'ė' => 'e', 'ƒ' => 'f', 'Ĝ' => 'G', 'Ġ' => 'G', 338 | 'ĝ' => 'g', 'ġ' => 'g', 'Ĥ' => 'H', 'Ħ' => 'H', 'ĥ' => 'h', 'ħ' => 'h', 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I', 339 | 'Ï' => 'I', 'Ĩ' => 'I', 'Ĭ' => 'I', 'Ǐ' => 'I', 'Į' => 'I', 'IJ' => 'IJ', 'ì' => 'i', 'í' => 'i', 'î' => 'i', 340 | 'ï' => 'i', 'ĩ' => 'i', 'ĭ' => 'i', 'ǐ' => 'i', 'į' => 'i', 'ij' => 'ij', 'Ĵ' => 'J', 'ĵ' => 'j', 'Ĺ' => 'L', 341 | 'Ľ' => 'L', 'Ŀ' => 'L', 'ĺ' => 'l', 'ľ' => 'l', 'ŀ' => 'l', 'Ñ' => 'N', 'ñ' => 'n', 'ʼn' => 'n', 'Ò' => 'O', 342 | 'Ô' => 'O', 'Õ' => 'O', 'Ō' => 'O', 'Ŏ' => 'O', 'Ǒ' => 'O', 'Ő' => 'O', 'Ơ' => 'O', 'Ø' => 'OE', 'Ǿ' => 'O', 343 | 'Œ' => 'OE', 'ò' => 'o', 'ô' => 'o', 'õ' => 'o', 'ō' => 'o', 'ŏ' => 'o', 'ǒ' => 'o', 'ő' => 'o', 'ơ' => 'o', 344 | 'ø' => 'oe', 'ǿ' => 'o', 'º' => 'o', 'œ' => 'oe', 'Ŕ' => 'R', 'Ŗ' => 'R', 'ŕ' => 'r', 'ŗ' => 'r', 'Ŝ' => 'S', 345 | 'Ș' => 'S', 'ŝ' => 's', 'ș' => 's', 'ſ' => 's', 'Ţ' => 'T', 'Ț' => 'T', 'Ŧ' => 'T', 'Þ' => 'TH', 'ţ' => 't', 346 | 'ț' => 't', 'ŧ' => 't', 'þ' => 'th', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ũ' => 'U', 'Ŭ' => 'U', 'Ű' => 'U', 347 | 'Ų' => 'U', 'Ư' => 'U', 'Ǔ' => 'U', 'Ǖ' => 'U', 'Ǘ' => 'U', 'Ǚ' => 'U', 'Ǜ' => 'U', 'ù' => 'u', 'ú' => 'u', 348 | 'û' => 'u', 'ũ' => 'u', 'ŭ' => 'u', 'ű' => 'u', 'ų' => 'u', 'ư' => 'u', 'ǔ' => 'u', 'ǖ' => 'u', 'ǘ' => 'u', 349 | 'ǚ' => 'u', 'ǜ' => 'u', 'Ŵ' => 'W', 'ŵ' => 'w', 'Ý' => 'Y', 'Ÿ' => 'Y', 'Ŷ' => 'Y', 'ý' => 'y', 'ÿ' => 'y', 350 | 'ŷ' => 'y', 'ა' => 'a', 'ბ' => 'b', 'გ' => 'g', 'დ' => 'd', 'ე' => 'e', 'ვ' => 'v', 'ზ' => 'z', 'თ' => 't', 351 | 'ი' => 'i', 'კ' => 'k', 'ლ' => 'l', 'მ' => 'm', 'ნ' => 'n', 'ო' => 'o', 'პ' => 'p', 'ჟ' => 'zh', 'რ' => 'r', 352 | 'ს' => 's', 'ტ' => 't', 'უ' => 'u', 'ფ' => 'f', 'ქ' => 'k', 'ღ' => 'gh', 'ყ' => 'q', 'შ' => 'sh', 'ჩ' => 'ch', 353 | 'ც' => 'ts', 'ძ' => 'dz', 'წ' => 'ts', 'ჭ' => 'ch', 'ხ' => 'kh', 'ჯ' => 'j', 'ჰ' => 'h', 'ΑΥ' => 'AU', 354 | 'Αυ' => 'Au', 'ΟΥ' => 'OU', 'Ου' => 'Ou', 'ΕΥ' => 'EU', 'Ευ' => 'Eu', 'ΕΙ' => 'I', 'Ει' => 'I', 'ΟΙ' => 'I', 355 | 'Οι' => 'I', 'ΥΙ' => 'I', 'Υι' => 'I', 'ΑΎ' => 'AU', 'Αύ' => 'Au', 'ΟΎ' => 'OU', 'Ού' => 'Ou', 'ΕΎ' => 'EU', 356 | 'Εύ' => 'Eu', 'ΕΊ' => 'I', 'Εί' => 'I', 'ΟΊ' => 'I', 'Οί' => 'I', 'ΎΙ' => 'I', 'Ύι' => 'I', 'ΥΊ' => 'I', 357 | 'Υί' => 'I', 'αυ' => 'au', 'ου' => 'ou', 'ευ' => 'eu', 'ει' => 'i', 'οι' => 'i', 'υι' => 'i', 'αύ' => 'au', 358 | 'ού' => 'ou', 'εύ' => 'eu', 'εί' => 'i', 'οί' => 'i', 'ύι' => 'i', 'υί' => 'i', 'Α' => 'A', 'Β' => 'V', 359 | 'Γ' => 'G', 'Δ' => 'D', 'Ε' => 'E', 'Ζ' => 'Z', 'Η' => 'I', 'Θ' => 'Th', 'Ι' => 'I', 'Κ' => 'K', 'Λ' => 'L', 360 | 'Μ' => 'M', 'Ν' => 'N', 'Ξ' => 'X', 'Ο' => 'O', 'Π' => 'P', 'Ρ' => 'R', 'Σ' => 'S', 'Τ' => 'T', 'Υ' => 'I', 361 | 'Φ' => 'F', 'Χ' => 'Ch', 'Ψ' => 'Ps', 'Ω' => 'O', 'Ά' => 'A', 'Έ' => 'E', 'Ή' => 'I', 'Ί' => 'I', 'Ό' => 'O', 362 | 'Ύ' => 'I', 'Ϊ' => 'I', 'Ϋ' => 'I', 'ϒ' => 'I', 'α' => 'a', 'β' => 'v', 'γ' => 'g', 'δ' => 'd', 'ε' => 'e', 363 | 'ζ' => 'z', 'η' => 'i', 'θ' => 'th', 'ι' => 'i', 'κ' => 'k', 'λ' => 'l', 'μ' => 'm', 'ν' => 'n', 'ξ' => 'x', 364 | 'ο' => 'o', 'π' => 'p', 'ρ' => 'r', 'ς' => 's', 'σ' => 's', 'τ' => 't', 'υ' => 'i', 'φ' => 'f', 'χ' => 'ch', 365 | 'ψ' => 'ps', 'ω' => 'o', 'ά' => 'a', 'έ' => 'e', 'ή' => 'i', 'ί' => 'i', 'ό' => 'o', 'ύ' => 'i', 'ϊ' => 'i', 366 | 'ϋ' => 'i', 'ΰ' => 'i', 'ώ' => 'o', 'ϐ' => 'v', 'ϑ' => 'th', 'अ' => 'a', 'आ' => 'aa', 'ए' => 'e', 'ई' => 'ii', 367 | 'ऍ' => 'ei', 'ऎ' => 'ऎ', 'ऐ' => 'ai', 'इ' => 'i', 'ओ' => 'o', 'ऑ' => 'oi', 'ऒ' => 'oii', 'ऊ' => 'uu', 368 | 'औ' => 'ou', 'उ' => 'u', 'ब' => 'B', 'भ' => 'Bha', 'च' => 'Ca', 'छ' => 'Chha', 'ड' => 'Da', 'ढ' => 'Dha', 369 | 'फ' => 'Fa', 'फ़' => 'Fi', 'ग' => 'Ga', 'घ' => 'Gha', 'ग़' => 'Ghi', 'ह' => 'Ha', 'ज' => 'Ja', 'झ' => 'Jha', 370 | 'क' => 'Ka', 'ख' => 'Kha', 'ख़' => 'Khi', 'ल' => 'L', 'ळ' => 'Li', 'ऌ' => 'Li', 'ऴ' => 'Lii', 'ॡ' => 'Lii', 371 | 'म' => 'Ma', 'न' => 'Na', 'ङ' => 'Na', 'ञ' => 'Nia', 'ण' => 'Nae', 'ऩ' => 'Ni', 'ॐ' => 'oms', 'प' => 'Pa', 372 | 'क़' => 'Qi', 'र' => 'Ra', 'ऋ' => 'Ri', 'ॠ' => 'Ri', 'ऱ' => 'Ri', 'स' => 'Sa', 'श' => 'Sha', 'ष' => 'Shha', 373 | 'ट' => 'Ta', 'त' => 'Ta', 'ठ' => 'Tha', 'द' => 'Tha', 'थ' => 'Tha', 'ध' => 'Thha', 'ड़' => 'ugDha', 374 | 'ढ़' => 'ugDhha', 'व' => 'Va', 'य' => 'Ya', 'य़' => 'Yi', 'ज़' => 'Za', 'Ā' => 'A', 'Ē' => 'E', 'Ģ' => 'G', 375 | 'Ī' => 'I', 'Ķ' => 'K', 'Ļ' => 'L', 'Ņ' => 'N', 'Ū' => 'U', 'ā' => 'a', 'ē' => 'e', 'ģ' => 'g', 'ī' => 'i', 376 | 'ķ' => 'k', 'ļ' => 'l', 'ņ' => 'n', 'ū' => 'u', 'Ą' => 'A', 'Ć' => 'C', 'Ę' => 'E', 'Ł' => 'L', 'Ń' => 'N', 377 | 'Ó' => 'O', 'Ś' => 'S', 'Ź' => 'Z', 'Ż' => 'Z', 'ą' => 'a', 'ć' => 'c', 'ę' => 'e', 'ł' => 'l', 'ń' => 'n', 378 | 'ó' => 'o', 'ś' => 's', 'ź' => 'z', 'ż' => 'z', 'Ъ' => '', 'Ь' => '', 'А' => 'A', 'Б' => 'B', 'Ц' => 'C', 379 | 'Ч' => 'Ch', 'Д' => 'D', 'Е' => 'E', 'Ё' => 'E', 'Э' => 'E', 'Ф' => 'F', 'Г' => 'G', 'Х' => 'H', 'И' => 'I', 380 | 'Й' => 'Y', 'Я' => 'Ya', 'Ю' => 'Yu', 'К' => 'K', 'Л' => 'L', 'М' => 'M', 'Н' => 'N', 'О' => 'O', 'П' => 'P', 381 | 'Р' => 'R', 'С' => 'S', 'Ш' => 'Sh', 'Щ' => 'Shch', 'Т' => 'T', 'У' => 'U', 'В' => 'V', 'Ы' => 'Y', 'З' => 'Z', 382 | 'Ж' => 'Zh', 'ъ' => '', 'ь' => '', 'а' => 'a', 'б' => 'b', 'ц' => 'c', 'ч' => 'ch', 'д' => 'd', 'е' => 'e', 383 | 'ё' => 'e', 'э' => 'e', 'ф' => 'f', 'г' => 'g', 'х' => 'h', 'и' => 'i', 'й' => 'y', 'я' => 'ya', 'ю' => 'yu', 384 | 'к' => 'k', 'л' => 'l', 'м' => 'm', 'н' => 'n', 'о' => 'o', 'п' => 'p', 'р' => 'r', 'с' => 's', 'ш' => 'sh', 385 | 'щ' => 'shch', 'т' => 't', 'у' => 'u', 'в' => 'v', 'ы' => 'y', 'з' => 'z', 'ж' => 'zh', 'Ç' => 'C', 'Ğ' => 'G', 386 | 'İ' => 'I', 'Ş' => 'S', 'ç' => 'c', 'ğ' => 'g', 'ı' => 'i', 'ş' => 's', 'Ґ' => 'G', 'І' => 'I', 'Ї' => 'Ji', 387 | 'Є' => 'Ye', 'ґ' => 'g', 'і' => 'i', 'ї' => 'ji', 'є' => 'ye', 'ạ' => 'a', 'ả' => 'a', 'ầ' => 'a', 'ấ' => 'a', 388 | 'ậ' => 'a', 'ẩ' => 'a', 'ẫ' => 'a', 'ằ' => 'a', 'ắ' => 'a', 'ặ' => 'a', 'ẳ' => 'a', 'ẵ' => 'a', 'ẹ' => 'e', 389 | 'ẻ' => 'e', 'ẽ' => 'e', 'ề' => 'e', 'ế' => 'e', 'ệ' => 'e', 'ể' => 'e', 'ễ' => 'e', 'ị' => 'i', 'ỉ' => 'i', 390 | 'ọ' => 'o', 'ỏ' => 'o', 'ồ' => 'o', 'ố' => 'o', 'ộ' => 'o', 'ổ' => 'o', 'ỗ' => 'o', 'ờ' => 'o', 'ớ' => 'o', 391 | 'ợ' => 'o', 'ở' => 'o', 'ỡ' => 'o', 'ụ' => 'u', 'ủ' => 'u', 'ừ' => 'u', 'ứ' => 'u', 'ự' => 'u', 'ử' => 'u', 392 | 'ữ' => 'u', 'ỳ' => 'y', 'ỵ' => 'y', 'ỷ' => 'y', 'ỹ' => 'y', 'Ạ' => 'A', 'Ả' => 'A', 'Ầ' => 'A', 'Ấ' => 'A', 393 | 'Ậ' => 'A', 'Ẩ' => 'A', 'Ẫ' => 'A', 'Ằ' => 'A', 'Ắ' => 'A', 'Ặ' => 'A', 'Ẳ' => 'A', 'Ẵ' => 'A', 'Ẹ' => 'E', 394 | 'Ẻ' => 'E', 'Ẽ' => 'E', 'Ề' => 'E', 'Ế' => 'E', 'Ệ' => 'E', 'Ể' => 'E', 'Ễ' => 'E', 'Ị' => 'I', 'Ỉ' => 'I', 395 | 'Ọ' => 'O', 'Ỏ' => 'O', 'Ồ' => 'O', 'Ố' => 'O', 'Ộ' => 'O', 'Ổ' => 'O', 'Ỗ' => 'O', 'Ờ' => 'O', 'Ớ' => 'O', 396 | 'Ợ' => 'O', 'Ở' => 'O', 'Ỡ' => 'O', 'Ụ' => 'U', 'Ủ' => 'U', 'Ừ' => 'U', 'Ứ' => 'U', 'Ự' => 'U', 'Ử' => 'U', 397 | ]; 398 | 399 | $string = strtr($input, $table); 400 | 401 | return $string; 402 | } 403 | 404 | 405 | /** 406 | * Return the substring denoted by n positive left-most characters 407 | * 408 | * @param string $string 409 | * @param int $n 410 | * 411 | * @return string 412 | * @author Aurimas Niekis 413 | */ 414 | function left($string, $n) 415 | { 416 | $start = 0; 417 | if ($n < 0) { 418 | $start = $n; 419 | $n = -$n; 420 | } 421 | 422 | return substr($string, $start, $n); 423 | } 424 | 425 | /** 426 | * Return the length of a string 427 | * 428 | * @param string $string the input string 429 | * @param bool $mb to use or not to use mb_strlen 430 | * 431 | * @return int the length of the input string 432 | * @author Rod Elias 433 | */ 434 | function len($string, $mb = false) 435 | { 436 | return length($string, $mb); 437 | } 438 | 439 | /** 440 | * Return the length of a string 441 | * 442 | * @param string $string the input string 443 | * @param bool $mb to use or not to use mb_strlen 444 | * 445 | * @return int the length of the input string 446 | * @author Rod Elias 447 | */ 448 | function length($string, $mb = false) 449 | { 450 | 451 | return $mb ? mb_strlen($string) : strlen($string); 452 | } 453 | 454 | /** 455 | * Returns an array with the lines. Cross-platform compatible 456 | * 457 | * @param string $string 458 | * 459 | * @return array 460 | * @author Aurimas Niekis 461 | */ 462 | function lines($string) 463 | { 464 | return preg_split('/\r\n|\n|\r/', $string); 465 | } 466 | 467 | 468 | /** 469 | * Converts string first char to lowercase 470 | * 471 | * @param string $input 472 | * 473 | * @return string 474 | * 475 | * @author Aurimas Niekis 476 | */ 477 | function lowerCaseFirst($input) 478 | { 479 | return lcfirst($input); 480 | } 481 | 482 | 483 | /** 484 | * Pads the string in the center with specified character. char may be a string or a number, defaults is a space 485 | * 486 | * @param string $string 487 | * @param int $length 488 | * @param string $char 489 | * 490 | * @return string 491 | * @author Aurimas Niekis 492 | */ 493 | function pad($string, $length, $char = ' ') 494 | { 495 | return str_pad($string, $length, $char, STR_PAD_BOTH); 496 | } 497 | 498 | 499 | /** 500 | * Left pads the string 501 | * 502 | * @param string $input 503 | * @param string $length 504 | * @param string $char 505 | * 506 | * @return string 507 | * 508 | * @author Lucantis Swann 509 | */ 510 | function padLeft($input, $length, $char = ' ') 511 | { 512 | return str_pad($input, $length, $char, STR_PAD_LEFT); 513 | } 514 | 515 | 516 | /** 517 | * Right pads the string 518 | * 519 | * @param string $input 520 | * @param string $length 521 | * @param string $char 522 | * 523 | * @return string 524 | * 525 | * @author Lucantis Swann 526 | */ 527 | function padRight($input, $length, $char = ' ') 528 | { 529 | return str_pad($input, $length, $char, STR_PAD_RIGHT); 530 | } 531 | 532 | 533 | /** 534 | * Repeat the string n times 535 | * 536 | * @param string $input 537 | * @param int $n 538 | * 539 | * @return string 540 | * 541 | * @author Lucantis Swann 542 | */ 543 | function repeat($input, $n) 544 | { 545 | return str_repeat($input, $n); 546 | } 547 | 548 | /** 549 | * Reverses a string 550 | * 551 | * @param string $input 552 | * 553 | * @return string 554 | * @author Rod Elias 555 | */ 556 | function reverse($input) 557 | { 558 | return strrev($input); 559 | } 560 | 561 | 562 | /** 563 | * Return the substring denoted by n positive right-most characters 564 | * 565 | * @param string $string 566 | * @param int $n 567 | * 568 | * @return string 569 | * @author Aurimas Niekis 570 | */ 571 | function right($string, $n) 572 | { 573 | $start = -$n; 574 | if ($n < 0) { 575 | $start = 0; 576 | $n = -$n; 577 | } 578 | 579 | return substr($string, $start, $n); 580 | } 581 | 582 | 583 | /** 584 | * Converts the text into a valid url slug. Removes accents from Latin characters 585 | * 586 | * @param string $string 587 | * 588 | * @return string 589 | * @author Aurimas Niekis 590 | */ 591 | function slugify($string) 592 | { 593 | $string = latinize($string); 594 | $string = preg_replace('~[^\\pL\d]+~u', '-', $string); 595 | $string = trim($string, '-'); 596 | $string = strtolower($string); 597 | 598 | return preg_replace('~[^-\w]+~', '', $string); 599 | } 600 | 601 | 602 | /** 603 | * Check if string starts with substring 604 | * 605 | * @param string $input 606 | * @param string $substring 607 | * 608 | * @return bool 609 | * 610 | * @author Lucantis Swann 611 | */ 612 | function startsWith($input, $substring) 613 | { 614 | return mb_strpos($input, $substring) === 0; 615 | } 616 | 617 | /** 618 | * Returns a new string with all occurrences of [string1],[string2],... removed. 619 | * 620 | * @param string $string 621 | * @param string $string1 622 | * 623 | * @return string 624 | * @author Aurimas Niekis 625 | */ 626 | function strip($string, $string1) 627 | { 628 | $arguments = func_get_args(); 629 | 630 | return str_replace(array_slice($arguments, 1), '', $string); 631 | } 632 | 633 | 634 | /** 635 | * Strip all of the punctuation 636 | * 637 | * @param string $string 638 | * 639 | * @return string 640 | * @author Aurimas Niekis 641 | */ 642 | function stripPunctuation($string) 643 | { 644 | return preg_replace('/[^\w\s]|_/', '', $string); 645 | } 646 | 647 | /** 648 | * Makes a case swapped version of the string 649 | * @param string $string the input string 650 | * @param boolean $mb to use or not to use multibyte character feature 651 | * @return string case swapped version of the input string 652 | * 653 | * @author Rod Elias 654 | */ 655 | function swapCase($string, $mb = false) 656 | { 657 | return array_reduce(str_split($string), function($carry, $item) use ($mb) { 658 | return $carry .= isLower($item, $mb) ? toUpper($item, $mb) : toLower($item, $mb); 659 | }, ''); 660 | } 661 | 662 | /** 663 | * Repeat the string n times 664 | * 665 | * @param string $input 666 | * @param int $n 667 | * 668 | * @return string 669 | * 670 | * @author Lucantis Swann 671 | */ 672 | function times($input, $n) 673 | { 674 | return repeat($input, $n); 675 | } 676 | 677 | /** 678 | * Creates a title version of the string. Capitalizes all the words and replaces some characters in the string to 679 | * create a nicer looking title. string_titleize is meant for creating pretty output 680 | * 681 | * @param string $string 682 | * @param array $ignore 683 | * 684 | * @return string 685 | * @author Aurimas Niekis 686 | */ 687 | function titleize($string, array $ignore = []) 688 | { 689 | $string = preg_replace('/(? 713 | */ 714 | function toLower($input, $mb = false) 715 | { 716 | return $mb ? mb_strtolower($input, mb_detect_encoding($input, 'auto')) : strtolower($input); 717 | } 718 | 719 | /** 720 | * Join an array into a human readable sentence 721 | * 722 | * @param array $array 723 | * @param string $delimiter 724 | * @param string $lastDelimiter 725 | * 726 | * @return string 727 | * @author Aurimas Niekis 728 | */ 729 | function toSentence($array, $delimiter = ', ', $lastDelimiter = ' and ') 730 | { 731 | $lastWord = array_pop($array); 732 | 733 | return implode($delimiter, $array) . $lastDelimiter . $lastWord; 734 | } 735 | 736 | /** 737 | * The same as string_to_sentence, but adjusts delimeters to use Serial comma) 738 | * 739 | * @param array $array 740 | * @param string $delimiter 741 | * @param string $lastDelimiter 742 | * 743 | * @return string 744 | * @author Aurimas Niekis 745 | */ 746 | function toSentenceSerial($array, $delimiter = ', ', $lastDelimiter = ' and ') 747 | { 748 | $lastWord = array_pop($array); 749 | 750 | $lastDel = ''; 751 | if (count($array) > 1) { 752 | $lastDel = trim($delimiter, ' '); 753 | } 754 | 755 | return implode($delimiter, $array) . $lastDel . $lastDelimiter . $lastWord; 756 | } 757 | 758 | 759 | /** 760 | * makes a string uppercase 761 | * @param string $input the input string 762 | * @param boolean $mb to use or not to use multibyte character feature 763 | * @return string uppercased string 764 | * 765 | * @author Rod Elias 766 | */ 767 | function toUpper($input, $mb = false) 768 | { 769 | return $mb ? mb_strtoupper($input, mb_detect_encoding($input, 'auto')) : strtoupper($input); 770 | } 771 | 772 | 773 | /** 774 | * Truncate string accounting for word placement and character count 775 | * 776 | * @param string $input 777 | * @param int $length 778 | * @param string $chars 779 | * 780 | * @return string 781 | * 782 | * @author Lucantis Swann 783 | */ 784 | function truncate($input, $length, $chars = '…') 785 | { 786 | if (strlen($input) > $length) { 787 | $splits = preg_split('/([\s\n\r]+)/u', $input, null, PREG_SPLIT_DELIM_CAPTURE); 788 | 789 | $splitsLength = 0; 790 | $splitsCount = count($splits); 791 | 792 | for ($lastSplit = 0; $lastSplit < $splitsCount; ++$lastSplit) { 793 | $splitsLength += strlen($splits[$lastSplit]); 794 | if ($splitsLength > $length) { 795 | break; 796 | } 797 | } 798 | 799 | return implode(array_slice($splits, 0, $lastSplit)) . $chars; 800 | } else { 801 | return $input; 802 | } 803 | } 804 | 805 | 806 | /** 807 | * Converts hyphens and camel casing to underscores 808 | * 809 | * @param string $string 810 | * 811 | * @return string 812 | * @author Aurimas Niekis 813 | */ 814 | function underscore($string) 815 | { 816 | return strtolower(preg_replace('/(? 828 | */ 829 | function upperCaseFirst($input) 830 | { 831 | return ucfirst($input); 832 | } 833 | --------------------------------------------------------------------------------