Skip to content

Commit

Permalink
Merge pull request #1 from sauls/1.x
Browse files Browse the repository at this point in the history
v1.1.1
  • Loading branch information
sauls committed Feb 22, 2018
2 parents cb68085 + 5a3f940 commit 88a1924
Show file tree
Hide file tree
Showing 13 changed files with 304 additions and 135 deletions.
16 changes: 11 additions & 5 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Sauls Collections

[![Build Status](https://travis-ci.org/sauls/collections.svg?branch=master)](https://travis-ci.org/sauls/collections)
[![Latest Stable Version](https://poser.pugx.org/sauls/collections/v/stable)](https://packagist.org/packages/sauls/collections)
[![Total Downloads](https://poser.pugx.org/sauls/collections/downloads)](https://packagist.org/packages/sauls/collections)
[![Coverage Status](https://coveralls.io/repos/github/sauls/collections/badge.svg?branch=master)](https://coveralls.io/github/sauls/collections?branch=master)
[![Packagist](https://img.shields.io/packagist/v/sauls/collections.svg)](https://packagist.org/packages/sauls/collections)
[![Total Downloads](https://img.shields.io/packagist/dt/sauls/collections.svg)](https://packagist.org/packages/sauls/collections)
[![Coverage Status](https://img.shields.io/coveralls/github/sauls/collections.svg)](https://coveralls.io/github/sauls/collections?branch=master)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/sauls/collections/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/sauls/collections/?branch=master)
[![License](https://poser.pugx.org/sauls/collections/license)](https://packagist.org/packages/sauls/collections)
[![License](https://img.shields.io/github/license/sauls/collections.svg)](https://packagist.org/packages/sauls/collections)

Various collections to store and retrieve your data.

Expand All @@ -19,6 +19,7 @@ PHP >= 7.2
```bash
$ composer require sauls/collections
```

### Apppend the composer.json file manually
```json
{
Expand All @@ -30,7 +31,12 @@ $ composer require sauls/collections

## Documentation

Available collections
### Available collections

* ArrayCollection
* ImmutableArrayCollection

### Additional type converters to [sauls/helpers](https://github.com/sauls/helpers) `function convert_to`

* CollectionToArrayConverter
* ArrayableToArrayConverter
12 changes: 10 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@
"sauls/helpers": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^6.5",
"phpunit/phpunit": "^7.0",
"php-coveralls/php-coveralls": "^2.0"
},
"autoload": {
"psr-4": {
"Sauls\\Component\\Collection\\": "src/"
},
"files": [
"src/Converter/converters.php"
],
"exclude-from-classmap": [
"../tests"
]
Expand All @@ -30,5 +33,10 @@
"Sauls\\Component\\Collection\\": "tests/"
}
},
"minimum-stability": "dev"
"minimum-stability": "dev",
"extra": {
"branch-alias": {
"dev-master": "v1.x-dev"
}
}
}
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<directory suffix=".php">src/</directory>
<exclude>
<file>src/ImmutableArrayCollection.php</file>
<file>src/Converter/converters.php</file>
</exclude>

</whitelist>
Expand Down
62 changes: 21 additions & 41 deletions src/ArrayCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,16 @@
namespace Sauls\Component\Collection;

use function Sauls\Component\Helper\array_deep_search;
use function Sauls\Component\Helper\array_diff_key_assoc;
use function Sauls\Component\Helper\array_get_value;
use function Sauls\Component\Helper\array_key_assoc;
use function Sauls\Component\Helper\array_remove_key;
use function Sauls\Component\Helper\array_remove_value;
use function Sauls\Component\Helper\array_merge;
use function \Sauls\Component\Helper\array_key_exists;
use function Sauls\Component\Helper\array_key_exists;
use function Sauls\Component\Helper\array_set_value;
use Sauls\Component\Helper\Exception\PropertyNotAccessibleException;
use function Sauls\Component\Helper\array_keys;
use function Sauls\Component\Helper\convert_to;

class ArrayCollection implements Collection, \Serializable
class ArrayCollection implements Collection, \JsonSerializable
{
/**
* @var array
Expand All @@ -32,23 +31,10 @@ class ArrayCollection implements Collection, \Serializable

public function __construct($elements = null)
{
$this->add($this->assureArray($elements));
$this->add(convert_to($elements, 'array'));
}

private function assureArray($elements)
{
if (\is_array($elements)) {
return $elements;
}

if ($elements instanceof \Traversable) {
return iterator_to_array($elements);
}

return (array) $elements;
}

public function create(array $elements): Collection
public static function create(array $elements): Collection
{
return new static($elements);
}
Expand Down Expand Up @@ -106,14 +92,14 @@ public function all(): array
return $this->elements;
}

public function filter(\Closure $function)
public function filter(\Closure $function): Collection
{
return \array_filter($this->elements, $function, ARRAY_FILTER_USE_BOTH);
return static::create(\array_filter($this->elements, $function, ARRAY_FILTER_USE_BOTH));
}

public function map(\Closure $function)
public function map(\Closure $function): Collection
{
return \array_map($function, $this->elements);
return static::create(\array_map($function, $this->elements));
}

public function keyOrValueExists($keyOrValue): bool
Expand All @@ -125,17 +111,17 @@ public function keyOrValueExists($keyOrValue): bool
return $this->valueExists($keyOrValue);
}

public function keyOrValueDoesNotExists($keyOrValue): bool
public function keyOrValueDoesNotExist($keyOrValue): bool
{
return false === $this->keyOrValueExists($keyOrValue);
}

public function keyExists($key): bool
{
return array_key_exists($this->elements, $key);
return array_key_exists($key, $this->elements);
}

public function keyDoesNotExists($key): bool
public function keyDoesNotExist($key): bool
{
return false === $this->keyExists($key);
}
Expand All @@ -145,7 +131,7 @@ public function valueExists($value): bool
return empty(array_deep_search($this->elements, $value)) ? false : true;
}

public function valueDoesNotExists($value): bool
public function valueDoesNotExist($value): bool
{
return false === $this->valueExists($value);
}
Expand All @@ -172,15 +158,14 @@ public function __toString(): string

public function getHash(): string
{
return md5($this->serialize());
return md5(\serialize($this->elements));
}

public function getSplHash(): string
{
return \spl_object_hash($this);
}


public function getIterator()
{
return new \ArrayIterator($this->elements);
Expand Down Expand Up @@ -211,23 +196,18 @@ public function count(): int
return \count($this->elements);
}

public function serialize(): string
{
return \serialize($this->elements);
}

public function unserialize($value): void
{
$this->elements = \unserialize($value);
}

public function keys(): Collection
{
return $this->create(array_key_assoc($this->elements));
return static::create(array_keys($this->elements));
}

protected function assign(array $elements): void
{
$this->elements = $elements;
}

public function jsonSerialize(): array
{
return $this->all();
}
}
18 changes: 18 additions & 0 deletions src/Arrayable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* This file is part of the sauls/collections package.
*
* @author Saulius Vaičeliūnas <vaiceliunas@inbox.lt>
* @link http://saulius.vaiceliunas.lt
* @copyright 2018
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sauls\Component\Collection;

interface Arrayable
{
public function toArray(): array;
}
15 changes: 9 additions & 6 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

interface Collection extends \Countable, \ArrayAccess, \IteratorAggregate
{
public function create(array $elements): Collection;
public static function create(array $elements): Collection;
public function set($key, $value): void;
public function add(array $elements): void;
public function get($key, $default = null);
Expand All @@ -25,18 +25,21 @@ public function removeValue($element);
public function slice($offset, $length = null): array;
public function clear(): void;
public function all(): array;
public function filter(\Closure $function);

public function filter(\Closure $function): Collection;
public function keys(): Collection;
public function map(\Closure $function);
public function map(\Closure $function): Collection;

public function keyOrValueExists($keyOrValue): bool;
public function keyOrValueDoesNotExists($keyOrValue): bool;
public function keyOrValueDoesNotExist($keyOrValue): bool;
public function keyExists($key): bool;
public function keyDoesNotExists($key): bool;
public function keyDoesNotExist($key): bool;
public function valueExists($value): bool;
public function valueDoesNotExists($value): bool;
public function valueDoesNotExist($value): bool;
public function valueIsNull($key): bool;
public function valueIsNotNull($key): bool;
public function isEmpty(): bool;

public function __toString(): string;
public function getHash(): string;
public function getSplHash(): string;
Expand Down
39 changes: 39 additions & 0 deletions src/Converter/Type/ArrayableToArrayConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* This file is part of the sauls/collections package.
*
* @author Saulius Vaičeliūnas <vaiceliunas@inbox.lt>
* @link http://saulius.vaiceliunas.lt
* @copyright 2018
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sauls\Component\Collection\Converter\Type;

use Sauls\Component\Collection\Arrayable;
use Sauls\Component\Helper\Operation\TypeOperation\Converter\ConverterInterface;

class ArrayableToArrayConverter implements ConverterInterface
{
public function convert($value): array
{
return $value->toArray();
}

public function supports($value): bool
{
return is_subclass_of($value, Arrayable::class);
}

public function getType(): string
{
return 'array';
}

public function getPriority(): int
{
return 512;
}
}
42 changes: 42 additions & 0 deletions src/Converter/Type/CollectionToArrayConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* This file is part of the sauls/collections package.
*
* @author Saulius Vaičeliūnas <vaiceliunas@inbox.lt>
* @link http://saulius.vaiceliunas.lt
* @copyright 2018
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sauls\Component\Collection\Converter\Type;

use Sauls\Component\Collection\Collection;
use Sauls\Component\Helper\Operation\TypeOperation\Converter\ConverterInterface;

class CollectionToArrayConverter implements ConverterInterface
{
/**
* @param Collection $value
*/
public function convert($value): array
{
return $value->all();
}

public function supports($value): bool
{
return is_subclass_of($value, Collection::class);
}

public function getType(): string
{
return 'array';
}

public function getPriority(): int
{
return 1024;
}
}
20 changes: 20 additions & 0 deletions src/Converter/converters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/**
* This file is part of the sauls/collections package.
*
* @author Saulius Vaičeliūnas <vaiceliunas@inbox.lt>
* @link http://saulius.vaiceliunas.lt
* @copyright 2018
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use function Sauls\Component\Helper\register_converters;

\spl_autoload_register(function() {
register_converters([
new Sauls\Component\Collection\Converter\Type\CollectionToArrayConverter,
new Sauls\Component\Collection\Converter\Type\ArrayableToArrayConverter
]);
});
4 changes: 2 additions & 2 deletions src/ImmutableArrayCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct($elements = null)
{
$this->assign((new ArrayCollection($elements))->all());
}

/**
* @throws \Sauls\Component\Collection\Exception\UnsupportedOperationException
*/
Expand Down Expand Up @@ -88,7 +88,7 @@ public function unserialize($value): void
/**
* @throws \Sauls\Component\Collection\Exception\UnsupportedOperationException
*/
public function map(\Closure $function)
public function map(\Closure $function): Collection
{
throw new UnsupportedOperationException();
}
Expand Down

0 comments on commit 88a1924

Please sign in to comment.