Skip to content

Commit

Permalink
ifundefined()
Browse files Browse the repository at this point in the history
  • Loading branch information
Alorel committed Oct 5, 2015
1 parent ef1bde2 commit 2b9a481
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
/.sensiolabs.yml export-ignore
/.travis.yml export-ignore
/phpunit.* export-ignore
/CHANGELOG.md export-ignore
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# 1.1 #

`Alo::ifundefined()` added
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ Latest release API documentation: [https://aloframework.github.io/common/](https

| dev-develop | Latest release |
|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------:|:-------------------------------------------------------------------------------------------------------------------------------------:|
| [![Dev Build Status](https://travis-ci.org/aloframework/common.svg?branch=develop)](https://travis-ci.org/aloframework/common) | [![Release Build Status](https://travis-ci.org/aloframework/common.svg?branch=1.0.2)](https://travis-ci.org/aloframework/common) |
| [![Dev Build Status](https://travis-ci.org/aloframework/common.svg?branch=develop)](https://travis-ci.org/aloframework/common) | [![Release Build Status](https://travis-ci.org/aloframework/common.svg?branch=1.1)](https://travis-ci.org/aloframework/common) |
| [![SensioLabsInsight](https://insight.sensiolabs.com/projects/2f980e0c-215e-4d11-8313-4d071b32b4fd/mini.png)](https://insight.sensiolabs.com/projects/2f980e0c-215e-4d11-8313-4d071b32b4fd) | [![SensioLabsInsight](https://i.imgur.com/KygqLtf.png)](https://insight.sensiolabs.com/projects/2f980e0c-215e-4d11-8313-4d071b32b4fd) |
52 changes: 51 additions & 1 deletion docs/class-AloFramework.Common.Alo.html
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ <h1>Class Alo</h1>

<b>Author:</b>
Art <a href="&#x6d;&#x61;&#x69;&#x6c;&#x74;&#x6f;&#x3a;&#x61;&#x2e;&#x6d;&#x6f;&#x6c;&#x63;&#x61;&#x6e;&#x6f;&#x76;&#x61;&#x73;&#x40;&#x67;&#x6d;&#97;&#105;&#108;&#46;&#99;&#111;&#109;">&#x61;&#x2e;&#x6d;&#x6f;&#x6c;&#x63;&#x61;&#x6e;&#x6f;&#x76;&#x61;&#x73;&#x40;&#x67;&#x6d;&#97;&#105;&#108;&#46;&#99;&#111;&#109;</a><br>
<b>Located at</b> class/Alo.php <br>
<b>Since:</b>
1.1 ifundefined() added<br>
<b>Located at</b> Alo.php <br>
</div>


Expand Down Expand Up @@ -371,6 +373,54 @@ <h4>Author</h4>
</div>


</div>
</div></td>
</tr>
<tr data-order="ifundefined" id="_ifundefined">

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

</code>
</td>

<td class="name"><div>
<a class="anchor" href="#_ifundefined">#</a>
<code>ifundefined( <span>string <var>$const</var></span>, <span>mixed <var>$planB</var></span> )</code>

<div class="description short">
<p>Returns the value of the constant with the name of $const if it's defined, $planB if it's not</p>
</div>

<div class="description detailed hidden">
<p>Returns the value of the constant with the name of $const if it's defined, $planB if it's not</p>


<h4>Parameters</h4>
<div class="list"><dl>
<dt><var>$const</var></dt>
<dd>Constant name</dd>
<dt><var>$planB</var></dt>
<dd>What to return if $const isn't defined</dd>
</dl></div>

<h4>Returns</h4>
<div class="list">
mixed
</div>


<h4>Author</h4>
<div class="list">
Art <a href="&#x6d;&#x61;&#x69;&#x6c;&#x74;&#x6f;&#x3a;&#x61;&#x2e;&#x6d;&#x6f;&#x6c;&#x63;&#x61;&#x6e;&#x6f;&#x76;&#x61;&#x73;&#x40;&#x67;&#x6d;&#97;&#105;&#108;&#46;&#99;&#111;&#109;">&#x61;&#x2e;&#x6d;&#x6f;&#x6c;&#x63;&#x61;&#x6e;&#x6f;&#x76;&#x61;&#x73;&#x40;&#x67;&#x6d;&#97;&#105;&#108;&#46;&#99;&#111;&#109;</a><br>
</div>
<h4>Since</h4>
<div class="list">
1.1<br>
</div>


</div>
</div></td>
</tr>
Expand Down
15 changes: 15 additions & 0 deletions src/Alo.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/**
* Static common component container
* @author Art <[email protected]>
* @since 1.1 ifundefined() added
*/
abstract class Alo {

Expand Down Expand Up @@ -105,6 +106,20 @@ static function ifnull(&$var, $planB, $useNullget = false) {
return $v !== null ? $v : $planB;
}

/**
* Returns the value of the constant with the name of $const if it's defined, $planB if it's not
* @author Art <[email protected]>
*
* @param string $const Constant name
* @param mixed $planB What to return if $const isn't defined
*
* @return mixed
* @since 1.1
*/
static function ifundefined($const, $planB) {
return defined($const) ? constant($const) : $planB;
}

/**
* Checks if the request was made via AJAX
* @author Art <[email protected]>
Expand Down
24 changes: 24 additions & 0 deletions tests/AloTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,28 @@ function pathProvider() {
[__DIR__, false],
['/nonexistent/' . mt_rand(~PHP_INT_MAX, PHP_INT_MAX), false]];
}

function testIfundefined() {
$const1 = self::generateConstName();
$const2 = self::generateConstName();
$planB = 'bar';

while (defined($const1)) {
$const1 = self::generateConstName();
}

while (defined($const2)) {
$const2 = self::generateConstName();
}

define($const1, 'foo');

$this->assertEquals(constant($const1), Alo::ifundefined($const1, $planB));
$this->assertEquals($planB, Alo::ifundefined($const2, $planB));
}

private static function generateConstName() {
return hash('sha512',
mt_rand(~PHP_INT_MAX, PHP_INT_MAX) . uniqid(mt_rand(~PHP_INT_MAX, PHP_INT_MAX), true));
}
}

0 comments on commit 2b9a481

Please sign in to comment.