forked from maciejczyzewski/bottomline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhas.php
61 lines (57 loc) · 1.45 KB
/
has.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
namespace collections;
/**
* Return true if `$collection` contains the requested `$key`.
*
* In contrast to `isset()`, `__::has()` returns true if the key exists but is null.
*
* ## Arrays
*
* **Usage**
*
* ```php
* __::has(['foo' => ['bar' => 'num'], 'foz' => 'baz'], 'foo.bar');
* ```
*
* **Result**
*
* ```
* true
* ```
*
* ## Objects
*
* **Usage**
*
* ```php
* __::hasKeys((object) ['foo' => 'bar', 'foz' => 'baz'], 'bar');
* ```
*
* **Result**
*
* ```
* false
* ```
*
* @param array|object $collection Array or object to search a key for
* @param string|int $path Path to look for. Supports dot notation for
* traversing multiple levels.
*
* @return bool
*/
function has($collection, $path)
{
$portions = \__::split($path, \__::DOT_NOTATION_DELIMITER, 2);
$key = $portions[0];
if (\count($portions) === 1) {
// Calling array_key_exists on an ArrayAccess object will not call `offsetExists()`
// See: http://php.net/manual/en/class.arrayaccess.php#104061
if ($collection instanceof \ArrayAccess) {
return $collection->offsetExists($key);
}
// We use a cast to array to handle the numeric keys for objects (workaround).
// See: https://wiki.php.net/rfc/convert_numeric_keys_in_object_array_casts
return array_key_exists($key, (array) $collection);
}
return has(\__::get($collection, $key), $portions[1]);
}