Example #1
0
 /**
  * Internal: Used to implement pluck().
  */
 private function pluck_property($container, $property)
 {
     $is_object = is_object($container);
     if (is_array($container) || $is_object && $container instanceof \ArrayAccess) {
         return Crankshaft::HasKey($container, $property) ? $container[$property] : null;
     } else {
         if ($is_object) {
             // property_exists() will return true if there's a defined public property for the
             // object's class, even if the instance value of that property is null -- but it
             // will return false if the container object uses __get magic.
             //
             // isset() will return true if magic is in use and __isSet($property) is true,
             // but will return false if the value of a magic or real property is null.
             //
             // So, we perform both tests, which catches most cases. If the object defines a
             // magic property whose value is null, we assume we'll fall through to the else
             // case where we return null anyway.
             if (property_exists($container, $property) || isset($container->{$property})) {
                 return $container->{$property};
             } else {
                 if ($container instanceof PropertyContainer) {
                     return $container->get($property);
                 } else {
                     return null;
                 }
             }
         } else {
             throw new UnpluckableError('Cannot pluck ' . var_dump($property, true) . ' from ' . var_dump($container, true));
         }
     }
 }