PHP Classes

PHPR: Provide functional interfaces to manipulate arrays

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 77%Total: 188 All time: 8,617 This week: 524Up
Version License PHP version Categories
phpr 0.0.2GNU General Publi...5.3.0PHP 5, Data types
Description 

Author

This package provides functional interfaces to manipulate arrays.

It provides collection class that provide an array interface and uses two traits that have additional functions to provide a functional interface to manipulate arrays in a functional way similar to the Ruby language.

Currently the traits provide functions for accessing array elements and traverse the array, sort the array, check if the array contains a certain value, get the minimum and maximum value in the array, filter or map the array values using a callback function, etc..

Innovation Award
PHP Programming Innovation award nominee
December 2015
Number 7
Many classes require manipulating collections of elements. However the types of elements are different for each type of collection.

This package implements a trait for manipulating generic collections of elements that can be reused in classes for many different purposes.

Manuel Lemos
Picture of Eustaquio Rangel de Oliveira Jr.
  Performance   Level  
Name: Eustaquio Rangel de ... is available for providing paid consulting. Contact Eustaquio Rangel de ... .
Classes: 6 packages by
Country: Brazil Brazil
Age: 53
All time rank: 27520 in Brazil Brazil
Week rank: 131 Up11 in Brazil Brazil Up
Innovation award
Innovation award
Nominee: 3x

Winner: 1x

Example

<?php
/**
 * Collection code sample
 *
 * PHP version 5.5
 *
 * @category Samples
 * @package PHPR
 * @author Eustáquio Rangel <taq@bluefish.com.br>
 * @license http://www.gnu.org/copyleft/gpl.html GPL
 * @link http://github.com/taq/torm
 */
require_once "../vendor/autoload.php";

$col = new PHPR\Collection(["one", "two", "three"]);
echo
$col[0]."\n";
echo
$col[1]."\n";
echo
$col[2]."\n";
?>


Details

PHPR

What is - motivation

Trying to extend some PHP objects to be used on a more functional way. Once one PHP developer said that nowadays IDE's can help us to avoid remembering the lack of standardization some PHP methods has. As I don't use an IDE, as lots of developers out there, I don't agree with that and I'm kind of tired to try to remember if on array_filter or array_map the array goes first and the function goes last or what order they are.

So, I think it's easier to use on a function and Ruby style way, where we can call the methods on the object and it returns what we need.

One notable exception to try to make things easier is that PHP have some reserved and constant words we cannot reuse even inside a namespace, like Array. So I needed to call the Array class Collection, otherwise it will be impossible to implement. All the classes have the original object accessible with the values method.

Other thing is that some methods just returns a value or null, true or false. No more guesses and wonderings of what happened if it returns 0 or false.

Samples

$t = new PHPR\Collection([0 => "zero", 1 => "one", 2 => "two"]);

// outputs: 
// null
// yay, no more 'undefined index' messages!
echo $t[10]."\n";

// outputs:
// zero
// one
// two
$t->each(function($e) {
   echo "$e\n";
});

// each
// outputs:
// 0 - zero
// 1 - one
// 2 - two
$t->each(function($key, $val) {
   echo "$key - $val\n";
});

$t->includes("one");    // true
$t->includes("three");  // false

// sort
// outputs:
// array(3) {
//   [0] =>
//   string(3) "one"
//   [1] =>
//   string(3) "two"
//   [2] =>
//   string(4) "zero"
// }
$sorted = $t->sort();
var_dump($sorted->values());

$t->min(); // "one"
$t->max(); // "zero"

// select
// outputs (keeping order):
// array(1) {
//   [0] =>
//   string(4) "zero"
// }
$selected = $t->select(function($e) {
   return strlen($e) > 3;
});
var_dump($selected->values());

// map
// outputs:
// array(3) {
//   [0] =>
//   string(4) "orez"
//   [1] =>
//   string(3) "eno"
//   [2] =>
//   string(3) "owt"
// }
$changed = $t->map(function($e) {
   return strrev($e);
});
var_dump($changed->values());

$t->all(function($e) { return strlen($e) > 2; })); // true
$t->all(function($e) { return strlen($e) > 3; })); // false

$t->any(function($e) { return strlen($e) > 3; })); // true
$t->any(function($e) { return strlen($e) > 4; })); // false

// chainable methods
// outputs:
// array(2) {
//     [0] =>
//     string(3) "eno"
//     [1] =>
//     string(3) "owt"
//   }
$changed = self::$_col->map(function($e) {
   return strrev($e);
})->select(function($e) { return strlen($e) <= 3; });
var_dump($changed->values());

  Files folder image Files (21)  
File Role Description
Files folder imagesamples (1 file, 1 directory)
Files folder imagesrc (3 files)
Files folder imagetest (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE.txt Lic. License
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (21)  /  samples  
File Role Description
Files folder imageenumerable (13 files)
  Accessible without login Plain text file collection.php Example Example script

  Files folder image Files (21)  /  samples  /  enumerable  
File Role Description
  Accessible without login Plain text file all.php Example Example script
  Accessible without login Plain text file any.php Example Example script
  Accessible without login Plain text file each.php Example Example script
  Accessible without login Plain text file find.php Example Example script
  Accessible without login Plain text file group_by.php Example Example script
  Accessible without login Plain text file includes.php Example Example script
  Accessible without login Plain text file inject.php Example Example script
  Accessible without login Plain text file map.php Example Example script
  Accessible without login Plain text file minmax.php Example Example script
  Accessible without login Plain text file partition.php Example Example script
  Accessible without login Plain text file reject.php Example Example script
  Accessible without login Plain text file select.php Example Example script
  Accessible without login Plain text file sort.php Example Example script

  Files folder image Files (21)  /  src  
File Role Description
  Plain text file ArrayAccessInterface.php Class Class source
  Plain text file Collection.php Class Class source
  Plain text file Enumerable.php Class Class source

  Files folder image Files (21)  /  test  
File Role Description
  Accessible without login Plain text file enumerableTest.php Test Unit test script

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:188
This week:0
All time:8,617
This week:524Up
 User Ratings  
 
 All time
Utility:83%StarStarStarStarStar
Consistency:83%StarStarStarStarStar
Documentation:91%StarStarStarStarStar
Examples:91%StarStarStarStarStar
Tests:91%StarStarStarStarStar
Videos:-
Overall:77%StarStarStarStar
Rank:55