This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
169 additions
and
4 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
composer.phar | ||
/vendor/ | ||
|
||
# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file | ||
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file | ||
# composer.lock | ||
composer.lock | ||
.idea |
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,20 @@ | ||
{ | ||
"name": "bavix/slice", | ||
"description": "Slice", | ||
"type": "library", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "REZ1DENT3", | ||
"email": "info@babichev.net" | ||
} | ||
], | ||
"require": { | ||
"bavix/iterator": "^1.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Bavix\\": "src/" | ||
} | ||
} | ||
} |
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,147 @@ | ||
<?php | ||
|
||
namespace Bavix\Slice; | ||
|
||
use Bavix\Helpers\Arr; | ||
use Bavix\Iterator\Iterator; | ||
use Bavix\Exceptions; | ||
|
||
class Slice extends Iterator | ||
{ | ||
|
||
/** | ||
* Slice constructor. | ||
* | ||
* @param array $data | ||
* @param array|Slice $parameters | ||
*/ | ||
public function __construct(array $data, $parameters = null) | ||
{ | ||
$this->setData($data); | ||
|
||
if (null !== $parameters) | ||
{ | ||
$this->walk($parameters); | ||
} | ||
} | ||
|
||
/** | ||
* @param Slice|array $slice | ||
*/ | ||
protected function walk($slice) | ||
{ | ||
if (is_array($slice)) | ||
{ | ||
$slice = $this->make($slice); | ||
} | ||
|
||
Arr::walkRecursive($this->data, function (&$value) use ($slice) | ||
{ | ||
if (!is_object($value) && $value{0} === '%' && $value{\strlen($value) - 1} === '%') | ||
{ | ||
$path = \substr($value, 1, -1); | ||
$value = $slice->getData($path); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* @return \Generator|Slice[] | ||
*/ | ||
public function asGenerator() | ||
{ | ||
foreach ($this->data as $key => $object) | ||
{ | ||
yield $key => $this->make($object); | ||
} | ||
} | ||
|
||
/** | ||
* @return Slice[] | ||
*/ | ||
public function asObject() | ||
{ | ||
return \iterator_to_array($this->asGenerator()); | ||
} | ||
|
||
/** | ||
* @param array $data | ||
* | ||
* @return static | ||
*/ | ||
public function setData(array $data) | ||
{ | ||
$this->data = $data; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param string $offset | ||
* @param mixed $default | ||
* | ||
* @return mixed | ||
*/ | ||
public function getData($offset, $default = null) | ||
{ | ||
return Arr::get($this->data, $offset, $default); | ||
} | ||
|
||
/** | ||
* @param array $data | ||
* | ||
* @return Slice | ||
*/ | ||
public function make(array $data) | ||
{ | ||
return (clone $this)->setData($data); | ||
} | ||
|
||
/** | ||
* @param string $offset | ||
* | ||
* @return array|mixed | ||
*/ | ||
public function getRequired($offset) | ||
{ | ||
return Arr::getRequired($this->data, $offset); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function offsetExists($offset) | ||
{ | ||
return Arr::get($this->data, $offset) !== null; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function offsetGet($offset) | ||
{ | ||
return $this->getRequired($offset); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function offsetSet($offset, $value) | ||
{ | ||
if (null === $offset) | ||
{ | ||
throw new Exceptions\Invalid('Slice does not support NULL'); | ||
} | ||
|
||
Arr::set($this->data, $offset, $value); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function offsetUnset($offset) | ||
{ | ||
Arr::remove($this->data, $offset); | ||
} | ||
|
||
} |