From 29b146dd507e7885df7e8a0efcd58775005effc2 Mon Sep 17 00:00:00 2001 From: Reza Salarmehr Date: Sat, 23 Sep 2017 23:34:55 +1000 Subject: [PATCH] adding $arrayOnly to only and except methods --- src/Ary.php | 26 ++++++++++++++++++++++++++ src/helper.php | 9 +++------ {test => tests}/Test.php | 31 ++++++++++++++++++++++--------- 3 files changed, 51 insertions(+), 15 deletions(-) rename {test => tests}/Test.php (87%) diff --git a/src/Ary.php b/src/Ary.php index d9547b7..ce62a3a 100644 --- a/src/Ary.php +++ b/src/Ary.php @@ -160,5 +160,31 @@ class Ary extends Collection { return new static(array_replace_recursive($this->items, $this->getArrayableItems($items))); } + + /** + * Get the items with the specified keys. + * + * @param mixed $keys + * @param bool $returnArray + * @return static + */ + public function only($keys, $returnArray = false) + { + $result = parent::only($keys); + return $returnArray ? $result->toArray() : $result; + } + + /** + * Get all items except for those with the specified keys. + * + * @param mixed $keys + * @return static + */ + public function except($keys, $returnArray = false) + { + $result = parent::except($keys); + return $returnArray ? $result->toArray() : $result; + } + #endregion } \ No newline at end of file diff --git a/src/helper.php b/src/helper.php index 0d350cf..dd62e29 100644 --- a/src/helper.php +++ b/src/helper.php @@ -5,16 +5,13 @@ use Salarmehr\Ary; -if (!function_exists('ary') && phpversion()) { - if (!defined('ary') && PHP_VERSION_ID > 50600) { +if (!function_exists('ary') && defined('PHP_VERSION_ID') && PHP_VERSION_ID > 50600) { /** * @param array $items * @return Ary */ function ary(...$items) { - return new Ary($items); + return new Ary(...$items); } - } -} - +} \ No newline at end of file diff --git a/test/Test.php b/tests/Test.php similarity index 87% rename from test/Test.php rename to tests/Test.php index 899ac0d..22cf6fd 100644 --- a/test/Test.php +++ b/tests/Test.php @@ -1,12 +1,5 @@ all(); $this->assertEquals($expectedary, $originalary); } @@ -55,6 +48,7 @@ class Test extends PHPUnit\Framework\TestCase array((object)['name' => 'ali', 'lastname' => 'reza', 'age' => 30], (object)['name' => 'ali', 'lastname' => 'reza', 'age' => 30]), array(['ali', 'reza', 'mohammad'], ['ali', 'reza', 'mohammad']), array([1, 2, 3, 4], [1, 2, 3, 4]), + array(['x'=>'y'], ['x'=>'y']), ); } @@ -68,6 +62,12 @@ class Test extends PHPUnit\Framework\TestCase $this->assertEquals($ary->get('x.xx.m'), 'xxx'); } + public function testAryHelper() + { + $a=ary(['x'=>2,'y'=>22]); + $this->assertEquals($a['x'],2); + } + /** * @dataProvider Provider @@ -108,7 +108,6 @@ class Test extends PHPUnit\Framework\TestCase public function testAssignment() { $ary = new Ary(); -// var_dump($ary);die(); $ary[] = 3; $this->assertEquals($ary[0], 3); $this->assertEquals($ary->{0}, 3); @@ -159,6 +158,20 @@ class Test extends PHPUnit\Framework\TestCase $this->assertEquals($expect, $c->replaceRecursively(new Ary($replacements))->all()); } + public function testOnly() + { + $c = new Ary(['foo' => 'x', 'bar' => 'y']); + $this->assertEquals(['bar' => 'y'], $c->only(['bar'], true)); + $this->assertEquals(['bar' => 'y'], $c->only(['bar'])->all()); + } + + public function testExcept() + { + $c = new Ary(['foo' => 'x', 'bar' => 'y']); + $this->assertEquals(['foo' => 'x'], $c->except(['bar'], true)); + $this->assertEquals(['foo' => 'x'], $c->except(['bar'])->all()); + } + // public function testOffsetExists() // { // $parameters = array(7,8,9,4);