Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
first version
Browse files Browse the repository at this point in the history
  • Loading branch information
rez1dent3 committed Apr 28, 2017
1 parent e36050b commit 5293954
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 4 deletions.
6 changes: 2 additions & 4 deletions .gitignore
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
20 changes: 20 additions & 0 deletions composer.json
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/"
}
}
}
147 changes: 147 additions & 0 deletions src/Slice/Slice.php
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);
}

}

0 comments on commit 5293954

Please sign in to comment.