Add new methods

This commit is contained in:
CismonX 2018-03-24 12:22:00 +08:00
parent 604c7b1ee6
commit d59af14f7a
7 changed files with 110 additions and 60 deletions

View File

@ -14,5 +14,5 @@ script:
- phpize
- ./configure
- make
- make test
- echo $'n\n' | make test
- sudo make install

View File

@ -9,22 +9,30 @@
#include "php_collections.h"
#include "php_collections_me.h"
#define NEW_COLLECTION_OBJ(name) \
#define NEW_OBJ(name, ce) \
zend_object* (name) = (zend_object*)ecalloc(1, sizeof(zend_object) + \
zend_object_properties_size(collections_collection_ce)); \
zend_object_std_init(name, collections_collection_ce); \
object_properties_init(name, collections_collection_ce); \
obj->handlers = &std_object_handlers;
zend_object_properties_size(ce)); \
zend_object_std_init(name, ce); \
object_properties_init(name, ce); \
(name)->handlers = &std_object_handlers;
#define NEW_COLLECTION_OBJ(name) NEW_OBJ(name, collections_collection_ce)
#define NEW_PAIR_OBJ(name) NEW_OBJ(name, collections_pair_ce)
#define IS_COLLECTION(zval) \
Z_TYPE_P(elements) == IS_OBJECT && Z_OBJCE_P(elements) == collections_collection_ce
#define COLLECTION_UPDATE(obj, value) \
zend_update_property_ex(zend_get_executed_scope(), obj, collection_property_name, value)
#define OBJ_PROPERTY_UPDATE(obj, property_name, value) \
zend_update_property_ex(zend_get_executed_scope(), obj, property_name, value)
#define OBJ_PROPERTY_FETCH(obj, property_name) \
zend_read_property_ex(zend_get_executed_scope(), obj, property_name, 1, &rv)
#define COLLECTION_UPDATE(obj, value) OBJ_PROPERTY_UPDATE(obj, collection_property_name, value)
#define COLLECTION_UPDATE_EX(value) COLLECTION_UPDATE(getThis(), value)
#define COLLECTION_FETCH(obj) \
zend_read_property_ex(zend_get_executed_scope(), obj, collection_property_name, 1, &rv)
#define COLLECTION_FETCH(obj) OBJ_PROPERTY_FETCH(obj, collection_property_name)
#define COLLECTION_FETCH_EX() COLLECTION_FETCH(getThis())
#define PAIR_UPDATE_FIRST(obj, value) OBJ_PROPERTY_UPDATE(obj, pair_first_name, value)
#define PAIR_UPDATE_SECOND(obj, value) OBJ_PROPERTY_UPDATE(obj, pair_second_name, value)
#define PAIR_FETCH_FIRST(obj) OBJ_PROPERTY_FETCH(obj, pair_first_name)
#define PAIR_FETCH_SECOND(obj) OBJ_PROPERTY_FETCH(obj, pair_second_name)
#define PHP_COLLECTIONS_ERROR(type, msg) php_error_docref(NULL, type, msg)
#define ERR_BAD_ARGUMENT_TYPE() PHP_COLLECTIONS_ERROR(E_WARNING, "Bad argument type")
@ -78,8 +86,8 @@ PHP_METHOD(Collection, all)
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_FUNC(fci, fcc)
ZEND_PARSE_PARAMETERS_END();
zval params[3], rv, retval;
fci.param_count = 3;
zval params[2], rv, retval;
fci.param_count = 2;
fci.retval = &retval;
fci.params = params;
zval* current = COLLECTION_FETCH_EX();
@ -88,8 +96,7 @@ PHP_METHOD(Collection, all)
if (bucket->key)
ZVAL_STR(&params[1], bucket->key);
else
ZVAL_NULL(&params[1]);
ZVAL_LONG(&params[2], bucket->h);
ZVAL_LONG(&params[1], bucket->h);
zend_call_function(&fci, &fcc);
if (Z_TYPE(retval) == IS_FALSE)
RETURN_FALSE;
@ -99,7 +106,27 @@ PHP_METHOD(Collection, all)
PHP_METHOD(Collection, any)
{
zend_fcall_info fci;
zend_fcall_info_cache fcc;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_FUNC(fci, fcc)
ZEND_PARSE_PARAMETERS_END();
zval params[2], rv, retval;
fci.param_count = 2;
fci.retval = &retval;
fci.params = params;
zval* current = COLLECTION_FETCH_EX();
ZEND_HASH_FOREACH_BUCKET(Z_ARRVAL_P(current), Bucket* bucket)
ZVAL_COPY(&params[0], &bucket->val);
if (bucket->key)
ZVAL_STR(&params[1], bucket->key);
else
ZVAL_LONG(&params[1], bucket->h);
zend_call_function(&fci, &fcc);
if (Z_TYPE(retval) == IS_TRUE)
RETURN_TRUE;
ZEND_HASH_FOREACH_END();
RETURN_FALSE;
}
PHP_METHOD(Collection, associate)
@ -649,5 +676,12 @@ PHP_METHOD(Collection, values)
PHP_METHOD(Pair, __construct)
{
zval* first;
zval* second;
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_ZVAL(first)
Z_PARAM_ZVAL(second)
ZEND_PARSE_PARAMETERS_END();
PAIR_UPDATE_FIRST(getThis(), first);
PAIR_UPDATE_SECOND(getThis(), second);
}

