Skip to content

Commit

Permalink
Added has method. Resolves #26 (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
dschoenbauer committed Dec 2, 2016
1 parent 715aca9 commit 0697b9a
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 8 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,13 @@ $user = ArrayDotNotation::with($mongoConnection)->get('mongo,default,user');
$user = 'username';
*/
````
````

### HAS
````
// Validates that the dot notation path is present in the data.
$isPresent = $config->has('mongo,default,user');
/*
$isPresent = true;
*/
````
45 changes: 42 additions & 3 deletions docs/class-DSchoenbauer.DotNotation.ArrayDotNotation.html
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ <h1>Class ArrayDotNotation</h1>
David Schoenbauer<br>
<b>Version:</b>
1.1.1<br>
<b>Located at</b> <a href="source-class-DSchoenbauer.DotNotation.ArrayDotNotation.html#34-253" title="Go to source code">DotNotation/ArrayDotNotation.php</a>
<b>Located at</b> <a href="source-class-DSchoenbauer.DotNotation.ArrayDotNotation.html#34-271" title="Go to source code">DotNotation/ArrayDotNotation.php</a>
<br>
</div>

Expand Down Expand Up @@ -646,11 +646,11 @@ <h4>Since</h4>
<code><a href="source-class-DSchoenbauer.DotNotation.ArrayDotNotation.html#225-232" title="Go to source code">getKeys</a>( <span>type <var>$notation</var></span> )</code>

<div class="description short">
<p>consistantly parses notation keys</p>
<p>consistently parses notation keys</p>
</div>

<div class="description detailed hidden">
<p>consistantly parses notation keys</p>
<p>consistently parses notation keys</p>


<h4>Parameters</h4>
Expand Down Expand Up @@ -742,6 +742,45 @@ <h4>Returns</h4>



</div>
</div></td>
</tr>
<tr data-order="has" id="_has">

<td class="attributes"><code>
public

boolean

</code>
</td>

<td class="name"><div>
<a class="anchor" href="#_has">#</a>
<code><a href="source-class-DSchoenbauer.DotNotation.ArrayDotNotation.html#253-269" title="Go to source code">has</a>( <span>string <var>$dotNotation</var></span> )</code>

<div class="description short">
<p>Checks to see if a dot notation path is present in the data set.</p>
</div>

<div class="description detailed hidden">
<p>Checks to see if a dot notation path is present in the data set.</p>


<h4>Parameters</h4>
<div class="list"><dl>
<dt><var>$dotNotation</var></dt>
<dd>dot notation representation of keys of where to remove a value</dd>
</dl></div>

<h4>Returns</h4>
<div class="list">
boolean<br>returns true if the do notation path exists in the data
</div>




