One of the most frustrating things you may encounter as a PHP developer is definitely the lack of clarity and consistency of many of the core array and string manipulation functions.

What goes wrong

Let’s take for example the function that checks if a string contains another string. You might want to use strstr but you should use strpos for performance purpose. You already noticed the lack of clarity in the name of the functions, and the fact that whatever you do, the better way of dealing with strings in php is never straightforward.
You may also know that the case insensitive equivalents of these function adds an i in the middle of their names (stristr, stripos). Again, this is not very explicit. Finally, some functions does not respect the same format, such as str_split or str_replace.

And the same matters occur with the array functions.

As you may know, I also have a .NET background, especially in C#. One of the thing I like is the way they deal with this issue. First of all, strings and arrays are objects… They also have the IEnumerable interface that is implemented by everything that is an enumeration of items (strings are enumeration of characters). They added a bunch a generic methods that extends the IEnumerable interface to provide clear and powerful functionalities. So whenever you have something that is enumerable you leverage automatically tens of filtering, ordering, transformation and manipulation functions.

I really miss IEnumerable when I am using PHP.

The BCCEnumerableUtility

When PHP5.4 came out, I saw in traits a way to port IEnumerable from C# to PHP.

So I mimicked the C# interface and made the Enumerable trait, with the Collection and String classes that leverage the trait.

Let’s avoid taking too much, here is an example with the Collection class that leverage the trait:

select(function($item) { return $item*$item; })->average();

// filter the even numbers and then order
$values->where(function($item) { return $item%2 == 0; })->orderBy();

The Enumerable trait comes with a bunch of nice functions that you can discover on the BBCEnumerableUtility Github repository.

The String class

The library also provides a String class that adds some string dedicated functions:

replace('world', 'pineapple') // replace world by pineapple
->toUpper() // to upper case
->skip(6) // skip the 6 first letters
->takeWhile(function($char) { $char != '!'; }); // take the rest while the char is different from '!'

echo $string; // PINEAPPLE

I know many people won’t like the idea of a String class, so I provide a StringUtility helper that gives you access to all the functionality with static calls:

<?php
use BCCEnumerableUtilityStringUtility;

$string = 'Hello world!';

$string = StringUtility::replace ($string, 'world', 'pineapple'); // replace world by pineapple
$string = StringUtility::toUpper ($string); // to upper case
$string = StringUtility::skip ($string, 6); // skip the 6 first letters
$string = StringUtility::takeWhile($string, function($char) { $char != '!'; }); // take the rest while the char is different from '!'

echo $string; // PINEAPPLE

Wrap up

The BCCEnumerableUtility library will help you manipulate strings and arrays more easily.

As usual, I published this library on Github. I welcome remarks, bugs and contributions🙂

One thought on “The BCCEnumerableUtility is out

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s