chris-kruining/utilities

Utilities for PHP 7.1^

0.2.17 2019-02-28 10:03 UTC

README

Latest Stable Version Latest Unstable Version License Build Status Maintainability Test Coverage SensioLabsInsight

Utilities

some utilities for php, nothing special, nothing new, just to my taste

Installation

installation is simply done via composer composer require chris-kruining/utilities

Usage

The main component of the library is the Collection at the moment. The goal of the Collection is to provide a object oriented interface for array functions. It also has a couple of extra's like method chaining and linq-esc implementation(NOT DONE YET) so that you may interact with the Collection as if it was a database table

for example

CPB\Utilities\Collections\Collection::from([ 'these', 'are', null, null, 'some', null, 'test', 'values', null ])
  ->filter()
  ->toString(' ');

would yield

'these are some test values'

which is the same as

join(' ', array_filter([ 'these', 'are', null, null, 'some', null, 'test', 'values', null ]));

I agree, the vannilla way is shorter now but the strength really comes into play when we start adding callbacks and increase the chain length

CPB\Utilities\Collections\Collection::from([ 'these', '', '', 'are', null, 'some', null, 'test', '', 'values', '' ])
  ->filter(function($v){ return $v !== null && strlen($v) > 0; })
  ->map(function($k, $v){ return $k . '::' . $v; })
  ->toString('|');

would yield

'0_these|1_are|2_some|3_test|4_values'

which is the same as

$filtered = array_filter(
  [ 'these', '', '', 'are', null, 'some', null, 'test', '', 'values', '' ], 
  function($v){ return $v !== null && strlen($v) > 0; }
);

join('|', array_map(function($k, $v){ return $k . '::' . $v; }, array_keys($filtered), $filtered));

As you can see the collection version maintains readability whereas the vannilla version loses in my opinion it's charm because to achieve a single goal you need to spread it out over multiple variables

The future

What I hope to accomplish is querying over a collection

$collction['max($ID), $Name, $City where $Name startswith "Chris" limit 3, 5']

Roadmap

  • Implement basic features to Collection
  • Bloat Collection with features :P
  • Split of features into an inheritance tree
  • Split lazy mode from Collection into LazyCollection and implement PHP's array functions as generators
  • Finish inheritance structure
  • Implement LazyCollection
  • Look into the possibility of turning the lib into an PHP extension with PHP-CPP (simply to improve performance)
  • Write an SQL syntax parser to allow querying over Queryable
  • (Better) implement the Queryable interface in a new class so the Collection doesn't become bloated