Skip to content

Commit

Permalink
Version 4:
Browse files Browse the repository at this point in the history
- introducing Lists (ListInterface) based on Kotlin
- introducing new data structure Map
- and a lot of other improvements and more tools for functional programming
  • Loading branch information
seboettg committed Jun 7, 2022
1 parent 408bfc9 commit 5e7a53c
Show file tree
Hide file tree
Showing 32 changed files with 1,976 additions and 888 deletions.
47 changes: 20 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,23 @@ composer.phar update
### Simple Usage ###

Initialize ArrayList

```php
<?php
use Seboettg\Collection\ArrayList;
$list = new ArrayList("a", "c", "h", "k", "j");
$map = new ArrayList();
$map->setArray([
"c" => "cc",
"b" => "bb",
"a" => "aa"
]);
use Seboettg\Collection\Lists;
use function Seboettg\Collection\Lists\listOf;
use function Seboettg\Collection\Map\emptyMap;
use function Seboettg\Collection\Map\pair;
$list = listOf("a", "c", "h", "k", "j");
$map = mapOf(pair("c", "cc"), pair("b", "bb"), pair("a", "aa"))
```

Get elements

```php
for ($i = 0; $i < $list->count(); ++$i) {
for ($i = 0; $i < $list->count(); ++$i)
{
echo $list->get($i)." ";
}
```
Expand All @@ -88,19 +88,11 @@ This will output:
a c h k j
```

ArrayList implements the ArrayAccess interface, so you can also access elements in an instance of ArrayList in exactly the same way as you access an element in arrays:

```php
for ($i = 0; $i < $list->count(); ++$i) {
echo $list[$i]." ";
}
```

Iterate ArrayList using foreach
Iterate a Map using foreach

```php
foreach ($map as $key => $value) {
foreach ($map as $key => $value)
{
echo "[".$key."] => ".$value."\n";
}
```
Expand Down Expand Up @@ -149,10 +141,11 @@ $list->clear(); //removes all elements of the list

#### Inherit from ArrayList ####
Inherit from ArrayList to extend your class with the whole functionality from ArrayList:

```php
<?php
namespace Vendor\Project;
use Seboettg\Collection\ArrayList;
use Seboettg\Collection\Lists;
class MyCustomList extends ArrayList {
protected $myCustomProperty;
Expand All @@ -165,13 +158,14 @@ class MyCustomList extends ArrayList {
```
Or implement ArrayListInterface and use the ArrayListTrait (which implements all functions that are required by the interface) in case of that your custom class inherits already from another class

```php
<?php
namespace Vendor\Project;
use Seboettg\Collection\ArrayList\ArrayListInterface;
use Seboettg\Collection\ArrayList\ArrayListTrait;
use Seboettg\Collection\Lists\ListInterface;
use Seboettg\Collection\Lists\ArrayListTrait;
class MyCustomList extends MyOtherCustomClass implements ArrayListInterface {
class MyCustomList extends MyOtherCustomClass implements ListInterface {
use ArrayListTrait;
Expand Down Expand Up @@ -240,7 +234,7 @@ Sort your list
```php
<?php
use Seboettg\Collection\ArrayList;
use Seboettg\Collection\Lists;
use Seboettg\Collection\Collections;
use Seboettg\Collection\Comparable\Comparator;
use Vendor\App\Util\Attribute1Comparator;
Expand All @@ -259,12 +253,11 @@ Collections::sort($list, new Attribute1Comparator(Comparator::ORDER_ASC));
#### sort your list using a custom order ####
```php
<?php
use Seboettg\Collection\Comparable\Comparator;
use Seboettg\Collection\Comparable\Comparable;
use Seboettg\Collection\ArrayList;
use Seboettg\Collection\Lists;
use Seboettg\Collection\Collections;
use Vendor\App\Model\Element;
Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@
"autoload": {
"psr-4": {"Seboettg\\Collection\\": "src/"},
"files": [
"src/ArrayList/Functions.php"
"src/Assert/Functions.php",
"src/Lists/Functions.php",
"src/Map/Functions.php"
]
},
"autoload-dev": {
"psr-4": {"Seboettg\\Collection\\Test\\": "tests/"}
},
"require": {
"php": ">=7.3"
"php": ">=7.4"
},
"require-dev": {
"phpunit/phpunit": "8.5.*",
Expand Down
9 changes: 6 additions & 3 deletions src/ArrayList.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@

namespace Seboettg\Collection;

use Seboettg\Collection\ArrayList\ArrayListInterface;
use Seboettg\Collection\ArrayList\ArrayListTrait;
use ArrayIterator;
use Exception;
use Seboettg\Collection\Lists\ListInterface;
use Seboettg\Collection\Lists\ArrayListTrait;
use Traversable;

/**
* ArrayList is a useful wrapper class for an array, similar to Java's ArrayList
* @package Seboettg\Collection
*
* @author Sebastian Böttger <[email protected]>
*/
class ArrayList implements ArrayListInterface
class ArrayList implements ListInterface
{
use ArrayListTrait;

Expand Down
214 changes: 0 additions & 214 deletions src/ArrayList/ArrayListInterface.php

This file was deleted.

Loading

0 comments on commit 5e7a53c

Please sign in to comment.