2017 © Pedro Peláez
 

library get-in

Handy Traversal of chained objects with error trap and default value (suited for View).

image

adhocore/get-in

Handy Traversal of chained objects with error trap and default value (suited for View).

  • Sunday, July 22, 2018
  • by adhocore
  • Repository
  • 1 Watchers
  • 6 Stars
  • 5 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

about

this prevents php error "Call to a member function on a non-object" and provides elegant syntax to access the methods of chained object in a fluent way and also has default value for if it should fail at any point, (*1)

rationale and usage

for something like:, (*2)

echo $user->getGroup()->getPermission()->getName();

this scenario is not uncommon in any ORM. now, in case $user is null or $user->getGroup() is null or etc, it can be hell of a checks like:, (*3)

$default = 'some name';
if ($user) {
    $group = $user->getGroup();
    if ($group) {
        $permission = $group->getPermission();
        if ($permission){
            echo $permission->getName();
        } else {
            echo $default;
        }
    } else {
        echo $default;
    }
} else {
    echo $default;
}

or it can be quite shorter in another smart way like:, (*4)

$default = 'some name';
if ($user and 
    $group = $user->getGroup() and 
    $permission = $group->getPermission()
) {
    echo $permission->getName();
} else {
    echo $default;
}

still not perfect. adhocore/get-in saves one from this PITA by providing handy wrapper like:, (*5)

echo \Ahc\Get::in($user, 'getGroup.getPermission.getName', 'some name');

installation

edit your composer.json to include "adhocore/get-in": "1.0.*@dev" in the require section and run composer update, (*6)

advantage

  • prevents multi layer check
  • prevents errors like "Call to a member function on a non-object"
  • saves from temporary variable creation during the checks
  • provides a way to have default value should it fail at any point

the name

the name get-in is based on igorw/get-in which is similar thing for array manipulation, adhocore/get-in being for chained objects, (*7)

The Versions

22/07 2018

dev-master

9999999-dev

Handy Traversal of chained objects with error trap and default value (suited for View).

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Jitendra Adhikari

object traverse chained

24/10 2017

dev-analysis-8w52dw

dev-analysis-8w52dw

Handy Traversal of chained objects with error trap and default value (suited for View).

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Jitendra Adhikari

object traverse chained