From fa224c2b000f75080d0cf377a5669f7f562b7374 Mon Sep 17 00:00:00 2001 From: Babichev Maxim Date: Wed, 20 Dec 2017 18:37:14 +0300 Subject: [PATCH] add raw --- src/Slice/Raw.php | 31 +++++++++++++++++++++++++++++++ src/Slice/Slice.php | 32 ++++++++++++++++++++++---------- tests/SliceTest.php | 37 +++++++++++++++++++++++++++++-------- 3 files changed, 82 insertions(+), 18 deletions(-) create mode 100644 src/Slice/Raw.php diff --git a/src/Slice/Raw.php b/src/Slice/Raw.php new file mode 100644 index 0000000..336593c --- /dev/null +++ b/src/Slice/Raw.php @@ -0,0 +1,31 @@ +data = $data; + } + + /** + * @return mixed + */ + public function getData() + { + return $this->data; + } + +} diff --git a/src/Slice/Slice.php b/src/Slice/Slice.php index 56e559f..e8d5878 100644 --- a/src/Slice/Slice.php +++ b/src/Slice/Slice.php @@ -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 { @@ -43,7 +44,7 @@ public static function from($data): self } /** - * @param int $depth + * @param int|bool $depth * * @return array */ @@ -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; } @@ -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); } + }); } diff --git a/tests/SliceTest.php b/tests/SliceTest.php index 916c14c..dc23f15 100644 --- a/tests/SliceTest.php +++ b/tests/SliceTest.php @@ -2,6 +2,7 @@ namespace Tests; +use Bavix\Slice\Raw; use Bavix\Slice\Slice; use Bavix\Tests\Unit; @@ -17,6 +18,7 @@ class SliceTest extends Unit * @var array */ protected $params = [ + 'raw' => 'raw data', 'name' => 'bavix/slice', 'license' => 'MIT', 'author' => [ @@ -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], @@ -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); } @@ -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() @@ -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__;