Skip to content

Instantly share code, notes, and snippets.

@kfriend
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kfriend/453a2e6d6d1260e66004 to your computer and use it in GitHub Desktop.
Save kfriend/453a2e6d6d1260e66004 to your computer and use it in GitHub Desktop.
Simple array proxy class. Helpful when interacting with $_POST, $_GET, etc
<?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