Skip to content

Commit

Permalink
add: class to menage collaction data from array
Browse files Browse the repository at this point in the history
  • Loading branch information
SonyPradana committed Nov 7, 2021
1 parent 75a58ca commit 98f5ddd
Show file tree
Hide file tree
Showing 5 changed files with 648 additions and 1 deletion.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [0.1.3] - 2021-11-07
### Added
- Add feture to ```generate::class``` class, property, const and method as string
- Add feature to ```generate::class``` class, property, const and method as string
- Add feature to collact array ```Collection::class``` and ```CollectionImmutable::class```

### Removed
- Remove ```Router::view```
Expand Down
156 changes: 156 additions & 0 deletions src/System/Collection/AbstractCollectionImmutable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php

namespace System\Collection;

abstract class AbstractCollectionImmutable
{
protected array $collection = [];

public function __construct(array $collection)
{
foreach ($collection as $key => $item) {
$this->set($key, $item);
}
}

public function __get($name)
{
return $this->get($name);
}

public function all(): array
{
return $this->collection;
}

public function get(string $name, $default = null)
{
return $this->collection[$name] ?? $default;
}

protected function set(string $name, $value)
{
$this->collection[$name] = $value;
return $this;
}

public function has(string $key)
{
return array_key_exists($key, $this->collection);
}

public function contain($item)
{
return in_array($item, $this->collection);
}

public function keys(): array
{
return array_keys($this->collection);
}

public function items(): array
{
return array_values($this->collection);
}

public function count(): int
{
return count($this->collection);
}

public function countBy(callable $condition): int
{
$count = 0;
foreach ($this->collection as $key => $item) {
$do_somethink = call_user_func($condition, $item, $key);

$count += $do_somethink === true ? 1 : 0;
}

return $count;
}

public function each(callable $callable)
{
if (! is_callable($callable)) {
return $this;
}

foreach ($this->collection as $key => $item) {
$do_somethink = call_user_func($callable, $item, $key);

// stop looping if callable returning false
if ($do_somethink === false) {
break;
}
}

return $this;
}

public function dumb()
{
var_dump($this->collection);

return $this;
}

public function some(callable $condition): bool
{
if (! is_callable($condition)) {
return $this;
}

foreach ($this->collection as $key => $item) {
$call = call_user_func($condition, $item, $key);

if ($call === true) {
return true;
}
}

return false;
}

public function every(callable $condition): bool
{
if (! is_callable($condition)) {
return $this;
}

foreach ($this->collection as $key => $item) {
$call = call_user_func($condition, $item, $key);

if ($call === false) {
return false;
}
}

return true;
}

public function json(): string
{
return json_encode($this->collection);
}

public function first($default = null)
{
$key = array_key_first($this->collection) ?? 0;
return $this->collection[$key] ?? $default;
}

public function last($default = null)
{
$key = array_key_last($this->collection);
return $this->collection[$key] ?? $default;
}

public function isEmpty(): bool
{
return empty($this->collection);
}

}

161 changes: 161 additions & 0 deletions src/System/Collection/Collection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?php

namespace System\Collection;

class Collection extends AbstractCollectionImmutable
{

public function __set($name, $value)
{
return $this->set($name, $value);
}

public function clear()
{
$this->collection = [];
return $this;
}

public function add(array $params)
{
foreach ($params as $key => $item) {
$this->set($key, $item);
}
return $this;
}

public function remove(string $name)
{
if ($this->has($name)) {
unset($this->collection[$name]);
}

return $this;
}

public function set(string $name, $value)
{
parent::set($name, $value);

return $this;
}

public function replace(array $new_collection)
{
$this->collection = [];
foreach ($new_collection as $key => $item) {
$this->set($key, $item);
}

return $this;
}

public function map(callable $callable)
{
if (! is_callable($callable)) {
return $this;
}

$new_collection = [];
foreach ($this->collection as $key => $item) {
$new_collection[$key] = call_user_func($callable, $item, $key);
}

$this->replace($new_collection);

return $this;
}

public function filter(callable $condition_true)
{
if (! is_callable($condition_true)) {
return $this;
}

$new_collection = [];
foreach ($this->collection as $key => $item) {
$call = call_user_func($condition_true, $item, $key);
$condition = is_bool($call) ? $call : false;

if ($condition === true) {
$new_collection[$key] = $item;
}
}

$this->replace($new_collection);

return $this;
}

public function reject(callable $condition_true)
{
if (! is_callable($condition_true)) {
return $this;
}

$new_collection = [];
foreach ($this->collection as $key => $item) {
$call = call_user_func($condition_true, $item, $key);
$condition = is_bool($call) ? $call : false;

if ($condition === false) {
$new_collection[$key] = $item;
}
}

$this->replace($new_collection);

return $this;
}

public function reverse()
{
return $this->replace(array_reverse($this->collection));
}

public function sort()
{
asort($this->collection);

return $this;
}

public function sortDesc()
{
arsort($this->collection);

return $this;
}

public function sortBy(callable $callable)
{
uasort($this->collection, $callable);

return $this;
}

public function sortByDecs(callable $callable)
{
return $this->sortBy($callable)->reverse();
}

public function sortKey()
{
ksort($this->collection);

return $this;
}

public function sortKeyDesc()
{
krsort($this->collection);

return $this;
}

public function clone()
{
return new Collection($this->collection);
}

}
9 changes: 9 additions & 0 deletions src/System/Collection/CollectionImmutable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace System\Collection;

class CollectionImmutable extends AbstractCollectionImmutable
{
// same as perent
}

Loading

0 comments on commit 98f5ddd

Please sign in to comment.