daily pastebin goal
79%
SHARE
TWEET

Untitled

a guest Jan 20th, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. namespace App\Model;
  3.  
  4. use Nette\Utils\Json;
  5. use Tracy\Debugger;
  6.  
  7. class Arrays extends \Nette\Utils\Arrays
  8. {
  9.     public static function filter(array $arr, callable $callback): array
  10.     {
  11.         return array_filter($arr, $callback, ARRAY_FILTER_USE_BOTH);
  12.     }
  13.  
  14.     public static function collect($arr): Collection
  15.     {
  16.         $col = new Collection($arr);
  17.         return $col;
  18.     }
  19. }
  20.  
  21. class Collection
  22. {
  23.     /** @var array */
  24.     protected $items;
  25.  
  26.     public function __construct($list)
  27.     {
  28.         $this->items = $this->tryConvertToArray($list);
  29.         return $this;
  30.     }
  31.  
  32.     /**
  33.      * @throws \RuntimeException
  34.      */
  35.     public function tryConvertToArray($list): array
  36.     {
  37.         if (is_array($list)) {
  38.             return $list;
  39.         } else if ($list instanceof \Traversable) {
  40.             return iterator_to_array($list);
  41.         } else {
  42.             throw new \RuntimeException();
  43.         }
  44.     }
  45.  
  46.     public function map(callable $function): self
  47.     {
  48.         $this->items = Arrays::map($this->items, $function);
  49.         return $this;
  50.     }
  51.  
  52.     public function filter(callable $function): self
  53.     {
  54.         $this->items = Arrays::filter($this->items, $function);
  55.         return $this;
  56.     }
  57.  
  58.     public function column($key): self
  59.     {
  60.         $this->items = array_column($this->items, $key);
  61.         return $this;
  62.     }
  63.  
  64.     /**
  65.      * @param $key mixed
  66.      * @param mixed|null $default
  67.      * @return mixed
  68.      */
  69.     public function get($key, $default = null)
  70.     {
  71.         return Arrays::get($this->items, $key, $default);
  72.     }
  73.  
  74.     public function shuffle(): self
  75.     {
  76.         $this->shuffle($this->items);
  77.         return $this;
  78.     }
  79.  
  80.     public function flatten(): self
  81.     {
  82.         $this->items = Arrays::flatten($this->items);
  83.         return $this;
  84.     }
  85.    
  86.     public function values(): self
  87.     {
  88.         $this->items = array_values($this->items);
  89.         return $this;
  90.     }
  91.  
  92.     public function keys(): self
  93.     {
  94.         $this->items = array_keys($this->items);
  95.         return $this;
  96.     }
  97.  
  98.     public function dump(bool $bar = false, $barName = null): self
  99.     {
  100.         if ($bar) {
  101.             Debugger::barDump($this->items, $barName);
  102.         } else {
  103.             dump($this->items);
  104.         }
  105.         return $this;
  106.     }
  107.  
  108.     public function assoc(string $path): self
  109.     {
  110.         $this->items = Arrays::associate($this->items, $path);
  111.         return $this;
  112.     }
  113.  
  114.     public function flip(): self
  115.     {
  116.         $this->items = array_flip($this->items);
  117.         return $this;
  118.     }
  119.  
  120.     public function reverse(): self
  121.     {
  122.         $this->items = array_reverse($this->items);
  123.         return $this;
  124.     }
  125.  
  126.     public function toList(): array
  127.     {
  128.         return array_values($this->items);
  129.     }
  130.  
  131.     public function toArray(): array
  132.     {
  133.         return array_values($this->items);
  134.     }
  135.  
  136.     public function toJson($opts = null): string
  137.     {
  138.         return Json::encode($this->items, $opts);
  139.     }
  140.  
  141.     public function count(): int
  142.     {
  143.         return count($this->items);
  144.     }
  145. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top