@@ -21,19 +21,19 @@ |
||
| 21 | 21 | * |
| 22 | 22 | * @link http://php.net/manual/class.seekableiterator.php |
| 23 | 23 | */ |
| 24 | -trait SeekableIteratorTrait{ |
|
| 24 | +trait SeekableIteratorTrait { |
|
| 25 | 25 | use IteratorTrait; |
| 26 | 26 | |
| 27 | 27 | /** |
| 28 | 28 | * @link http://php.net/manual/seekableiterator.seek.php |
| 29 | 29 | * @inheritdoc |
| 30 | 30 | */ |
| 31 | - public function seek($pos){ |
|
| 31 | + public function seek($pos) { |
|
| 32 | 32 | $this->rewind(); |
| 33 | 33 | |
| 34 | - for( ; $this->offset < $pos; ){ |
|
| 34 | + for (; $this->offset < $pos;) { |
|
| 35 | 35 | |
| 36 | - if(!next($this->array)) { |
|
| 36 | + if (!next($this->array)) { |
|
| 37 | 37 | throw new OutOfBoundsException('invalid seek position: '.$pos); |
| 38 | 38 | } |
| 39 | 39 | |
@@ -17,16 +17,16 @@ discard block |
||
| 17 | 17 | /** |
| 18 | 18 | * a generic container with magic getter and setter |
| 19 | 19 | */ |
| 20 | -trait Container{ |
|
| 20 | +trait Container { |
|
| 21 | 21 | |
| 22 | 22 | /** |
| 23 | 23 | * @param array $properties |
| 24 | 24 | */ |
| 25 | - public function __construct(array $properties = null){ |
|
| 25 | + public function __construct(array $properties = null) { |
|
| 26 | 26 | |
| 27 | - if(!empty($properties)){ |
|
| 27 | + if (!empty($properties)) { |
|
| 28 | 28 | |
| 29 | - foreach($properties as $key => $value){ |
|
| 29 | + foreach ($properties as $key => $value) { |
|
| 30 | 30 | $this->__set($key, $value); |
| 31 | 31 | } |
| 32 | 32 | |
@@ -39,9 +39,9 @@ discard block |
||
| 39 | 39 | * |
| 40 | 40 | * @return mixed |
| 41 | 41 | */ |
| 42 | - public function __get(string $property){ |
|
| 42 | + public function __get(string $property) { |
|
| 43 | 43 | |
| 44 | - if(property_exists($this, $property) && !(new ReflectionProperty($this, $property))->isPrivate()){ |
|
| 44 | + if (property_exists($this, $property) && !(new ReflectionProperty($this, $property))->isPrivate()) { |
|
| 45 | 45 | return $this->{$property}; |
| 46 | 46 | } |
| 47 | 47 | |
@@ -54,9 +54,9 @@ discard block |
||
| 54 | 54 | * |
| 55 | 55 | * @return void |
| 56 | 56 | */ |
| 57 | - public function __set(string $property, $value){ |
|
| 57 | + public function __set(string $property, $value) { |
|
| 58 | 58 | |
| 59 | - if(property_exists($this, $property) && !(new ReflectionProperty($this, $property))->isPrivate()){ |
|
| 59 | + if (property_exists($this, $property) && !(new ReflectionProperty($this, $property))->isPrivate()) { |
|
| 60 | 60 | $this->{$property} = $value; |
| 61 | 61 | } |
| 62 | 62 | |
@@ -68,7 +68,7 @@ discard block |
||
| 68 | 68 | public function __toArray():array { |
| 69 | 69 | $data = []; |
| 70 | 70 | |
| 71 | - foreach($this as $key => $value){ |
|
| 71 | + foreach ($this as $key => $value) { |
|
| 72 | 72 | $data[$key] = $value; |
| 73 | 73 | } |
| 74 | 74 | |
@@ -19,7 +19,7 @@ discard block |
||
| 19 | 19 | |
| 20 | 20 | /** |
| 21 | 21 | */ |
| 22 | -class ByteArrayDispenser{ |
|
| 22 | +class ByteArrayDispenser { |
|
| 23 | 23 | |
| 24 | 24 | /** |
| 25 | 25 | * @var string |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | */ |
| 44 | 44 | public function fromIntSize(int $size):ByteArray{ |
| 45 | 45 | |
| 46 | - if(!$this->isAllowedInt($size)){ |
|
| 46 | + if (!$this->isAllowedInt($size)) { |
|
| 47 | 47 | throw new TraitException('invalid size'); |
| 48 | 48 | } |
| 49 | 49 | |
@@ -59,18 +59,18 @@ discard block |
||
| 59 | 59 | */ |
| 60 | 60 | public function fromArray($array, $save_indexes = null):ByteArray{ // @todo \Traversable, $target, allow variable arrays, iterators etc |
| 61 | 61 | |
| 62 | - try{ |
|
| 62 | + try { |
|
| 63 | 63 | $out = $this->fromIntSize(count($array)); |
| 64 | 64 | |
| 65 | 65 | $array = ($save_indexes !== null ? $save_indexes : true) ? $array : array_values($array); |
| 66 | 66 | |
| 67 | - foreach($array as $k => $v){ |
|
| 67 | + foreach ($array as $k => $v) { |
|
| 68 | 68 | $out[$k] = $v; |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | return $out; |
| 72 | 72 | } |
| 73 | - catch(Exception $e){ |
|
| 73 | + catch (Exception $e) { |
|
| 74 | 74 | throw new TraitException($e->getMessage()); |
| 75 | 75 | } |
| 76 | 76 | |
@@ -85,7 +85,7 @@ discard block |
||
| 85 | 85 | */ |
| 86 | 86 | public function fromRange(int $start, int $len):ByteArray{ |
| 87 | 87 | |
| 88 | - if(!$this->isAllowedInt($start) || $start < 0){ |
|
| 88 | + if (!$this->isAllowedInt($start) || $start < 0) { |
|
| 89 | 89 | throw new TraitException('invalid start'); |
| 90 | 90 | } |
| 91 | 91 | |
@@ -121,7 +121,7 @@ discard block |
||
| 121 | 121 | public function fromHex(string $hex):ByteArray{ |
| 122 | 122 | $hex = preg_replace('/[\s\r\n\t ]/', '', $hex); |
| 123 | 123 | |
| 124 | - if(!$this->isAllowedHex($hex)){ |
|
| 124 | + if (!$this->isAllowedHex($hex)) { |
|
| 125 | 125 | throw new TraitException('invalid hex string'); |
| 126 | 126 | } |
| 127 | 127 | |
@@ -148,7 +148,7 @@ discard block |
||
| 148 | 148 | public function fromJSON(string $json):ByteArray{ |
| 149 | 149 | $json = trim($json); |
| 150 | 150 | |
| 151 | - if(!$this->isAllowedJSON($json)){ |
|
| 151 | + if (!$this->isAllowedJSON($json)) { |
|
| 152 | 152 | throw new TraitException('invalid JSON array'); |
| 153 | 153 | } |
| 154 | 154 | |
@@ -175,7 +175,7 @@ discard block |
||
| 175 | 175 | public function fromBase64(string $base64):ByteArray{ |
| 176 | 176 | $base64 = trim($base64); |
| 177 | 177 | |
| 178 | - if(!$this->isAllowedBase64($base64)){ |
|
| 178 | + if (!$this->isAllowedBase64($base64)) { |
|
| 179 | 179 | throw new TraitException('invalid base64 string'); |
| 180 | 180 | } |
| 181 | 181 | |
@@ -210,19 +210,19 @@ discard block |
||
| 210 | 210 | */ |
| 211 | 211 | public function guessFrom($data):ByteArray{ |
| 212 | 212 | |
| 213 | - if($data instanceof Traversable){ |
|
| 213 | + if ($data instanceof Traversable) { |
|
| 214 | 214 | return $this->fromArray(iterator_to_array($data)); |
| 215 | 215 | } |
| 216 | 216 | |
| 217 | - if(is_array($data)){ |
|
| 217 | + if (is_array($data)) { |
|
| 218 | 218 | return $this->fromArray($data); |
| 219 | 219 | } |
| 220 | 220 | |
| 221 | - if(is_string($data)){ |
|
| 221 | + if (is_string($data)) { |
|
| 222 | 222 | |
| 223 | - foreach(['Hex', 'Bin', 'JSON', 'Base64'] as $type){ |
|
| 223 | + foreach (['Hex', 'Bin', 'JSON', 'Base64'] as $type) { |
|
| 224 | 224 | |
| 225 | - if($this->{'isAllowed'.$type}($data)){ |
|
| 225 | + if ($this->{'isAllowed'.$type}($data)) { |
|
| 226 | 226 | return $this->{'from'.$type}($data); |
| 227 | 227 | } |
| 228 | 228 | |
@@ -69,8 +69,7 @@ |
||
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | return $out; |
| 72 | - } |
|
| 73 | - catch(Exception $e){ |
|
| 72 | + } catch(Exception $e){ |
|
| 74 | 73 | throw new TraitException($e->getMessage()); |
| 75 | 74 | } |
| 76 | 75 | |
@@ -17,7 +17,7 @@ discard block |
||
| 17 | 17 | /** |
| 18 | 18 | * @extends \SplFixedArray |
| 19 | 19 | */ |
| 20 | -class ByteArray extends SplFixedArray{ |
|
| 20 | +class ByteArray extends SplFixedArray { |
|
| 21 | 21 | |
| 22 | 22 | /** |
| 23 | 23 | * @return string |
@@ -30,7 +30,7 @@ discard block |
||
| 30 | 30 | * @return string |
| 31 | 31 | */ |
| 32 | 32 | public function toHex():string{ |
| 33 | - return $this->map(function($v){ |
|
| 33 | + return $this->map(function($v) { |
|
| 34 | 34 | return str_pad(dechex($v), '2', '0', STR_PAD_LEFT); |
| 35 | 35 | }); |
| 36 | 36 | } |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | * @return string |
| 54 | 54 | */ |
| 55 | 55 | public function toBin():string{ |
| 56 | - return $this->map(function($v){ |
|
| 56 | + return $this->map(function($v) { |
|
| 57 | 57 | return str_pad(decbin($v), '8', '0', STR_PAD_LEFT); |
| 58 | 58 | }); |
| 59 | 59 | } |
@@ -82,11 +82,11 @@ discard block |
||
| 82 | 82 | |
| 83 | 83 | $diff = $offset + $length; |
| 84 | 84 | |
| 85 | - if($diff > $this->count()){ |
|
| 85 | + if ($diff > $this->count()) { |
|
| 86 | 86 | $this->setSize($diff); |
| 87 | 87 | } |
| 88 | 88 | |
| 89 | - for($i = 0; $i < $length; $i++){ |
|
| 89 | + for ($i = 0; $i < $length; $i++) { |
|
| 90 | 90 | $this[$i + $offset] = $src[$i + $srcOffset]; |
| 91 | 91 | } |
| 92 | 92 | |
@@ -101,9 +101,9 @@ discard block |
||
| 101 | 101 | */ |
| 102 | 102 | public function slice(int $offset, int $length = null):ByteArray{ |
| 103 | 103 | /** @var \chillerlan\Traits\ArrayHelpers\ByteArray $slice */ |
| 104 | - $slice = (new ReflectionClass($this))->newInstanceArgs([$length ?? $this->count() - $offset]); |
|
| 104 | + $slice = (new ReflectionClass($this))->newInstanceArgs([$length ?? $this->count() - $offset]); |
|
| 105 | 105 | |
| 106 | - foreach($slice as $i => $_){ |
|
| 106 | + foreach ($slice as $i => $_) { |
|
| 107 | 107 | $slice[$i] = $this[$offset + $i]; |
| 108 | 108 | } |
| 109 | 109 | |
@@ -117,13 +117,13 @@ discard block |
||
| 117 | 117 | */ |
| 118 | 118 | public function equal(SplFixedArray $array):bool{ |
| 119 | 119 | |
| 120 | - if($this->count() !== $array->count()){ |
|
| 120 | + if ($this->count() !== $array->count()) { |
|
| 121 | 121 | return false; // @todo: throw Exception? |
| 122 | 122 | } |
| 123 | 123 | |
| 124 | 124 | $diff = 0; |
| 125 | 125 | |
| 126 | - foreach($this as $k => $v){ |
|
| 126 | + foreach ($this as $k => $v) { |
|
| 127 | 127 | $diff |= $v ^ $array[$k]; |
| 128 | 128 | } |
| 129 | 129 | |
@@ -14,7 +14,7 @@ discard block |
||
| 14 | 14 | |
| 15 | 15 | use ArrayIterator, ArrayObject, RecursiveArrayIterator, RecursiveIteratorIterator, Traversable; |
| 16 | 16 | |
| 17 | -class ArraySearch{ |
|
| 17 | +class ArraySearch { |
|
| 18 | 18 | |
| 19 | 19 | /** |
| 20 | 20 | * @var \IteratorIterator|\RecursiveIteratorIterator |
@@ -31,21 +31,21 @@ discard block |
||
| 31 | 31 | * |
| 32 | 32 | * @param array|object|\Traversable|\ArrayIterator|\ArrayObject|null $array |
| 33 | 33 | */ |
| 34 | - public function __construct($array = null){ |
|
| 34 | + public function __construct($array = null) { |
|
| 35 | 35 | |
| 36 | - if(($array instanceof ArrayObject) || ($array instanceof ArrayIterator)){ |
|
| 36 | + if (($array instanceof ArrayObject) || ($array instanceof ArrayIterator)) { |
|
| 37 | 37 | $this->array = $array->getArrayCopy(); |
| 38 | 38 | } |
| 39 | - elseif($array instanceof Traversable){ |
|
| 39 | + elseif ($array instanceof Traversable) { |
|
| 40 | 40 | $this->array = iterator_to_array($array); |
| 41 | 41 | } |
| 42 | - elseif(gettype($array) === 'object'){ |
|
| 42 | + elseif (gettype($array) === 'object') { |
|
| 43 | 43 | $this->array = get_object_vars($array); |
| 44 | 44 | } |
| 45 | - elseif(is_array($array)){ |
|
| 45 | + elseif (is_array($array)) { |
|
| 46 | 46 | $this->array = $array; |
| 47 | 47 | } |
| 48 | - else{ |
|
| 48 | + else { |
|
| 49 | 49 | $this->array = []; |
| 50 | 50 | } |
| 51 | 51 | |
@@ -56,12 +56,12 @@ discard block |
||
| 56 | 56 | * |
| 57 | 57 | * @return mixed |
| 58 | 58 | */ |
| 59 | - public function arraySearch(string $dotKey){ |
|
| 59 | + public function arraySearch(string $dotKey) { |
|
| 60 | 60 | $this->iterator = $this->getRecursiveIteratorIterator(); |
| 61 | 61 | |
| 62 | - foreach($this->iterator as $v){ |
|
| 62 | + foreach ($this->iterator as $v) { |
|
| 63 | 63 | |
| 64 | - if($this->getPath() === $dotKey){ |
|
| 64 | + if ($this->getPath() === $dotKey) { |
|
| 65 | 65 | return $v; |
| 66 | 66 | } |
| 67 | 67 | |
@@ -78,9 +78,9 @@ discard block |
||
| 78 | 78 | public function arrayIsset(string $dotKey):bool{ |
| 79 | 79 | $this->iterator = $this->getRecursiveIteratorIterator(); |
| 80 | 80 | |
| 81 | - foreach($this->iterator as $v){ |
|
| 81 | + foreach ($this->iterator as $v) { |
|
| 82 | 82 | |
| 83 | - if($this->getPath() === $dotKey){ |
|
| 83 | + if ($this->getPath() === $dotKey) { |
|
| 84 | 84 | return true; |
| 85 | 85 | } |
| 86 | 86 | |
@@ -35,17 +35,13 @@ |
||
| 35 | 35 | |
| 36 | 36 | if(($array instanceof ArrayObject) || ($array instanceof ArrayIterator)){ |
| 37 | 37 | $this->array = $array->getArrayCopy(); |
| 38 | - } |
|
| 39 | - elseif($array instanceof Traversable){ |
|
| 38 | + } elseif($array instanceof Traversable){ |
|
| 40 | 39 | $this->array = iterator_to_array($array); |
| 41 | - } |
|
| 42 | - elseif(gettype($array) === 'object'){ |
|
| 40 | + } elseif(gettype($array) === 'object'){ |
|
| 43 | 41 | $this->array = get_object_vars($array); |
| 44 | - } |
|
| 45 | - elseif(is_array($array)){ |
|
| 42 | + } elseif(is_array($array)){ |
|
| 46 | 43 | $this->array = $array; |
| 47 | - } |
|
| 48 | - else{ |
|
| 44 | + } else{ |
|
| 49 | 45 | $this->array = []; |
| 50 | 46 | } |
| 51 | 47 | |
@@ -15,7 +15,7 @@ discard block |
||
| 15 | 15 | /** |
| 16 | 16 | * @link https://github.com/laravel/framework/blob/5.4/src/Illuminate/Support/Arr.php |
| 17 | 17 | */ |
| 18 | -trait DotArray{ |
|
| 18 | +trait DotArray { |
|
| 19 | 19 | |
| 20 | 20 | /** |
| 21 | 21 | * Checks if $key isset in $array using dot notation and returns it on success. |
@@ -26,15 +26,15 @@ discard block |
||
| 26 | 26 | * |
| 27 | 27 | * @return mixed returns $array[$key], $default otherwise. |
| 28 | 28 | */ |
| 29 | - protected function arrayGet(array $array, string $key, $default = null){ |
|
| 29 | + protected function arrayGet(array $array, string $key, $default = null) { |
|
| 30 | 30 | |
| 31 | - if(isset($array[$key])){ |
|
| 31 | + if (isset($array[$key])) { |
|
| 32 | 32 | return $array[$key]; |
| 33 | 33 | } |
| 34 | 34 | |
| 35 | - foreach(explode('.', $key) as $segment){ |
|
| 35 | + foreach (explode('.', $key) as $segment) { |
|
| 36 | 36 | |
| 37 | - if(!is_array($array) || !array_key_exists($segment, $array)){ |
|
| 37 | + if (!is_array($array) || !array_key_exists($segment, $array)) { |
|
| 38 | 38 | return $default; |
| 39 | 39 | } |
| 40 | 40 | |
@@ -54,17 +54,17 @@ discard block |
||
| 54 | 54 | */ |
| 55 | 55 | protected function arrayIn(array $array, string $key):bool{ |
| 56 | 56 | |
| 57 | - if(empty($array)){ |
|
| 57 | + if (empty($array)) { |
|
| 58 | 58 | return false; |
| 59 | 59 | } |
| 60 | 60 | |
| 61 | - if(array_key_exists($key, $array)){ |
|
| 61 | + if (array_key_exists($key, $array)) { |
|
| 62 | 62 | return true; |
| 63 | 63 | } |
| 64 | 64 | |
| 65 | - foreach(explode('.', $key) as $segment){ |
|
| 65 | + foreach (explode('.', $key) as $segment) { |
|
| 66 | 66 | |
| 67 | - if(!is_array($array) || !array_key_exists($segment, $array)){ |
|
| 67 | + if (!is_array($array) || !array_key_exists($segment, $array)) { |
|
| 68 | 68 | return false; |
| 69 | 69 | } |
| 70 | 70 | |
@@ -85,21 +85,21 @@ discard block |
||
| 85 | 85 | * |
| 86 | 86 | * @return array |
| 87 | 87 | */ |
| 88 | - protected function arraySet(array &$array, string $key, $value){ |
|
| 88 | + protected function arraySet(array &$array, string $key, $value) { |
|
| 89 | 89 | |
| 90 | - if(is_null($key)){ |
|
| 90 | + if (is_null($key)) { |
|
| 91 | 91 | return $array = $value; |
| 92 | 92 | } |
| 93 | 93 | |
| 94 | 94 | $keys = explode('.', $key); |
| 95 | 95 | |
| 96 | - while(count($keys) > 1){ |
|
| 96 | + while (count($keys) > 1) { |
|
| 97 | 97 | $key = array_shift($keys); |
| 98 | 98 | |
| 99 | 99 | // If the key doesn't exist at this depth, we will just create an empty array |
| 100 | 100 | // to hold the next value, allowing us to create the arrays to hold final |
| 101 | 101 | // values at the correct depth. Then we'll keep digging into the array. |
| 102 | - if(!isset($array[$key]) || !is_array($array[$key])){ |
|
| 102 | + if (!isset($array[$key]) || !is_array($array[$key])) { |
|
| 103 | 103 | $array[$key] = []; |
| 104 | 104 | } |
| 105 | 105 | |