View File

@ -22,7 +22,7 @@ class Collection implements ArrayAccess, Countable
/**
* Returns true if all elements match the given predicate.
*
* @param callable $predicate ($value, $key, $index) -> bool
* @param callable $predicate ($value, $key) -> bool
* @return bool
*/
function all($predicate) {}
@ -30,7 +30,7 @@ class Collection implements ArrayAccess, Countable
/**
* Returns true if at least one element matches the given predicate.
*
* @param callable $predicate ($value, $key, $index) -> bool
* @param callable $predicate ($value, $key) -> bool
* @return bool
*/
function any($predicate) {}
@ -141,7 +141,7 @@ class Collection implements ArrayAccess, Countable
* Returns a collection containing only elements from the given array having distinct keys
* returned by the given selector function.
*
* @param callable $selector ($value, $key, $index) -> $key
* @param callable $selector ($value, $key) -> $key
* @return Collection
*/
function distinctBy($selector) {}
@ -166,7 +166,7 @@ class Collection implements ArrayAccess, Countable
* Returns a collection containing all elements except last elements that satisfy
* the given predicate.
*
* @param callable $predicate ($value, $key, $index) -> bool
* @param callable $predicate ($value, $key) -> bool
* @return Collection
*/
function dropLastWhile($predicate) {}
@ -175,7 +175,7 @@ class Collection implements ArrayAccess, Countable
* Returns a collection containing all elements except first elements that satisfy
* the given predicate.
*
* @param $predicate $predicate ($value, $key, $index) -> bool
* @param $predicate $predicate ($value, $key) -> bool
* @return Collection
*/
function dropWhile($predicate) {}
@ -193,7 +193,7 @@ class Collection implements ArrayAccess, Countable
/**
* Returns a collection containing only elements matching the given predicate.
*
* @param callable $predicate ($value, $key, $index) -> bool
* @param callable $predicate ($value, $key) -> bool
* @return Collection
*/
function filter($predicate) {}
@ -201,7 +201,7 @@ class Collection implements ArrayAccess, Countable
/**
* Returns a collection containing only elements not matching the given predicate.
*
* @param callable $predicate ($value, $key, $index) -> bool
* @param callable $predicate ($value, $key) -> bool
* @return Collection
*/
function filterNot($predicate) {}
@ -210,7 +210,7 @@ class Collection implements ArrayAccess, Countable
* Appends all elements not matching the given predicate to the given destination.
*
* @param Collection $destination
* @param callable $predicate ($value, $key, $index) -> bool
* @param callable $predicate ($value, $key) -> bool
* @return Collection
*/
function filterNotTo($destination, $predicate) {}
@ -219,7 +219,7 @@ class Collection implements ArrayAccess, Countable
* Appends all elements matching the given predicate to the given destination.
*
* @param Collection $destination
* @param callable $predicate ($value, $key, $index) -> bool
* @param callable $predicate ($value, $key) -> bool
* @return Collection
*/
function filterTo($destination, $predicate) {}
@ -228,7 +228,7 @@ class Collection implements ArrayAccess, Countable
* Returns the first element matching the given predicate, or null if no such
* element was found.
*
* @param callable $predicate ($value, $key, $index) -> bool
* @param callable $predicate ($value, $key) -> bool
* @return mixed
*/
function find($predicate) {}
@ -237,7 +237,7 @@ class Collection implements ArrayAccess, Countable
* Returns the last element matching the given predicate, or null if no such
* element was found.
*
* @param callable $predicate ($value, $key, $index) -> bool
* @param callable $predicate ($value, $key) -> bool
* @return mixed
*/
function findLast($predicate) {}
@ -245,7 +245,7 @@ class Collection implements ArrayAccess, Countable
/**
* Returns the first element matching the given predicate.
*
* @param callable $predicate[optional] ($value, $key, $index) -> bool
* @param callable $predicate[optional] ($value, $key) -> bool
* @return mixed
*/
function first($predicate) {}
@ -254,7 +254,7 @@ class Collection implements ArrayAccess, Countable
* Returns a collection of all elements yielded from results of transform function
* being invoked on each element of original collection.
*
* @param callable $transform ($value, $key, $index) -> array|Collection
* @param callable $transform ($value, $key) -> array|Collection
* @return Collection
*/
function flatMap($transform) {}
@ -264,7 +264,7 @@ class Collection implements ArrayAccess, Countable
* original collection, to the given destination.
*
* @param Collection $destination
* @param callable $transform ($value, $key, $index) -> array|Collection
* @param callable $transform ($value, $key) -> array|Collection
* @return Collection
*/
function flatMapTo($destination, $transform) {}
@ -299,7 +299,7 @@ class Collection implements ArrayAccess, Countable
/**
* Performs the given action on each element.
*
* @param callable $action ($value, $key, $index) -> void
* @param callable $action ($value, $key) -> void
* @return void
*/
function forEach($action) {}
@ -319,7 +319,7 @@ class Collection implements ArrayAccess, Countable
* original array by the key returned by the given key_selector function applied to the element
* and returns a collection where each group key is associated with a list of corresponding values.
*
* @param callable $key_selector ($value, $key, $index) -> $new_key
* @param callable $key_selector ($value, $key) -> $new_key
* @param callable $value_transform[optional] ($value) -> $new_value
* @return Collection
*/
@ -331,7 +331,7 @@ class Collection implements ArrayAccess, Countable
* puts to the destination collection each group key associated with a list of corresponding values.
*
* @param Collection $destination
* @param callable $key_selector ($value, $key, $index) -> $new_key
* @param callable $key_selector ($value, $key) -> $new_key
* @param callable $value_transform[optional] ($value) -> $new_value
* @return Collection
*/
@ -366,7 +366,7 @@ class Collection implements ArrayAccess, Countable
/**
* Initialize collection with given data.
*
* @param array|Collection $elements[$optional]
* @param array|Collection $elements[optional]
* @return Collection
*/
static function init($elements) {}
@ -401,7 +401,7 @@ class Collection implements ArrayAccess, Countable
/**
* Returns the last element matching the given predicate.
*
* @param callable $predicate[optional] ($value, $key, $index) -> bool
* @param callable $predicate[optional] ($value, $key) -> bool
* @return mixed
*/
function last($predicate) {}
@ -418,7 +418,7 @@ class Collection implements ArrayAccess, Countable
* Returns a collection containing the results of applying the given transform function
* to each element in the original collection.
*
* @param callable $transform ($value, $key, $index) -> Pair
* @param callable $transform ($value, $key) -> Pair
* @return Collection
*/
function map($transform) {}
@ -427,7 +427,7 @@ class Collection implements ArrayAccess, Countable
* Returns a new collection with entries having the keys obtained by applying the transform function
* to each keys and values of this collection.
*
* @param callable $transform ($value, $key, $index) -> $new_key
* @param callable $transform ($value, $key) -> $new_key
* @return Collection
*/
function mapKeys($transform) {}
@ -437,7 +437,7 @@ class Collection implements ArrayAccess, Countable
* transform function to each keys and values of this collections.
*
* @param Collection $destination
* @param callable $transform ($value, $key, $index) -> $new_key
* @param callable $transform ($value, $key) -> $new_key
* @return Collection
*/
function mapKeysTo($destination, $transform) {}
@ -447,7 +447,7 @@ class Collection implements ArrayAccess, Countable
* results to the given destination.
*
* @param Collection $destination
* @param callable $transform ($value, $key, $index) -> Pair
* @param callable $transform ($value, $key) -> Pair
* @return Collection
*/
function mapTo($destination, $transform) {}
@ -456,7 +456,7 @@ class Collection implements ArrayAccess, Countable
* Returns a new collection with entries having the keys of this collection and the values obtained
* by applying the transform function to each entry in this collection.
*
* @param callable $transform ($value, $key, $index) -> $new_value
* @param callable $transform ($value, $key) -> $new_value
* @return Collection
*/
function mapValues($transform) {}
@ -466,7 +466,7 @@ class Collection implements ArrayAccess, Countable
* the values obtained by applying the transform function to each entry in this collection.
*
* @param Collection $destination
* @param callable $transform ($value, $key, $index) -> $new_value
* @param callable $transform ($value, $key) -> $new_value
* @return Collection
*/
function mapValuesTo($destination, $transform) {}
@ -482,7 +482,7 @@ class Collection implements ArrayAccess, Countable
* Returns the first element yielding the largest value of the given function or null if
* there are no elements.
*
* @param callable $selector ($value, $key, $index) -> $new_value
* @param callable $selector ($value, $key) -> $new_value
* @return mixed
*/
function maxBy($selector) {}
@ -507,7 +507,7 @@ class Collection implements ArrayAccess, Countable
* Returns the first element yielding the smallest value of the given function or null if
* there are no elements.
*
* @param callable $selector ($value, $key, $index) -> $new_value
* @param callable $selector ($value, $key) -> $new_value
* @return mixed
*/
function minBy($selector) {}
@ -576,7 +576,7 @@ class Collection implements ArrayAccess, Countable
/**
* Returns true if no elements match the given predicate.
*
* @param callable $predicate ($value, $key, $index) -> bool
* @param callable $predicate ($value, $key) -> bool
* @return bool
*/
function none($predicate) {}
@ -604,7 +604,7 @@ class Collection implements ArrayAccess, Countable
/**
* Performs the given action on each element and returns the collection itself afterwards.
*
* @param callable $action ($value, $key, $index) -> void
* @param callable $action ($value, $key) -> void
* @return Collection
*/
function onEach($action) {}
@ -621,7 +621,7 @@ class Collection implements ArrayAccess, Countable
* elements for which predicate yielded true, while second collection contains elements for
* which predicate yielded false.
*
* @param callable $predicate ($value, $key, $index) -> bool
* @param callable $predicate ($value, $key) -> bool
* @return Pair
*/
function partition($predicate) {}
@ -697,7 +697,7 @@ class Collection implements ArrayAccess, Countable
/**
* Removes all elements from this collection that match the given predicate.
*
* @param callable $predicate ($value, $key, $index) -> bool
* @param callable $predicate ($value, $key) -> bool
* @return bool
*/
function removeAll($predicate) {}
@ -705,7 +705,7 @@ class Collection implements ArrayAccess, Countable
/**
* Retains only elements of this collection that match the given predicate.
*
* @param callable $predicate ($value, $key, $index) -> bool
* @param callable $predicate ($value, $key) -> bool
* @return bool
*/
function retainAll($predicate) {}
@ -735,7 +735,7 @@ class Collection implements ArrayAccess, Countable
* Returns the single element matching the given predicate, or null if there is no or more than
* one matching element.
*
* @param callable $predicate ($value, $key, $index) -> bool
* @param callable $predicate ($value, $key) -> bool
* @return mixed
*/
function single($predicate) {}
@ -766,7 +766,7 @@ class Collection implements ArrayAccess, Countable
* Sorts elements in the collection in-place according to natural sort order of the value returned
* by specified selector function.
*
* @param callable $selector ($value, $key, $index) -> $new_value
* @param callable $selector ($value, $key) -> $new_value
* @return void
*/
function sortBy($selector) {}
@ -775,7 +775,7 @@ class Collection implements ArrayAccess, Countable
* Sorts elements in the collection in-place descending according to natural sort order of the
* value returned by specified selector function.
*
* @param callable $selector ($value, $key, $index) -> $new_value
* @param callable $selector ($value, $key) -> $new_value
* @return void
*/
function sortByDescending($selector) {}
@ -806,7 +806,7 @@ class Collection implements ArrayAccess, Countable
* Returns a collection of all elements sorted according to natural sort order of the
* value returned by specified selector function.
*
* @param callable $selector ($value, $key, $index) -> $new_value
* @param callable $selector ($value, $key) -> $new_value
* @return Collection
*/
function sortedBy($selector) {}
@ -815,7 +815,7 @@ class Collection implements ArrayAccess, Countable
* Returns a collection of all elements sorted descending according to natural sort order
* of the value returned by specified selector function.
*
* @param callable $selector ($value, $key, $index) -> $new_value
* @param callable $selector ($value, $key) -> $new_value
* @return Collection
*/
function sortedByDescending($selector) {}
@ -823,7 +823,7 @@ class Collection implements ArrayAccess, Countable
/**
* Returns a collection of all elements sorted descending according to their natural sort order.
*
* @param callable $selector ($value, $key, $index) -> $new_value
* @param callable $selector ($value, $key) -> $new_value
* @return Collection
*/
function sortedDescending($selector) {}
@ -855,7 +855,7 @@ class Collection implements ArrayAccess, Countable
/**
* Returns a collection containing last elements satisfying the given predicate.
*
* @param $predicate ($value, $key, $index) -> bool
* @param $predicate ($value, $key) -> bool
* @return Collection
*/
function takeLastWhile($predicate) {}
@ -863,7 +863,7 @@ class Collection implements ArrayAccess, Countable
/**
* Returns a collection containing first elements satisfying the given predicate.
*
* @param $predicate ($value, $key, $index) -> bool
* @param $predicate ($value, $key) -> bool
* @return Collection
*/
function takeWhile($predicate) {}

