-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add: class to menage collaction data from array
- Loading branch information
1 parent
75a58ca
commit 98f5ddd
Showing
5 changed files
with
648 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
Oops, something went wrong.