Accessing properties, keys and instance methods without checking them all the time.
VarCheck is a single class to verify nested complex variable without lots of isset() and exist()., (*2)
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 ..., (*3)
$myComplexVar = array(1 => new stdClass()); $myComplexVar[1]->name = 'John Doe';
// Get the value: $output = isset($myComplexVar[1]) && isset($myComplexVar[1]->name) ? $myComplexVar[1]->name : $otherwise;
$output = VarCheck::take($myComplexVar)->key(1)->attr('name')->value($otherwise); // or even simpler: $output = VarCheck::take($myComplexVar)->{'1'}->name->value($otherwise);
VarCheck::take($myComplexVar)->key(1)->attr('name')->exist(); // TRUE; // or: VarCheck::take($myComplexVar)->{'1'}->name->exist(); // TRUE;
VarCheck::take($myComplexVar)->key(1)->attr('name')->value(); // John Doe;
// Instead of this: $value = isset($variable['key']['foo']->element) ? my_function($variable['key']['foo']->element) : NULL; // Do this: $value = VarCheck::take($variable)->key->foo->element->my_function(); // Or: $myClassInstance; $value = arCheck::take($variable)->key->foo->element->call(array($myClassInstance, 'instanceFunction'));
VarCheck::take($myComplexVar)->key(1)->attr('job')->exist(); // FALSE; VarCheck::take($myComplexVar)->key(1)->attr('job')->attr('title')->exist(); // FALSE;
if ($value = VarCheck::take($form_status)->key('values')->key('#node')->attr('field_image')->key(LANGUAGE_NONE)->key(0)->key('item')->key('fid')->value()) { // Use $value; } // or: if ($value = VarCheck::take($form_status)->values->{'#node'}->field_image->{LANGUAGE_NONE}->{'0'}->item->fid->value()) { // Use $value; }
VarCheck::take($myVar)->key(3)->attr('title')->call(function ($v) { return $v > 10; });