mirror of
https://github.com/filegator/filegator.git
synced 2025-07-31 05:20:13 +02:00
initial commit
This commit is contained in:
58
backend/Utils/Collection.php
Normal file
58
backend/Utils/Collection.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the FileGator package.
|
||||
*
|
||||
* (c) Milos Stojanovic <alcalbg@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE file
|
||||
*/
|
||||
|
||||
namespace Filegator\Utils;
|
||||
|
||||
trait Collection
|
||||
{
|
||||
protected $items = [];
|
||||
|
||||
public function add($obj)
|
||||
{
|
||||
return $this->items[] = $obj;
|
||||
}
|
||||
|
||||
public function delete($obj)
|
||||
{
|
||||
foreach ($this->items as $key => $item) {
|
||||
if ($item === $obj) {
|
||||
unset($this->items[$key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function all()
|
||||
{
|
||||
return $this->items;
|
||||
}
|
||||
|
||||
public function length()
|
||||
{
|
||||
return count($this->items);
|
||||
}
|
||||
|
||||
public function jsonSerialize()
|
||||
{
|
||||
return $this->items;
|
||||
}
|
||||
|
||||
public function sortByValue($value, $desc = false)
|
||||
{
|
||||
usort($this->items, function ($a, $b) use ($value) {
|
||||
return $a[$value] <=> $b[$value];
|
||||
});
|
||||
|
||||
if ($desc) {
|
||||
$this->items = array_reverse($this->items);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user