Last active
August 29, 2015 14:01
-
-
Save kfriend/453a2e6d6d1260e66004 to your computer and use it in GitHub Desktop.
Simple array proxy class. Helpful when interacting with $_POST, $_GET, etc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Kfriend\Data; | |
use Closure; | |
class Proxy extends \ArrayObject | |
{ | |
public function __invoke($item, $default = null) | |
{ | |
if ($this->offsetExists($item)) { return $this->offsetGet($item); } | |
return $default; | |
} | |
public function offsetGet($item) | |
{ | |
if ($this->offsetExists($item)) { return parent::offsetGet($item); } | |
return null; | |
} | |
public function isTrue($condition, $whenTrue, $whenFalse) | |
{ | |
$isTrue = false; | |
if ($condition instanceof Closure) { | |
$isTrue = (bool) $condition(); | |
} | |
else { | |
$isTrue = (bool) $condition; | |
} | |
return ($isTrue) ? $whenTrue : $whenFalse; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment