urmaul/rich-types

Yet another rich types library.

0.3.4 2018-02-06 07:47 UTC

This package is not auto-updated.

Last update: 2024-11-17 08:30:13 UTC


README

Yet another rich types library. Designed to use fluent interaces with base PHP types.

No Not enough comments, no tests, no release planned. Use it only if you are me brave enough.

While major version is 0 minor version change means backwards incompatible changes. So you should install it with composer like this:

php composer.phar require urmaul/rich-types '0.3.*'

Build Status

Example

use rich\collections\Strings;

$words = Strings::split(' ', $text)->lower()->unique()->values();

Supported types

Collections

Variables

Base collection class. Can be passed to foreach and count().

  • static from($array) - creates new collection instance.
  • map($callback) - performs array_map to each item in collection.
  • indexBy($callback) - maps values to keys using callback.
  • mapKeys(array $replaces, $strict = true) - replaces collection keys using map array..
  • filter($callback = null) - performs array_filter to each item in collection.
  • diff($array) - Removes from collection all items that are present in $array using array_diff.
  • unique() - performs array_unique to collection.
  • merge($array) - merges another array to collection.
  • find($callback) - finds an item by callback function. Returns it's value.
  • reduce($callback, $initial = null) - performs array_reduce to collection. Returns resulting value.
  • count() - returns items count.
  • value() - returns collection data preserving keys.
  • values() - returns collection items as array indexed numerically.
  • asVariables() - converts collection to Variables. Can be used to clone collection.
  • asNumbers() - converts collection to Numbers. Can be used to clone collection.
  • asStrings() - converts collection to Strings. Can be used to clone collection.
  • asObjects() - converts collection to Objects. Can be used to clone collection.

Strings

  • static split($delimiter, $string) - creates new collection instance by splitting text like explode does.
  • static splitList($string) - creates new collection instance by splitting text wtih comma-separated values.
  • lower() - makes every string in collection lowercase.
  • trim() - performs trim to each item in collection.
  • replace($search, $replace) - performs str_replace to each item in collection.

Numbers

  • sum() - returns collection items sum.

Objects

  • property($property) - cleates new collection with items picked from objects property.

Arrays

  • column($columnKey, $indexKey = null) - returns the values from a single column of the array, identified by the column_key. Optionally, you may provide an index_key to index the values in the returned array by the values from the index_key column in the input array.

    Performs array_column to collection. If php version is lower than 5.5, ramsey/array_column is used.

  • indexByColumn($columnKey) - uses the values from a single column of the array as collection keys.