collections

Fluent collection and array pipeline utilities for HolyPHP.

v1.0.0Data structuresbundledimport "collections";
Overview Installation Documentation Examples

Overview

Fluent collection and array pipeline utilities for HolyPHP.

bundled — works offline array pipeline map

Quick start

import "collections";

$result = collect([1, 2, 2, 3, 4, 5, 6, 7, 8])
    ->unique()
    ->filter(function($x) { return $x % 2 === 0; })
    ->map(function($x) { return $x * 10; })
    ->skip(1)
    ->take(2)
    ->toArray();

// $result => [40, 60]

Installation

In a project

Inside a project (a folder with hphp.yaml, see Projects & packages):

hphp install collections

This adds the library to hphp.yaml, installs it into .hphp/packages/collections/ and pins the exact version in hphp.lock:

require:
  collections: "^1.0"

Use it

import "collections";

collections is bundled with the compiler, so import "collections"; also works in a single file without installing anything, and hphp install works offline.

Outside a project

hphp install collections          # for your user, shared by all scripts
hphp create myapp                 # or start a project, then: hphp install collections
Packagecollections
Version1.0.0 (follows the compiler)
Entry filecollections.hphp
Sourcepackages/lib/collections ↗

Documentation

API Reference

collect($items = []): Collection

Wraps an array (or clones an existing Collection) into a fluent Collection instance.

Collection Class

Every transformation method returns a new Collection instance (immutable pipeline), while terminal methods return a value:

Standalone Helpers

All operations are also available as standalone collections_* functions:

Error Handling

All functions validate their inputs and throw Exception with clear collections: ... messages:

Examples

01Quick start

Bundled with the compiler — import "collections"; works offline in any file, or add it to a project with hphp install collections.

import "collections";

$result = collect([1, 2, 2, 3, 4, 5, 6, 7, 8])
    ->unique()
    ->filter(function($x) { return $x % 2 === 0; })
    ->map(function($x) { return $x * 10; })
    ->skip(1)
    ->take(2)
    ->toArray();

// $result => [40, 60]
Try it
hphp install collections
hphp run main.hphp

Covered by the official test suite in tests/positive/lib_collections.hphp.