/ Published in: PHP
URL: http://www.phpclasses.org/browse/file/27467.html
This class can be used to Manipulate values in iterable data sets.
It can iterate over a set of elements contained in a collection managed by an object that implements the Iterable interface.
The class executes several types of operations to manipulate the collection elements by invoking given callback functions that determine what to do with the elements.
Currently it can filter a subset of the elements, call a function to manipulate each of the elements, map the elements to a new set of values, reduce the elements to a single value.
Expand |
Embed | Plain Text
<?php /** * Allows to use FP-like features for arrays, strings, SPL iterators and even results * returned from callbacks * * This class adds a lot of syntax sugar to your recordset processing routines or other * similar tasks when you don't want to write a loop construct. You will never see the * calls of FuncUtility methods in the Frontier's core. It uses call_user_func(), * which is slow. But I hope, it's performance will be improved someday though. * * @author Stanis Shramko <stanislav.shramko@gmail.com> */ class FuncUtility { /** * Filters all elements of $set using the $callback * * @return array * @throws InvalidArgumentException */ { { throw new InvalidArgumentException( 'The first argument is not callable'); } { { { $values[] = $value; } } } else { foreach (($set = self::prepareIterable($set)) as $value) { $values[] = $value; } } return $values; } /** * Applies the $callback to all elements of $set * * @return void * @throws InvalidArgumentException */ { { throw new InvalidArgumentException( 'The first argument is not callable'); } { { } } else { foreach (($set = self::prepareIterable($set)) as $value) { } } } /** * Applies the $callback to all elements of $set * * @return array * @throws InvalidArgumentException */ { { throw new InvalidArgumentException( 'The first argument is not callable'); } { { } } else { foreach (($set = self::prepareIterable($set)) as $value) { } } return $values; } /** * Reduces the $set using $result * * @return array * @throws InvalidArgumentException */ { { throw new InvalidArgumentException( 'The first argument is not callable'); } { { } } else { $first = true; foreach (($set = self::prepareIterable($set)) as $value) { if (!$first) { $value); } else { $result = $value; $first = false; } } } return $result; } /** * Prepares "something iterable" * * @param mixed $set * @return mixed array or Iterator */ { { return $set; } { return str_split($set); } throw new InvalidArgumentException('The second argument is not iterable'); } }
You need to login to post a comment.