9
tests/001-pair.phpt Normal file
View File

@ -0,0 +1,9 @@
--TEST--
Test Pair.
--FILE--
<?php
$pair = new Pair('foo', 'bar');
if ($pair->first != 'foo' || $pair->second != 'bar')
echo 'Test for Pair failed.', PHP_EOL;
?>
--EXPECT--

View File

@ -4,8 +4,9 @@ Test Collection::init().
<?php
$array = ['a' => 'b'];
$collection = Collection::init($array);
$collection2 = Collection::init($collection);
if ($array != $collection2->toArray())
$collection1 = Collection::init($collection);
$collection2 = Collection::init();
if ($array != $collection1->toArray() || $collection2->toArray() != [])
echo 'Collection::init() failed.', PHP_EOL;
?>
--EXPECT--

View File

@ -10,10 +10,16 @@ $collection = Collection::init([
$result = $collection->all(function ($value) {
return $value < 100;
});
$result2 = $collection->all(function ($value, $key) {
$result1 = $collection->all(function ($value, $key) {
return strlen($key) < 4;
});
if ($result || !$result2)
$result2 = $collection->any(function ($value) {
return $value % 3 == 0;
});
$result3 = $collection->any(function ($value, $key) {
return strpos($key, 'g') !== false;
});
if ($result || !$result1 || !$result2 || $result3)
echo 'Collection::all() failed.', PHP_EOL;
?>
--EXPECT--