Overview

Namespaces

  • Deimos
    • Helper
      • Exceptions
      • Helpers
        • Arr
        • Str
      • Traits
  • PHP

Classes

  • Deimos\Helper\AbstractHelper
  • Deimos\Helper\Helper
  • Deimos\Helper\Helpers\Arr\Arr
  • Deimos\Helper\Helpers\Dir
  • Deimos\Helper\Helpers\File
  • Deimos\Helper\Helpers\Json
  • Deimos\Helper\Helpers\Math
  • Deimos\Helper\Helpers\Money
  • Deimos\Helper\Helpers\Str\Str

Interfaces

  • Deimos\Helper\InterfaceHelper
  • Throwable

Traits

  • Deimos\Helper\Helpers\Arr\KeyTrait
  • Deimos\Helper\Helpers\Arr\StackTrait
  • Deimos\Helper\Helpers\Str\DefaultTrait
  • Deimos\Helper\Traits\Helper

Exceptions

  • Deimos\Helper\Exceptions\ExceptionEmpty
  • Exception
  • InvalidArgumentException
  • LogicException
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: 
  3: namespace Deimos\Helper\Helpers\Arr;
  4: 
  5: use Deimos\Helper\AbstractHelper;
  6: use Deimos\Helper\Exceptions\ExceptionEmpty;
  7: 
  8: class Arr extends AbstractHelper
  9: {
 10: 
 11:     use KeyTrait;
 12:     use StackTrait;
 13: 
 14:     /**
 15:      * @param array    $storage
 16:      * @param callable $callback
 17:      *
 18:      * @return array
 19:      */
 20:     public function map(array $storage, $callback)
 21:     {
 22:         return array_map($callback, $storage);
 23:     }
 24: 
 25:     /**
 26:      * @param array    $storage
 27:      * @param callable $callback
 28:      *
 29:      * @return array
 30:      */
 31:     public function filter(array $storage, $callback)
 32:     {
 33:         return array_filter($storage, $callback, ARRAY_FILTER_USE_BOTH);
 34:     }
 35: 
 36:     /**
 37:      * @param     $string
 38:      * @param     $length
 39:      * @param int $start
 40:      *
 41:      * @return array
 42:      */
 43:     public function fill($string, $length, $start = 0)
 44:     {
 45:         return array_fill($start, $length, $string);
 46:     }
 47: 
 48:     /**
 49:      * @param mixed $begin
 50:      * @param mixed $end
 51:      * @param int   $step
 52:      *
 53:      * @return array
 54:      */
 55:     public function range($begin, $end, $step = 1)
 56:     {
 57:         return range($begin, $end, $step);
 58:     }
 59: 
 60:     /**
 61:      * @param array $storage
 62:      * @param mixed $needle
 63:      *
 64:      * @return bool
 65:      */
 66:     public function inStrict(array $storage, $needle)
 67:     {
 68:         return in_array($needle, $storage, true);
 69:     }
 70: 
 71:     /**
 72:      * @param array $storage
 73:      * @param mixed $needle
 74:      *
 75:      * @return bool
 76:      */
 77:     public function in(array $storage, $needle)
 78:     {
 79:         return in_array($needle, $storage, false);
 80:     }
 81: 
 82:     /**
 83:      * @param array $storage
 84:      *
 85:      * @return array
 86:      */
 87:     public function shuffle(array &$storage)
 88:     {
 89:         return shuffle($storage);
 90:     }
 91: 
 92:     /**
 93:      * @param array  $storage
 94:      * @param string $path
 95:      * @param mixed  $value
 96:      */
 97:     public function set(array &$storage, $path, $value)
 98:     {
 99:         $keys = $this->keys($path);
100: 
101:         $rows = &$storage;
102:         foreach ($keys as $key)
103:         {
104:             if (!$this->keyExists($rows, $key))
105:             {
106:                 $rows[$key] = [];
107:             }
108: 
109:             $rows = &$rows[$key];
110:         }
111: 
112:         $rows = $value;
113:     }
114: 
115:     /**
116:      * @param array $storage
117:      * @param array $keys
118:      *
119:      * @return array|mixed
120:      * @throws ExceptionEmpty
121:      */
122:     protected function findPath(array $storage, array $keys)
123:     {
124:         if (empty($keys))
125:         {
126:             throw new ExceptionEmpty('Not found keys');
127:         }
128: 
129:         $rows = &$storage;
130: 
131:         foreach ($keys as $key)
132:         {
133:             if (!$this->keyExists($rows, $key))
134:             {
135:                 throw new ExceptionEmpty("Key {$key} not found");
136:             }
137: 
138:             $rows = &$rows[$key];
139:         }
140: 
141:         return $rows;
142:     }
143: 
144:     /**
145:      * @param array $storage
146:      * @param null  $key
147:      *
148:      * @return array|mixed
149:      * @throws ExceptionEmpty
150:      */
151:     public function getRequired(array $storage, $key = null)
152:     {
153:         return $this->findPath($storage, $this->keys($key));
154:     }
155: 
156: }
API documentation generated by ApiGen