Skip to content

Commit

Permalink
Add easy-collection files
Browse files Browse the repository at this point in the history
  • Loading branch information
andyexeter committed Aug 13, 2023
1 parent 7815e6b commit 23144b0
Show file tree
Hide file tree
Showing 15 changed files with 6,225 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
charset = utf-8
end_of_line = LF
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = space
indent_size = 4

[.github/workflows/*.yaml]
indent_size = 2
10 changes: 10 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/.github export-ignore
/tests export-ignore
/var export-ignore
/.editorconfig export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.php-cs-fixer.dist.php export-ignore
/composer.lock export-ignore
/phpunit.xml.dist export-ignore
/psalm.xml export-ignore
48 changes: 48 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Build

on: push

jobs:
run:
runs-on: 'ubuntu-20.04'
strategy:
matrix:
php-versions: [ '8.1', '8.2' ]
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}

- name: Declare variables
id: vars
run: |
echo ::set-output name=composer_cache_dir::$(composer config cache-dir)
- name: Cache composer downloads
uses: actions/cache@v3
with:
path: ${{ steps.vars.outputs.composer_cache_dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-composer-
- name: Lint PHP
run: find -L src -type f -name '*.php' -print0 | xargs -0 -n 1 -P $(nproc) php -l

- name: Install dependencies
run: composer install --prefer-dist --no-interaction --no-ansi

- name: PHP-CS-Fixer
run: ./vendor/bin/php-cs-fixer fix -v --dry-run --stop-on-violation --using-cache=no --diff
if: matrix.php-versions == 8.1

- name: Psalm
run: ./vendor/bin/psalm --config=psalm.xml --no-cache
if: matrix.php-versions == 8.1

- name: PHPUnit
run: ./vendor/bin/phpunit -v
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/vendor
/phpunit.xml
/.php-cs-fixer.php
17 changes: 17 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

use Palmtree\PhpCsFixerConfig\Config;

$config = new Config();

$config
->setCacheFile(__DIR__ . '/var/.php-cs-fixer.cache')
->getFinder()
->in(__DIR__ . '/src')
->in(__DIR__ . '/tests')
->append([__FILE__])
;

return $config;
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2021, Andy Palmer

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
82 changes: 82 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# :palm_tree: Easy Collection

[![License](https://img.shields.io/github/license/palmtreephp/easy-collection)](LICENSE)
[![Build](https://img.shields.io/github/workflow/status/palmtreephp/easy-collection/Build.svg)](https://github.com/palmtreephp/easy-collection/actions/workflows/build.yml)

Immutable collections which provide commonly used functionality missing from arrays.

## Requirements

* PHP >= 8.1

## Installation

Use composer to add the package to your dependencies:

```bash
composer require palmtree/easy-collection
```

## Usage

Collections can be used just like arrays for the most part. They implement [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess), [`Traversable`](https://www.php.net/manual/en/class.traversable) and [`Countable`](https://www.php.net/manual/en/class.countable):

```php
use function Palmtree\EasyCollection\c;

$collection = c(['foo' => 'bar', 'baz' => 'qux']);

$foo = $collection['foo'];
$collection['baz2'] = 'qux';

isset($collection['baz']); // true
unset($collection['baz']);
count($collection);

foreach ($collection as $key => $value) {
// do stuff with value and/or key
}

// find returns the first matching element
$foo = $collection->find(fn ($v) => $v === 'bar');

// filter returns a new filtered collection
$quxCollection = $collection->filter(fn ($v) => $v === 'qux');
```

```php
use function Palmtree\EasyCollection\c;

$collection = c([1, 9, 5, 3, 7, 10])
->sort()
->filter(fn ($i) $i < 10)
->values()

// returns true as every element remaining in the collection is odd
$isAllOdd = $collection->every(fn ($i) => $i % 2 !== 0);
// returns false as it was removed in our filter
$collection->contains(10);

// returns true as at least one of the elements is false
c([true, true, false, true, true])->some(fn ($v) => !$v);
```

Many other methods are provided. Read through the documented [source code](src/Collection.php) to see more.

### Generics

The library supports template annotations for use by static analysers such as Psalm and PHPStan:

```php
/** @var Collection<int, Foo> **/
$collection = new Collection();
$collection->add(new Foo());

foreach ($collection as $foo) {
// Psalm/PHPStan and PhpStorm know that $foo is an instance of Foo here
}
```

## License

Released under the [MIT license](LICENSE)
44 changes: 44 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "palmtree/easy-collection",
"description": "Immutable collections which provide commonly used functionality missing from arrays",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Andy Palmer",
"email": "[email protected]",
"homepage": "https://andypalmer.me"
}
],
"require": {
"php": "^8.1"
},
"autoload": {
"psr-4": {
"Palmtree\\EasyCollection\\": "src"
},
"files": [
"functions.php"
]
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"vimeo/psalm": "^5.2",
"palmtree/php-cs-fixer-config": "^2.0"
},
"autoload-dev": {
"psr-4": {
"Palmtree\\EasyCollection\\Test\\": "tests"
}
},
"scripts": {
"fix": "@php vendor/bin/php-cs-fixer fix",
"psalm": "@php vendor/bin/psalm --no-cache",
"test": "@php vendor/bin/phpunit"
},
"config": {
"platform": {
"php": "8.1"
}
}
}
Loading

0 comments on commit 23144b0

Please sign in to comment.