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

Commit

Permalink
add raw
Browse files Browse the repository at this point in the history
  • Loading branch information
rez1dent3 committed Dec 20, 2017
1 parent 9af100a commit fa224c2
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 18 deletions.
31 changes: 31 additions & 0 deletions src/Slice/Raw.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Bavix\Slice;

class Raw
{

/**
* @var mixed
*/
protected $data;

/**
* Raw constructor.
*
* @param $data
*/
public function __construct($data)
{
$this->data = $data;
}

/**
* @return mixed
*/
public function getData()
{
return $this->data;
}

}
32 changes: 22 additions & 10 deletions src/Slice/Slice.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace Bavix\Slice;

use Bavix\Exceptions;
use Bavix\Helpers\Arr;
use Bavix\Helpers\Str;
use Bavix\Iterator\Iterator;
use Bavix\Exceptions;

class Slice extends Iterator
{
Expand Down Expand Up @@ -43,7 +44,7 @@ public static function from($data): self
}

/**
* @param int $depth
* @param int|bool $depth
*
* @return array
*/
Expand All @@ -60,7 +61,7 @@ public function asArray($depth = INF)
{
$results[$key] =
$data instanceof self ?
$data->asArray(is_bool($data) ? INF : --$depth) :
$data->asArray(\is_bool($depth) ? INF : --$depth) :
$data;
}

Expand All @@ -72,21 +73,32 @@ public function asArray($depth = INF)
*/
protected function walk($slice)
{
if (is_array($slice))
if (\is_array($slice))
{
$slice = $this->make($slice);
}

Arr::walkRecursive($this->data, function (&$value) use ($slice)
{
if (!empty($value) &&
\is_string($value) && $value{0} === '%' &&
$value{\strlen($value) - 1} === '%' &&
Arr::walkRecursive($this->data, function (&$value) use ($slice) {

if (\is_object($value) && $value instanceof Raw)
{
$value = $value->getData();
return;
}

if (empty($value) || !\is_string($value))
{
return;
}

if (Str::first($value) === '%' &&
Str::last($value) === '%' &&
\substr_count($value, '%') === 2)
{
$path = \substr($value, 1, -1);
$path = Str::sub($value, 1, -1);
$value = $slice->getRequired($path);
}

});
}

Expand Down
37 changes: 29 additions & 8 deletions tests/SliceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tests;

use Bavix\Slice\Raw;
use Bavix\Slice\Slice;
use Bavix\Tests\Unit;

Expand All @@ -17,6 +18,7 @@ class SliceTest extends Unit
* @var array
*/
protected $params = [
'raw' => 'raw data',
'name' => 'bavix/slice',
'license' => 'MIT',
'author' => [
Expand All @@ -28,14 +30,7 @@ class SliceTest extends Unit
/**
* @var array
*/
protected $data = [
'name' => '%name%',
'license' => '%license%',
'author' => [
'name' => '%author.name%',
'email' => '%author.email%',
],
];
protected $data;

protected $matrix = [
[1, 2, 3, 4],
Expand All @@ -46,6 +41,16 @@ class SliceTest extends Unit

public function setUp()
{
$this->data = [
'raw' => new Raw('%raw%'),
'name' => '%name%',
'license' => '%license%',
'author' => [
'name' => '%author.name%',
'email' => '%author.email%',
],
];

parent::setUp();
$this->slice = new Slice($this->data, $this->params);
}
Expand All @@ -62,6 +67,21 @@ public function testWithoutParams()

public function testAsArray()
{
$this->assertSame(
'%raw%',
$this->slice['raw']
);

$this->assertNotSame(
$this->params['raw'],
$this->slice['raw']
);

unset(
$this->params['raw'],
$this->slice['raw']
);

$this->assertArraySubset(
$this->params,
$this->slice->asArray()
Expand Down Expand Up @@ -121,6 +141,7 @@ public function testOffsetExists()
$this->assertTrue(isset($this->slice['name']));
$this->assertFalse(isset($this->slice[__FUNCTION__]));
}

public function testOffsetSet()
{
$this->slice['name'] = __FUNCTION__;
Expand Down

0 comments on commit fa224c2

Please sign in to comment.