Skip to content

Commit

Permalink
+ php 8.0 support
Browse files Browse the repository at this point in the history
+ ArrayList extended with `collect` and `collectToString` methods
  • Loading branch information
seboettg committed May 7, 2022
1 parent 292a342 commit a081b13
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 5 deletions.
10 changes: 6 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@
}
],
"autoload": {
"psr-4": {"Seboettg\\Collection\\": "src/"}
"psr-4": {"Seboettg\\Collection\\": "src/"},
"files": [
"src/ArrayList/Functions.php"
]
},
"autoload-dev": {
"psr-4": {"Seboettg\\Collection\\Test\\": "tests/"}
},
"require": {
"php": ">=7.1"
"php": ">=7.3"
},
"require-dev": {
"phpunit/phpunit": "6.5.*",
"satooshi/php-coveralls": "2.*"
"phpunit/phpunit": "8.5.*"
}
}
20 changes: 20 additions & 0 deletions src/ArrayList/ArrayListInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,24 @@ public function map(callable $mapFunction): ArrayListInterface;
* @return ArrayListInterface
*/
public function flatten(): ArrayListInterface;

/**
* Expects a callable function which collects the elements of this list and returns any object. The callable
* function gets passed the entire array of the list
*
* @param callable $collectionFunction
* @return mixed
*/
public function collect(callable $collectionFunction);

/**
* Tries to convert each element of the list to a string and concatenates them with given delimiter.
* Throws a <code>NotConvertibleToStringException</code> if any of the objects in the list is not a string or is not
* convertible to string.
*
* @param string $delimiter
* @throws NotConvertibleToStringException
* @return string
*/
public function collectToString(string $delimiter): string;
}
34 changes: 34 additions & 0 deletions src/ArrayList/ArrayListTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Seboettg\Collection\ArrayList;

use function Seboettg\Collection\ArrayList\strval;

/**
* Trait ArrayListTrait
* @package Seboettg\Collection
Expand Down Expand Up @@ -251,4 +253,36 @@ public function merge(ArrayListInterface $list): void
{
$this->array = array_merge($this->array, $list->toArray());
}

/**
* @inheritDoc
* @param callable $collectFunction
* @return mixed
*/
public function collect(callable $collectFunction)
{
return $collectFunction($this->array);
}

/**
* @inheritDoc
* @param string $delimiter
* @return string
*/
public function collectToString(string $delimiter): string
{
return implode($delimiter, $this->map(function ($item) {
if (is_scalar($item)) {
return strval($item);
} else if (is_object($item)) {
if (method_exists($item, "toString")) {
return $item->toString();
}
}
throw new NotConvertibleToStringException(
"Couldn't collectToString since any object in list contains objects which are not " .
"convertible to string.");
})->toArray());
}
}

25 changes: 25 additions & 0 deletions src/ArrayList/Functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Seboettg\Collection\ArrayList;

final class Functions
{

public static function strval($value): string {
if (is_double($value)) {
$str = \strval($value);
if (strlen($str) == 1) {
return sprintf("%1\$.1f",$value);
}
return \strval($value);
}
if (is_bool($value)) {
return $value ? "true" : "false";
}
return "$value";
}
}

function strval($value): string {
return Functions::strval($value);
}
10 changes: 10 additions & 0 deletions src/ArrayList/NotConvertibleToStringException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Seboettg\Collection\ArrayList;

use Exception;

class NotConvertibleToStringException extends Exception
{

}
62 changes: 61 additions & 1 deletion tests/ArrayListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
use Seboettg\Collection\Collections;
use Seboettg\Collection\Comparable\Comparable;
use Seboettg\Collection\Comparable\Comparator;
use Seboettg\Collection\Stack;

use function Seboettg\Collection\ArrayList\strval;

class ArrayListTest extends TestCase
{
Expand All @@ -31,7 +34,7 @@ class ArrayListTest extends TestCase
private $hashMap;


public function setUp()
public function setUp(): void
{
$this->numeratedArrayList = new ArrayList(
new Element("a", "aa"),
Expand Down Expand Up @@ -323,6 +326,43 @@ public function testMerge()
$this->assertEquals(count($array), $first->count());
$this->assertEquals($first->toArray(), $array);
}

public function testCollect()
{
$arrayList = new ArrayList('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h');
/** @var Stack $stack */
$stack = $arrayList
->collect(function(array $list) {
$result = new Stack();
foreach ($list as $item) {
$result->push($item);
}
return $result;
});
$this->assertEquals(8, $stack->count());
$this->assertTrue('h' == $stack->pop());
}

public function testCollectToString()
{
$arrayList = new ArrayList('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h');
$result = $arrayList->collectToString(", ");
$this->assertEquals("a, b, c, d, e, f, g, h", $result);
}

public function testCollectToStringWithDoubleValues()
{
$arrayList = new ArrayList(1.0, 1.1, 1.2, 1.3);
$result = $arrayList->collectToString("; ");
$this->assertEquals("1.0; 1.1; 1.2; 1.3", $result);
}

public function testCollectToStringWithToStringObjects()
{
$arrayList = new ArrayList(new StringableObject(2), new StringableObject(3.1), new StringableObject(true));
$result = $arrayList->collectToString("; ");
$this->assertEquals("2; 3.1; true", $result);
}
}

class Element implements Comparable
Expand Down Expand Up @@ -391,3 +431,23 @@ public function compareTo(Comparable $b): int
return strcmp($this->attr1, $b->getAttr1());
}
}

class StringableObject {
private $value;
public function __construct($value)
{
$this->value = $value;
}
public function setValue($value) {
$this->value = $value;
}
public function getValue($value) {
return $value;
}
public function toString(): string {
return strval($this->value);
}
public function __toString(): string {
return $this->toString();
}
}

0 comments on commit a081b13

Please sign in to comment.