itarato / var-check
Accessing properties, keys and instance methods without checking them all the time.
Installs: 2 589
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
This package is not auto-updated.
Last update: 2024-11-09 16:58:37 UTC
README
Changelog
#Version 2
- Use of PSR-4 autoading:
require_once __DIR__ . '/vendor/autoload.php'; use itarato\VarCheck\VC;
- Instance creation:
VC::make($variable);
- VarCheck methods got underscore (avoiding real object properties, functions or array keys):
VC::make($variable)->_value(); VC::make($variable)->_empty();
__call
magic method now calls the instance function of the current object:
class User { function getParent() { return $this->parent; } } $child = new User(); VC::make($child)->getParent()->_value();
Install
- Through composer:
"require": "itarato/var-check": "2.*@dev"
$ echo '{"require": {"itarato/var-check": "2.*@dev"}}' > composer.json ; composer install
VarCheck is a single class to verify nested complex variable without lots of isset() and exist().
To avoid multiple level of isset/exist/etc this class provides an easy way to verify nested values in a variable. Typical use case when you have a large variable, and you are not sure if it has the right index, and inside there an object, and an attribute ...
The complex variable
$myComplexVar = array(1 => new stdClass()); $myComplexVar[1]->name = 'John Doe';
Problem to solve
// Get the value: $output = isset($myComplexVar[1]) && isset($myComplexVar[1]->name) ? $myComplexVar[1]->name : $otherwise;
Solution
$output = VC::make($myComplexVar)->_key(1)->_attr('name')->_value($otherwise); // or even simpler: $output = VC::make($myComplexVar)->{'1'}->name->_value($otherwise);
Checking if the nested value exist
VC::make($myComplexVar)->_key(1)->_attr('name')->_exist(); // TRUE; // or: VC::make($myComplexVar)->{'1'}->name->_exist(); // TRUE;
Get the nested value
VC::make($myComplexVar)->_key(1)->_attr('name')->_value(); // John Doe;
Call a function on the value if exist
// Instead of this: $value = isset($variable['key']['foo']->element) ? my_function($variable['key']['foo']->element) : NULL; // Do this: $value = VC::make($variable)->key->foo->element->my_function(); // Or: $myClassInstance; $value = arCheck::make($variable)->key->foo->element->call(array($myClassInstance, 'instanceFunction'));
Failsafe check in case it does not exist
VC::make($myComplexVar)->_key(1)->_attr('job')->_exist(); // FALSE; VC::make($myComplexVar)->_key(1)->_attr('job')->_attr('title')->_exist(); // FALSE;
Check and value at the same time
if ($value = VC::make($form_status)->_key('values')->_key('#node')->_attr('field_image')->_key(LANGUAGE_NONE)->_key(0)->_key('item')->_key('fid')->_value()) { // Use $value; } // or: if ($value = VC::make($form_status)->values->{'#node'}->field_image->{LANGUAGE_NONE}->{'0'}->item->fid->_value()) { // Use $value; }
Custom validation
VC::make($myVar)->_key(3)->_attr('title')->_call(function ($v) { return $v > 10; });