</div>
</div></td>
</tr>
Expand Down
24 changes: 21 additions & 3 deletions docs/source-class-DSchoenbauer.DotNotation.ArrayDotNotation.html
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ <h3>Exceptions</h3>
</span><span id="223" class="l"><a href="#223">223: </a> }
</span><span id="224" class="l"><a href="#224">224: </a>
</span><span id="225" class="l"><a href="#225">225: </a> <span class="php-comment">/**
</span></span><span id="226" class="l"><a href="#226">226: </a><span class="php-comment"> * consistantly parses notation keys
</span></span><span id="226" class="l"><a href="#226">226: </a><span class="php-comment"> * consistently parses notation keys
</span></span><span id="227" class="l"><a href="#227">227: </a><span class="php-comment"> * @param type $notation key path to a value in an array
</span></span><span id="228" class="l"><a href="#228">228: </a><span class="php-comment"> * @return array array of keys as delimited by the notation type
</span></span><span id="229" class="l"><a href="#229">229: </a><span class="php-comment"> */</span>
Expand All @@ -353,8 +353,26 @@ <h3>Exceptions</h3>
</span><span id="250" class="l"><a href="#250">250: </a> <span class="php-keyword1">return</span> <span class="php-var">$this</span>;
</span><span id="251" class="l"><a href="#251">251: </a> }
</span><span id="252" class="l"><a href="#252">252: </a>
</span><span id="253" class="l"><a href="#253">253: </a>}
</span><span id="254" class="l"><a href="#254">254: </a></span></code></pre>
</span><span id="253" class="l"><a href="#253">253: </a> <span class="php-comment">/**
</span></span><span id="254" class="l"><a href="#254">254: </a><span class="php-comment"> * Checks to see if a dot notation path is present in the data set.
</span></span><span id="255" class="l"><a href="#255">255: </a><span class="php-comment"> * @param string $dotNotation dot notation representation of keys of where to remove a value
</span></span><span id="256" class="l"><a href="#256">256: </a><span class="php-comment"> * @return boolean returns true if the do notation path exists in the data
</span></span><span id="257" class="l"><a href="#257">257: </a><span class="php-comment"> */</span>
</span><span id="258" class="l"><a href="#258">258: </a> <span class="php-keyword1">public</span> <span class="php-keyword1">function</span> has(<span class="php-var">$dotNotation</span>) {
</span><span id="259" class="l"><a href="#259">259: </a> <span class="php-var">$keys</span> = <span class="php-var">$this</span>-&gt;getKeys(<span class="php-var">$dotNotation</span>);
</span><span id="260" class="l"><a href="#260">260: </a> <span class="php-var">$dataRef</span> = &amp;<span class="php-var">$this</span>-&gt;_data;
</span><span id="261" class="l"><a href="#261">261: </a> <span class="php-keyword1">foreach</span> (<span class="php-var">$keys</span> <span class="php-keyword1">as</span> <span class="php-var">$key</span>) {
</span><span id="262" class="l"><a href="#262">262: </a> <span class="php-keyword1">if</span> (!<span class="php-keyword2">is_array</span>(<span class="php-var">$dataRef</span>) || !<span class="php-keyword2">array_key_exists</span>(<span class="php-var">$key</span>, <span class="php-var">$dataRef</span>)) {
</span><span id="263" class="l"><a href="#263">263: </a> <span class="php-keyword1">return</span> <span class="php-keyword1">false</span>;
</span><span id="264" class="l"><a href="#264">264: </a> } <span class="php-keyword1">else</span> {
</span><span id="265" class="l"><a href="#265">265: </a> <span class="php-var">$dataRef</span> = &amp;<span class="php-var">$dataRef</span>[<span class="php-var">$key</span>];
</span><span id="266" class="l"><a href="#266">266: </a> }
</span><span id="267" class="l"><a href="#267">267: </a> }
</span><span id="268" class="l"><a href="#268">268: </a> <span class="php-keyword1">return</span> <span class="php-keyword1">true</span>;
</span><span id="269" class="l"><a href="#269">269: </a> }
</span><span id="270" class="l"><a href="#270">270: </a>
</span><span id="271" class="l"><a href="#271">271: </a>}
</span><span id="272" class="l"><a href="#272">272: </a></span></code></pre>

<div id="footer">
API documentation generated by <a href="http://apigen.org">ApiGen</a>
Expand Down
20 changes: 19 additions & 1 deletion src/DotNotation/ArrayDotNotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ protected function recursiveRemove(array &$data, array $keys) {
}

/**
* consistantly parses notation keys
* consistently parses notation keys
* @param type $notation key path to a value in an array
* @return array array of keys as delimited by the notation type
*/
Expand All @@ -250,4 +250,22 @@ public function setNotationType($notationType = ".") {
return $this;
}

/**
* Checks to see if a dot notation path is present in the data set.
* @param string $dotNotation dot notation representation of keys of where to remove a value
* @return boolean returns true if the dot notation path exists in the data
*/
public function has($dotNotation) {
$keys = $this->getKeys($dotNotation);
$dataRef = &$this->_data;
foreach ($keys as $key) {
if (!is_array($dataRef) || !array_key_exists($key, $dataRef)) {
return false;
} else {
$dataRef = &$dataRef[$key];
}
}
return true;
}

}
9 changes: 9 additions & 0 deletions tests/DotNotation/ArrayDotNotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,5 +179,14 @@ public function testChangeNotationTypeSameLevelDefaultValue() {
$this->assertEquals('noValue', $this->_object->setNotationType('-')->get('levelA-levelC', 'noValue'));
}

public function testHas(){
$this->assertTrue($this->_object->has('levelA'));
$this->assertTrue($this->_object->has('levelA.levelB'));
$this->assertTrue($this->_object->has('levelB'));
$this->assertTrue($this->_object->has('level1'));
$this->assertTrue($this->_object->has('level1.level2'));
$this->assertFalse($this->_object->has('level1.level2.level3'));
$this->assertFalse($this->_object->has('level2'));
}

}

0 comments on commit 0697b9a

Please sign in to comment.