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)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,12 +1,5 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: samaneh
|
||||
* Date: 2015/10/15
|
||||
* Time: 11:12 AM
|
||||
*/
|
||||
|
||||
use \Salarmehr\Ary;
|
||||
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
@ -40,7 +33,7 @@ class Test extends PHPUnit\Framework\TestCase
|
||||
|
||||
public function testAll($originalary, $expectedary)
|
||||
{
|
||||
$ary = new Ary();
|
||||
$ary = ary($originalary);
|
||||
$ary->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);
|
||||
Reference in New Issue
Block a user