Skip to content

Commit

Permalink
php 7.1 features
Browse files Browse the repository at this point in the history
  • Loading branch information
seboettg committed Dec 24, 2019
1 parent 704a56d commit 9c0cfb5
Show file tree
Hide file tree
Showing 19 changed files with 136 additions and 63 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![PHP](https://img.shields.io/badge/PHP-%3E=7.0-green.svg?style=flat)](http://docs.php.net/manual/en/migration70.new-features.php)
[![PHP](https://img.shields.io/badge/PHP-%3E=7.1-green.svg?style=flat)](http://docs.php.net/manual/en/migration71.new-features.php)
[![Total Downloads](https://poser.pugx.org/seboettg/collection/downloads)](https://packagist.org/packages/seboettg/collection/stats)
[![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](https://github.com/seboettg/Collection/blob/master/LICENSE)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/seboettg/Collection/badges/quality-score.png?b=version-2.0)](https://scrutinizer-ci.com/g/seboettg/Collection/?branch=master)
Expand All @@ -10,6 +10,9 @@

Collection is a set of useful wrapper classes for arrays, similar to Java Collection.

## Versions
Since [Version 2.1](https://github.com/seboettg/Collection/releases/tag/v2.1.0) you need PHP 7.1 to use Collection library. Previous versions are running from PHP 5.6 upwards.

### ArrayList, Stack, Queue ###
The current version comes with a [ArrayList](#arraylist), a [Stack](#stack), and a [Queue](#queue). These classes can be used as wrapper instead using simple arrays. This approach guarantees consistently object-oriented handling for collection-like data-structures.

Expand All @@ -25,6 +28,8 @@ ArrayList is powerful alternative with a lot of of features:

Take also a look to the UML [class diagram](#class-diagram).



# Installing Collection ##

The recommended way to install Collection is through
Expand Down Expand Up @@ -203,7 +208,7 @@ class Element implements Comparable
public function getAttribute2() { return $this->attribute2; }
//compareTo function
public function compareTo(Comparable $b)
public function compareTo(Comparable $b): int
{
return strcmp($this->attribute1, $b->getAttribute1());
}
Expand All @@ -221,7 +226,7 @@ use Seboettg\Collection\Comparable\Comparable;
class Attribute1Comparator extends Comparator
{
public function compare(Comparable $a, Comparable $b)
public function compare(Comparable $a, Comparable $b): int
{
if ($this->sortingOrder === Comparator::ORDER_ASC) {
return $a->compareTo($b);
Expand Down Expand Up @@ -266,7 +271,7 @@ use Vendor\App\Model\Element;
//Define a custom Comparator
class MyCustomOrderComparator extends Comparator
{
public function compare(Comparable $a, Comparable $b)
public function compare(Comparable $a, Comparable $b): int
{
return (array_search($a->getAttribute1(), $this->customOrder) >= array_search($b->getAttribute1(), $this->customOrder)) ? 1 : -1;
}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"psr-4": {"Seboettg\\Collection\\Test\\": "tests/"}
},
"require": {
"php": ">=7.0"
"php": ">=7.1"
},
"require-dev": {
"phpunit/phpunit": "6.5.*",
Expand Down
1 change: 1 addition & 0 deletions src/ArrayList.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2016 Sebastian Böttger <[email protected]>
* You may use, distribute and modify this code under the
Expand Down
1 change: 1 addition & 0 deletions src/ArrayList/ArrayAccessTrait.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2018 Sebastian Böttger <[email protected]>
* You may use, distribute and modify this code under the
Expand Down
75 changes: 62 additions & 13 deletions src/ArrayList/ArrayListInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2018 Sebastian Böttger <[email protected]>
* You may use, distribute and modify this code under the
Expand Down Expand Up @@ -37,7 +38,7 @@ public function get($key);
* @param $element
* @return ArrayListInterface
*/
public function set($key, $element);
public function set($key, $element): ArrayListInterface;

/**
* Returns the value of the array element that's currently being pointed to by the
Expand Down Expand Up @@ -69,17 +70,17 @@ public function prev();

/**
* @param array $array
* @return mixed
* @return ArrayListInterface
*/
public function replace(array $array);
public function replace(array $array): ArrayListInterface;

/**
* Appends the passed element to the end of this list.
*
* @param $element
* @return $this
* @return ArrayListInterface
*/
public function append($element);
public function append($element): ArrayListInterface;

/**
* Inserts the specified element at the specified position in this list. If another element already exist at the
Expand All @@ -88,33 +89,33 @@ public function append($element);
*
* @param $key
* @param $element
* @return $this
* @return ArrayListInterface
*/
public function add($key, $element);
public function add($key, $element): ArrayListInterface;

/**
* Removes the element at the specified position in this list.
*
* @param $key
* @return $this
* @return ArrayListInterface
*/
public function remove($key);
public function remove($key): ArrayListInterface;

/**
* Returns true if an element exists on the specified position.
*
* @param mixed $key
* @return bool
*/
public function hasKey($key);
public function hasKey($key): bool;

/**
* Returns true if the passed element already exists in this list, otherwise false.
*
* @param string $element
* @return mixed
* @return bool
*/
public function hasElement($element);
public function hasElement($element): bool;

/**
* Returns the first element in this list
Expand All @@ -128,11 +129,59 @@ public function first();
*/
public function last();

/**
* alias of replace function
* @param array $array
* @return ArrayListInterface
*/
public function setArray(array $array): ArrayListInterface;

/**
* flush array list
*
* @return ArrayListInterface
*/
public function clear(): ArrayListInterface;

/**
* Shuffles this list (randomizes the order of the elements in). It uses the PHP function shuffle
* @see http://php.net/manual/en/function.shuffle.php
* @return ArrayListInterface
*/
public function shuffle(): ArrayListInterface;

/**
* returns a clone of this ArrayList, filtered by the given closure function
* @param callable $filterFunction
* @return ArrayListInterface
*/
public function filter(callable $filterFunction): ArrayListInterface;

/**
* returns a clone of this ArrayList, filtered by the given array keys
* @param array $keys
* @return ArrayListInterface
*/
public function filterByKeys(array $keys): ArrayListInterface;

/**
* Merges the elements of the passed list together with this list so that the values of the passed list are appended
* to the end of the this list
* @param ArrayListInterface $list
* @return void
*/
public function merge(ArrayListInterface $list);
public function merge(ArrayListInterface $list): void;

/**
* returns a new ArrayList containing all the elements of this ArrayList after applying the callback function to each one.
* @param callable $mapFunction
* @return ArrayListInterface
*/
public function map(callable $mapFunction): ArrayListInterface;

/**
* Returns a new ArrayList containing an one-dimensional array of all elements of this ArrayList. Keys are going lost.
* @return ArrayListInterface
*/
public function flatten(): ArrayListInterface;
}
Loading

0 comments on commit 9c0cfb5

Please sign in to comment.