adding $arrayOnly to only and except methods
This commit is contained in:
26
src/Ary.php
26
src/Ary.php
@ -160,5 +160,31 @@ class Ary extends Collection
|
|||||||
{
|
{
|
||||||
return new static(array_replace_recursive($this->items, $this->getArrayableItems($items)));
|
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
|
#endregion
|
||||||
}
|
}
|
||||||
@ -5,16 +5,13 @@
|
|||||||
|
|
||||||
use Salarmehr\Ary;
|
use Salarmehr\Ary;
|
||||||
|
|
||||||
if (!function_exists('ary') && phpversion()) {
|
if (!function_exists('ary') && defined('PHP_VERSION_ID') && PHP_VERSION_ID > 50600) {
|
||||||
if (!defined('ary') && PHP_VERSION_ID > 50600) {
|
|
||||||
/**
|
/**
|
||||||
* @param array $items
|
* @param array $items
|
||||||
* @return Ary
|
* @return Ary
|
||||||
*/
|
*/
|
||||||
function ary(...$items)
|
function ary(...$items)
|
||||||
{
|
{
|
||||||
return new Ary($items);
|
return new Ary(...$items);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@ -1,12 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
|
||||||
* Created by PhpStorm.
|
|
||||||
* User: samaneh
|
|
||||||
* Date: 2015/10/15
|
|
||||||
* Time: 11:12 AM
|
|
||||||
*/
|
|
||||||
|
|
||||||
use \Salarmehr\Ary;
|
use \Salarmehr\Ary;
|
||||||
|
|
||||||
require __DIR__ . '/../vendor/autoload.php';
|
require __DIR__ . '/../vendor/autoload.php';
|
||||||
@ -40,7 +33,7 @@ class Test extends PHPUnit\Framework\TestCase
|
|||||||
|
|
||||||
public function testAll($originalary, $expectedary)
|
public function testAll($originalary, $expectedary)
|
||||||
{
|
{
|
||||||
$ary = new Ary();
|
$ary = ary($originalary);
|
||||||
$ary->all();
|
$ary->all();
|
||||||
$this->assertEquals($expectedary, $originalary);
|
$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((object)['name' => 'ali', 'lastname' => 'reza', 'age' => 30], (object)['name' => 'ali', 'lastname' => 'reza', 'age' => 30]),
|
||||||
array(['ali', 'reza', 'mohammad'], ['ali', 'reza', 'mohammad']),
|
array(['ali', 'reza', 'mohammad'], ['ali', 'reza', 'mohammad']),
|
||||||
array([1, 2, 3, 4], [1, 2, 3, 4]),
|
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');
|
$this->assertEquals($ary->get('x.xx.m'), 'xxx');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testAryHelper()
|
||||||
|
{
|
||||||
|
$a=ary(['x'=>2,'y'=>22]);
|
||||||
|
$this->assertEquals($a['x'],2);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dataProvider Provider
|
* @dataProvider Provider
|
||||||
@ -108,7 +108,6 @@ class Test extends PHPUnit\Framework\TestCase
|
|||||||
public function testAssignment()
|
public function testAssignment()
|
||||||
{
|
{
|
||||||
$ary = new Ary();
|
$ary = new Ary();
|
||||||
// var_dump($ary);die();
|
|
||||||
$ary[] = 3;
|
$ary[] = 3;
|
||||||
$this->assertEquals($ary[0], 3);
|
$this->assertEquals($ary[0], 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());
|
$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()
|
// public function testOffsetExists()
|
||||||
// {
|
// {
|
||||||
// $parameters = array(7,8,9,4);
|
// $parameters = array(7,8,9,4);
|
||||||
Reference in New Issue
Block a user