Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/stk2k/collection
Simple PHP collection class library
https://github.com/stk2k/collection
Last synced: 18 days ago
JSON representation
Simple PHP collection class library
- Host: GitHub
- URL: https://github.com/stk2k/collection
- Owner: stk2k
- License: mit
- Created: 2019-11-19T01:42:09.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2021-06-05T19:13:27.000Z (over 3 years ago)
- Last Synced: 2024-10-31T02:42:07.321Z (2 months ago)
- Language: PHP
- Size: 27.3 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Simple PHP collection class library
=======================[![Latest Version on Packagist](https://img.shields.io/packagist/v/stk2k/collection.svg?style=flat-square)](https://packagist.org/packages/stk2k/collection)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
[![Build Status](https://travis-ci.org/stk2k/collection.svg?branch=master)](https://travis-ci.org/stk2k/collection)
[![Coverage Status](https://coveralls.io/repos/github/stk2k/collection/badge.svg?branch=master)](https://coveralls.io/github/stk2k/collection?branch=master)
[![Code Climate](https://codeclimate.com/github/stk2k/collection/badges/gpa.svg)](https://codeclimate.com/github/stk2k/collection)
[![Total Downloads](https://img.shields.io/packagist/dt/stk2k/collection.svg?style=flat-square)](https://packagist.org/packages/stk2k/collection)## Description
Simple PHP collection class library
## Feature
- Sortable(ArrayList/Queue/Stack/Vector)
## Supported Data Structure
- ArrayList
- Collection
- Property(supports hierarchical access to array for string/int/float/bool values)
- Queue
- Set
- Stack
- Vector## Demo
### Collection
```php
$data = ['red', 'green', 'blue'];echo 'iterate:' . PHP_EOL;
$output = [];
foreach(new Collection($data) as $item){
$output[] = $item;
}
echo ' ' . implode(',', $output) . PHP_EOL; // red,green,blueecho 'join:' . PHP_EOL;
echo ' ' . (new Collection($data))->join() . PHP_EOL; // red,green,blueecho 'replace:' . PHP_EOL;
echo ' ' . (new Collection($data))->replace('green', 'yellow')->join() . PHP_EOL; // red,yellow,blueecho 'each:' . PHP_EOL;
(new Collection($data))->each(function($item){
echo "[$item],";
}); // [red],[green],[blue],echo 'map:' . PHP_EOL;
echo ' ' . (new Collection($data))->map(function($item){
return "[$item]";
})->join() . PHP_EOL; // [red],[green],[blue]echo 'reduce:' . PHP_EOL;
echo ' ' . (new Collection($data))->reduce(function($tmp,$item){
return $tmp + strlen($item);
}) . PHP_EOL; // 12```
### ArrayList
```php
$data = ['red', 'green', 'blue'];echo 'iterate:' . PHP_EOL;
$output = [];
foreach(new ArrayList($data) as $item){
$output[] = $item;
}
echo ' ' . implode(',', $output) . PHP_EOL; // red,green,blueecho 'join:' . PHP_EOL;
echo ' ' . (new ArrayList($data))->join() . PHP_EOL; // red,green,blueecho 'first:' . PHP_EOL;
echo ' ' . (new ArrayList($data))->first() . PHP_EOL; // redecho 'last:' . PHP_EOL;
echo ' ' . (new ArrayList($data))->last() . PHP_EOL; // blueecho 'reverse:' . PHP_EOL;
echo ' ' . (new ArrayList($data))->reverse()->join() . PHP_EOL; // blue,green,redecho 'replace then reverse:' . PHP_EOL;
echo ' ' . (new ArrayList($data))->replace('green', 'yellow')->reverse()->join() . PHP_EOL; // blue,yellow,redecho 'shift:' . PHP_EOL;
echo ' ' . (new ArrayList($data))->shift($item)->join() . PHP_EOL; // green,blueecho 'unshift:' . PHP_EOL;
echo ' ' . (new ArrayList($data))->unshift('yellow')->join() . PHP_EOL; // yellow,red,green,blueecho 'push:' . PHP_EOL;
echo ' ' . (new ArrayList($data))->push('yellow')->join() . PHP_EOL; // red,green,blue,yellowecho 'pop:' . PHP_EOL;
echo ' ' . (new ArrayList($data))->pop($item)->join() . PHP_EOL; // red,greenecho 'sort:' . PHP_EOL;
echo ' ' . (new ArrayList($data))->sort()->join() . PHP_EOL; // blue,green,red```
### Vector
```php
$data = ['red', 'green', 'blue'];echo 'iterate:' . PHP_EOL;
$output = [];
foreach(new Vector($data) as $item){
$output[] = $item;
}
echo ' ' . implode(',', $output) . PHP_EOL; // red,green,blueecho 'join:' . PHP_EOL;
echo ' ' . (new Vector($data))->join() . PHP_EOL; // red,green,blueecho 'first:' . PHP_EOL;
echo ' ' . (new Vector($data))->first() . PHP_EOL; // redecho 'last:' . PHP_EOL;
echo ' ' . (new Vector($data))->last() . PHP_EOL; // blueecho 'reverse:' . PHP_EOL;
echo ' ' . (new Vector($data))->reverse()->join() . PHP_EOL; // blue,green,redecho 'replace then reverse:' . PHP_EOL;
echo ' ' . (new Vector($data))->replace('green', 'yellow')->reverse()->join() . PHP_EOL; // blue,yellow,redecho 'shift:' . PHP_EOL;
echo ' ' . (new Vector($data))->shift($item)->join() . PHP_EOL; // green,blueecho 'unshift:' . PHP_EOL;
echo ' ' . (new Vector($data))->unshift('yellow')->join() . PHP_EOL; // yellow,red,green,blueecho 'push:' . PHP_EOL;
echo ' ' . (new Vector($data))->push('yellow')->join() . PHP_EOL; // red,green,blue,yellowecho 'pop:' . PHP_EOL;
echo ' ' . (new Vector($data))->pop($item)->join() . PHP_EOL; // red,greenecho 'sort:' . PHP_EOL;
echo ' ' . (new Vector($data))->sort()->join() . PHP_EOL; // blue,green,red```
### Stack
```php
$data = ['red', 'green', 'blue'];echo 'iterate:' . PHP_EOL;
$output = [];
foreach(new Stack($data) as $item){
$output[] = $item;
}
echo ' ' . implode(',', $output) . PHP_EOL; // red,green,blueecho 'join:' . PHP_EOL;
echo ' ' . (new Stack($data))->join() . PHP_EOL; // red,green,blueecho 'peek:' . PHP_EOL;
echo ' ' . (new Stack($data))->peek() . PHP_EOL; // redecho 'reverse:' . PHP_EOL;
echo ' ' . (new Stack($data))->reverse()->join() . PHP_EOL; // blue,green,redecho 'replace then reverse:' . PHP_EOL;
echo ' ' . (new Stack($data))->replace('green', 'yellow')->reverse()->join() . PHP_EOL; // blue,yellow,redecho 'push:' . PHP_EOL;
echo ' ' . (new Stack($data))->push('yellow')->join() . PHP_EOL; // red,green,blue,yellowecho 'pop:' . PHP_EOL;
echo ' ' . (new Stack($data))->pop($item)->join() . PHP_EOL; // red,greenecho 'sort:' . PHP_EOL;
echo ' ' . (new Stack($data))->sort()->join() . PHP_EOL; // blue,green,red```
### HashMap
```php
$data = ['name' => 'David', 'age' => 21, 'height' => 172.2];echo 'iterate:' . PHP_EOL;
$output = [];
foreach(new HashMap($data) as $item){
$output[] = $item;
}
echo ' ' . implode(',', $output) . PHP_EOL; // David,21,172.2echo 'join:' . PHP_EOL;
echo ' ' . (new HashMap($data))->join() . PHP_EOL; // David,21,172.2```
### Set
```php
$data = ['red', 'green', 'blue'];echo 'iterate:' . PHP_EOL;
$output = [];
foreach(new Set($data) as $item){
$output[] = $item;
}
echo ' ' . implode(',', $output) . PHP_EOL; // red,green,blueecho 'join:' . PHP_EOL;
echo ' ' . (new Set($data))->join() . PHP_EOL; // red,green,blue```
## Requirement
PHP 7.2 or later
## Installing stk2k/collection
The recommended way to install stk2k/collection is through
[Composer](http://getcomposer.org).```bash
composer require stk2k/collection
```After installing, you need to require Composer's autoloader:
```php
require 'vendor/autoload.php';
```## License
This library is licensed under the MIT license.## Author
[stk2k](https://github.com/stk2k)
## Disclaimer
This software is no warranty.
We are not responsible for any results caused by the use of this software.
Please use the responsibility of the your self.