Skip to content

Instantly share code, notes, and snippets.

@smailliwcs
Created September 22, 2020 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smailliwcs/779828181e2bb8d934d6e22dbc2658c0 to your computer and use it in GitHub Desktop.
Save smailliwcs/779828181e2bb8d934d6e22dbc2658c0 to your computer and use it in GitHub Desktop.
Fixing Underscore.php
@@ -176,7 +176,7 @@ all: Wrap return value
$__ = new self;
if(!is_null($iterator)) $collection = $__->map($collection, $iterator);
$collection = (array) $collection;
- if(count($collection) === 0) return true;
+ if(count($collection) === 0) return self::_wrap(true);
return self::_wrap(is_bool(array_search(false, $collection, false)));
}
@@ -191,8 +191,8 @@ filter: Pass key to iterator
$collection = self::_collection($collection);
$return = array();
- foreach($collection as $val) {
- if(call_user_func($iterator, $val)) $return[] = $val;
+ foreach($collection as $k=>$v) {
+ if(call_user_func($iterator, $v, $k, $collection)) $return[] = $v;
}
return self::_wrap($return);
}
@@ -205,8 +205,8 @@ reject: Pass key to iterator
$collection = self::_collection($collection);
$return = array();
- foreach($collection as $val) {
- if(!call_user_func($iterator, $val)) $return[] = $val;
+ foreach($collection as $k=>$v) {
+ if(!call_user_func($iterator, $v, $k, $collection)) $return[] = $v;
}
return self::_wrap($return);
}
@@ -220,8 +220,8 @@ find: Pass key to iterator and wrap return value
$collection = self::_collection($collection);
- foreach($collection as $val) {
- if(call_user_func($iterator, $val)) return $val;
+ foreach($collection as $k=>$v) {
+ if(call_user_func($iterator, $v, $k, $collection)) return self::_wrap($v);
}
return self::_wrap(false);
}
@@ -501,7 +501,7 @@ max: Wrap return value
arsort($results);
$__ = new self;
$first_key = $__->first(array_keys($results));
- return $collection[$first_key];
+ return self::_wrap($collection[$first_key]);
}
@@ -549,7 +549,7 @@ groupBy: Wrap return value
if(!array_key_exists($key, $result)) $result[$key] = array();
$result[$key][] = $v;
}
- return $result;
+ return self::_wrap($result);
}
@@ -588,7 +588,9 @@ toArray: Wrap arguments and return value
// Return the collection as an array
public function toArray($collection=null) {
- return (array) $collection;
+ list($collection) = self::_wrapArgs(func_get_args(), 1);
+
+ return self::_wrap((array) $collection);
}
@@ -615,7 +617,7 @@ extend: Wrap return value
$args = self::_wrapArgs(func_get_args(), 1);
$num_args = func_num_args();
- if($num_args === 1) return $object;
+ if($num_args === 1) return self::_wrap($object);
$is_object = is_object($object);
$array = (array) $object;
@@ -635,7 +637,7 @@ defaults: Wrap return value
list($object) = $args;
$num_args = count($args);
- if($num_args === 1) return $object;
+ if($num_args === 1) return self::_wrap($object);
$is_object = is_object($object);
$array = (array) $object;
@@ -660,7 +662,7 @@ clon: Pass argument by value
// Returns a shallow copy of the object
- public function clon(&$object=null) {
+ public function clon($object=null) {
list($object) = self::_wrapArgs(func_get_args(), 1);
$clone = null;
@@ -858,7 +860,7 @@ __call: Wrap return value
public function __call($name, $arguments) {
$mixins =& self::getInstance()->_mixins;
$arguments = self::_wrapArgs($arguments);
- return call_user_func_array($mixins[$name], $arguments);
+ return self::_wrap(call_user_func_array($mixins[$name], $arguments));
}
@@ -1045,7 +1047,7 @@ compose: Apply functions in reverse order
return self::_wrap(function() use ($functions) {
$args = func_get_args();
- foreach($functions as $function) {
+ foreach(array_reverse($functions) as $function) {
$args[0] = call_user_func_array($function, $args);
}
return $args[0];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment