Skip to content

Commit ec305d2

Browse files
committed
Adding has() method for key checking
Updating documentation Using PHP5.3 compatible array notation Disabling tls on Travis to avoid 5.3.3 SSL issue Disabling TLS on composer (travis) Fixing typo Trying to avoid OpenSSL error PHP 5.3.3 crashes
1 parent 7a0960d commit ec305d2

File tree

6 files changed

+62
-1
lines changed

6 files changed

+62
-1
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
language: php
22

33
php:
4-
- 5.3.3
54
- 5.3
65
- 5.4
76
- 5.5

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ $data->get('a.b.d');
3333

3434
// array('E0', 'E1', 'E2')
3535
$data->get('a.b.e');
36+
37+
// true
38+
$data->has('a.b.c');
39+
40+
// false
41+
$data->has('a.b.d.j');
3642
```
3743

3844
A more concrete example:
@@ -92,6 +98,9 @@ $data->set('hosts.april', array(
9298
'password' => '@---S',
9399
'roles' => array('web'),
94100
));
101+
102+
// Check if a key exists (true to this case)
103+
$hasKey = $data->has('hosts.dewey.username');
95104
```
96105

97106

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
"name": "Beau Simensen",
1616
"email": "[email protected]",
1717
"homepage": "http://beausimensen.com"
18+
},
19+
{
20+
"name": "Carlos Frutos",
21+
"email": "[email protected]",
22+
"homepage": "https://github.com/cfrutos"
1823
}
1924
],
2025
"require": {

src/Dflydev/DotAccessData/Data.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,28 @@ public function get($key, $default = null)
159159
return $currentValue === null ? $default : $currentValue;
160160
}
161161

162+
/**
163+
* {@inheritdoc}
164+
*/
165+
public function has($key)
166+
{
167+
$currentValue = &$this->data;
168+
$keyPath = explode('.', $key);
169+
170+
for ( $i = 0; $i < count($keyPath); $i++ ) {
171+
$currentKey = $keyPath[$i];
172+
if (
173+
!is_array($currentValue) ||
174+
!isset($currentValue[$currentKey])
175+
) {
176+
return false;
177+
}
178+
$currentValue = &$currentValue[$currentKey];
179+
}
180+
181+
return true;
182+
}
183+
162184
/**
163185
* {@inheritdoc}
164186
*/

src/Dflydev/DotAccessData/DataInterface.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ public function remove($key);
4646
*/
4747
public function get($key, $default = null);
4848

49+
/**
50+
* Check if the key exists
51+
*
52+
* @param string $key
53+
*
54+
* @return bool
55+
*/
56+
public function has($key);
57+
4958
/**
5059
* Get a data instance for a key
5160
*

tests/Dflydev/DotAccessData/DataTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,23 @@ public function testGet()
146146
$this->runSampleDataTests($data);
147147
}
148148

149+
public function testHas()
150+
{
151+
$data = new Data($this->getSampleData());
152+
153+
foreach (
154+
array('a', 'i', 'b.d', 'f.g.h', 'h.i', 'b.d.d1') as $existentKey
155+
) {
156+
$this->assertTrue($data->has($existentKey));
157+
}
158+
159+
foreach (
160+
array('p', 'b.b1', 'b.c.C1', 'h.i.I', 'b.d.d1.D1') as $notExistentKey
161+
) {
162+
$this->assertFalse($data->has($notExistentKey));
163+
}
164+
}
165+
149166
public function testGetData()
150167
{
151168
$wrappedData = new Data(array(

0 commit comments

Comments
 (0)