public function mapBy(array $input, $key)
    {
        return array_map(function ($item) use ($key) {
            if (is_array($item)) {
                if (!array_key_exists($key, $item)) {
                    throw new \LogicException("Could not map item by key \"$key\". Array key does not exist.");
                }

                return $item[$key];
            }

            // TODO: use PropertyAccessor ??
            if (!is_object($item)) {
                throw new \InvalidArgumentException("Item must be an array or object.");
            }

            $ref = new \ReflectionClass($item);

            if ($ref->hasProperty($key) && $ref->getProperty($key)->isPublic()) {
                return $item->$key;
            }

            if ($ref->hasMethod($key) && !$ref->getMethod($key)->isPrivate()) {
                return $item->$key();
            }

            $get = 'get'.ucfirst($key);
            if ($ref->hasMethod($get) && !$ref->getMethod($get)->isPrivate()) {
                return $item->$get();
            }

            $is = 'is'.ucfirst($key);
            if ($ref->hasMethod($is) && !$ref->getMethod($is)->isPrivate()) {
                return $item->$is();
            }

            throw new \LogicException("Could not map item by key \"$key\". Cannot access the property directly or through getter/is method.");

        }, $input);
    }
public function mapBy(array $input, $key)
    {
        return array_map(function ($item) use ($key) {
            if (is_array($item)) {
                if (!array_key_exists($key, $item)) {
                    throw new \LogicException("Could not map item by key \"$key\". Array key does not exist.");
                }

                return $item[$key];
            }

            // TODO: use PropertyAccessor ??
            if (!is_object($item)) {
                throw new \InvalidArgumentException("Item must be an array or object.");
            }

            $ref = new \ReflectionClass($item);

            if ($ref->hasProperty($key) && $ref->getProperty($key)->isPublic()) {
                return $item->$key;
            }

            if ($ref->hasMethod($key) && !$ref->getMethod($key)->isPrivate()) {
                return $item->$key();
            }

            $get = 'get'.ucfirst($key);
            if ($ref->hasMethod($get) && !$ref->getMethod($get)->isPrivate()) {
                return $item->$get();
            }

            $is = 'is'.ucfirst($key);
            if ($ref->hasMethod($is) && !$ref->getMethod($is)->isPrivate()) {
                return $item->$is();
            }

            throw new \LogicException("Could not map item by key \"$key\". Cannot access the property directly or through getter/is method.");

        }, $input);
    }
public function mapBy ( array $ input , $ key ) { return array_map ( function ( $ item ) use ( $ key ) { if ( is_array ( $ item ) ) { if ( ! array_key_exists ( $ key , $ item ) ) { throw new \ LogicException ( "Could not map item by key \"$key\". Array key does not exist." ) ; } return $ item [ $ key ] ; } // TODO: use PropertyAccessor ?? if ( ! is_object ( $ item ) ) { throw new \ InvalidArgumentException ( "Item must be an array or object." ) ; } $ ref = new \ ReflectionClass ( $ item ) ; if ( $ ref -> hasProperty ( $ key ) && $ ref -> getProperty ( $ key ) -> isPublic ( ) ) { return $ item -> $ key ; } if ( $ ref -> hasMethod ( $ key ) && ! $ ref -> getMethod ( $ key ) -> isPrivate ( ) ) { return $ item -> $ key ( ) ; } $ get = 'get' . ucfirst ( $ key ) ; if ( $ ref -> hasMethod ( $ get ) && ! $ ref -> getMethod ( $ get ) -> isPrivate ( ) ) { return $ item -> $ get ( ) ; } $ is = 'is' . ucfirst ( $ key ) ; if ( $ ref -> hasMethod ( $ is ) && ! $ ref -> getMethod ( $ is ) -> isPrivate ( ) ) { return $ item -> $ is ( ) ; } throw new \ LogicException ( "Could not map item by key \"$key\". Cannot access the property directly or through getter/is method." ) ; } , $ input ) ; }
Map collection by key. For objects, return the property, use
get method or is method, if avaliable.

@param  array                     $input Array of arrays or objects.
@param  string                    $key   Key to map by.
@throws \InvalidArgumentException If array item is not an array or object.
@throws \LogicException           If array item could not be mapped by given key.
@return array                     Mapped array.
Map collection by key . For objects return the property use get method or is method if avaliable .