Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
U
UserAdminV2
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
庄欣
UserAdminV2
Commits
4167a295
Commit
4167a295
authored
Sep 15, 2011
by
Taylor Otwell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added tests for arr::first.
parent
de2eabdf
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
27 additions
and
20 deletions
+27
-20
laravel/arr.php
+7
-20
tests/ArrTest.php
+20
-0
No files found.
laravel/arr.php
View file @
4167a295
<?php
namespace
Laravel
;
<?php
namespace
Laravel
;
use
Closure
;
class
Arr
{
class
Arr
{
/**
/**
...
@@ -9,14 +11,6 @@ class Arr {
...
@@ -9,14 +11,6 @@ class Arr {
* also be accessed using JavaScript "dot" style notation. Retrieving items nested
* also be accessed using JavaScript "dot" style notation. Retrieving items nested
* in multiple arrays is supported.
* in multiple arrays is supported.
*
*
* <code>
* // Get the "name" item from the array
* $name = Arr::get(array('name' => 'Fred'), 'name');
*
* // Get the "age" item from the array, or return 25 if it doesn't exist
* $name = Arr::get(array('name' => 'Fred'), 'age', 25);
* </code>
*
* @param array $array
* @param array $array
* @param string $key
* @param string $key
* @param mixed $default
* @param mixed $default
...
@@ -30,7 +24,7 @@ class Arr {
...
@@ -30,7 +24,7 @@ class Arr {
{
{
if
(
!
is_array
(
$array
)
or
!
array_key_exists
(
$segment
,
$array
))
if
(
!
is_array
(
$array
)
or
!
array_key_exists
(
$segment
,
$array
))
{
{
return
(
$default
instanceof
\
Closure
)
?
call_user_func
(
$default
)
:
$default
;
return
(
$default
instanceof
Closure
)
?
call_user_func
(
$default
)
:
$default
;
}
}
$array
=
$array
[
$segment
];
$array
=
$array
[
$segment
];
...
@@ -46,11 +40,6 @@ class Arr {
...
@@ -46,11 +40,6 @@ class Arr {
* a variable depth, such as configuration arrays. Like the Arr::get
* a variable depth, such as configuration arrays. Like the Arr::get
* method, JavaScript "dot" syntax is supported.
* method, JavaScript "dot" syntax is supported.
*
*
* <code>
* // Set an array's "name" item to "Fred"
* Arr::set($array, 'name', 'Fred');
* </code>
*
* @param array $array
* @param array $array
* @param string $key
* @param string $key
* @param mixed $value
* @param mixed $value
...
@@ -80,21 +69,18 @@ class Arr {
...
@@ -80,21 +69,18 @@ class Arr {
/**
/**
* Return the first element in an array which passes a given truth test.
* Return the first element in an array which passes a given truth test.
*
*
* <code>
* // Get the first element in an array that is less than 2
* $value = Arr::first(array(4, 3, 2, 1), function($key, $value) {return $value < 2;});
* </code>
*
* @param array $array
* @param array $array
* @param Closure $callback
* @param Closure $callback
* @return mixed
* @return mixed
*/
*/
public
static
function
first
(
$array
,
$callback
)
public
static
function
first
(
$array
,
$callback
,
$default
=
null
)
{
{
foreach
(
$array
as
$key
=>
$value
)
foreach
(
$array
as
$key
=>
$value
)
{
{
if
(
call_user_func
(
$callback
,
$key
,
$value
))
return
$value
;
if
(
call_user_func
(
$callback
,
$key
,
$value
))
return
$value
;
}
}
return
(
$default
instanceof
Closure
)
?
call_user_func
(
$default
)
:
$default
;
}
}
}
}
\ No newline at end of file
tests/ArrTest.php
View file @
4167a295
...
@@ -36,6 +36,26 @@ class ArrTest extends PHPUnit_Framework_TestCase {
...
@@ -36,6 +36,26 @@ class ArrTest extends PHPUnit_Framework_TestCase {
}
}
/**
* @dataProvider getArray
*/
public
function
testFirstMethodReturnsFirstItemPassingTruthTest
(
$array
)
{
$array
[
'email2'
]
=
'taylor@hotmail.com'
;
$this
->
assertEquals
(
'taylorotwell@gmail.com'
,
Arr
::
first
(
$array
,
function
(
$k
,
$v
)
{
return
substr
(
$v
,
0
,
3
)
==
'tay'
;}));
}
/**
* @dataProvider getArray
*/
public
function
testFirstMethodReturnsDefaultWhenNoItemExists
(
$array
)
{
$this
->
assertNull
(
Arr
::
first
(
$array
,
function
(
$k
,
$v
)
{
return
$v
===
'something'
;}));
$this
->
assertEquals
(
'default'
,
Arr
::
first
(
$array
,
function
(
$k
,
$v
)
{
return
$v
===
'something'
;},
'default'
));
$this
->
assertEquals
(
'default'
,
Arr
::
first
(
$array
,
function
(
$k
,
$v
)
{
return
$v
===
'something'
;},
function
()
{
return
'default'
;}));
}
public
function
getArray
()
public
function
getArray
()
{
{
return
array
(
array
(
return
array
(
array
(
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment