rfc:functional-elements

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:functional-elements [2008/03/06 02:15] – Adding another use case lstrojnyrfc:functional-elements [2017/09/22 13:28] (current) – external edit 127.0.0.1
Line 1: Line 1:
-====== Functional elements for object lists ======+This RFC is still in progress 
 +====== Functional elements to work with aggregations ======
 ==== Abstract ==== ==== Abstract ====
-A common pattern in PHP is iterating through a list of objects and executing certain methods of the function. This is especially common when it comes to 1:n-relations (e.g. one object, n adapters). This proposal is meant to abstract those iterations in convenient functional constructs.+A common pattern in PHP is iterating through a list of objects and executing certain methods of the objects. This is especially common when it comes to 1:n-relations (e.g. one object, n adapters). This proposal is meant to abstract those iterations in convenient functional constructs by adding method call iterators and iterator functions.
  
 ==== Current usage patterns ==== ==== Current usage patterns ====
-=== UC-1 ===+=== UC-1: Iterating over an aggregation of objects ===
 <code php> <code php>
 foreach ($list as $element) { foreach ($list as $element) {
-    $element->method();    array $params = array(), +    $element->method();
-    array $condition_params = array(), +
-    bool $collect = true, +
-    bool $finite = true+
 } }
 </code> </code>
  
-=== UC-2 ===+=== UC-2: Iterating over an aggregation of objects and keeping the results ===
 <code php> <code php>
 $result = array(); $result = array();
Line 22: Line 20:
 </code> </code>
  
-=== UC-3 ===+=== UC-3: Iterating over an aggregation of objects and conditionally executing a method ===
 <code php> <code php>
 foreach ($list as $element) { foreach ($list as $element) {
Line 31: Line 29:
 </code> </code>
  
-=== UC-4 ===+=== UC-4: Iterating over an aggregation of objects and conditionally executing a method while keeping the results ===
 <code php> <code php>
 $result = array(); $result = array();
Line 41: Line 39:
 </code> </code>
  
-==== Extracted functions ==== 
-=== spl_iterator_element_call() === 
-<code php> 
-array|bool spl_iterator_element_call( 
-    array|Iterator $iterator, 
-    string|array $methods, 
-    array $params = array(), 
-    bool $collect = true 
-) 
-</code> 
  
-Iterating over a list of objects, call method "testMethod" and get the list of results: 
-<code php> 
-$result = spl_iterator_element_call($list, 'testMethod'); 
-</code> 
  
-Iterating over a list of objects, call methods "testMethod" (with param "test") and "otherTestMethod" (with param "other") and get the list of results:+==== Required functionality ==== 
 +Extracting the required functionality to leads to the following required additions: 
 +  * [[rfc:functional-elements:call-iterator|class CallIterator]]: a simple iterator which walks over a traversable list (including arrays) and calls a specified method. The CallIterator might optionally gather the results 
 +  * [[rfc::functional-elements:is-traversable|function is_traversable()]]: Returns true if a list is traversable (Iterator/IteratorAggregate/array) 
 +  * [[rfc:functional-elements:iterate|function iterate()]]: Walks over a traversable list and does nothing 
 +  * function iterator_true(): Walks over a traversable list and assumes every element to be bool(true) 
 +  * function iterator_false()Walks over a traversable list and assumes every element to be bool(false) 
 + 
 +==== Modified use cases ==== 
 +=== UC-1: Iterating over an aggregation of objects ===
 <code php> <code php>
-$result = spl_iterator_element_call($list, array('testMethod', 'otherTestMethod'), array('test', 'other'));+iterate(new CallIterator($list, 'method'));
 </code> </code>
  
-Iterating over a list of objects, call method 'testMethod' (with param "test1" and "test2") and "otherTestMethod" (with param "other1" and "other2") and get the list of results:+=== UC-2: Iterating over an aggregation of objects and keeping the results ===
 <code php> <code php>
-$result = spl_iterator_element_call($list, array('testMethod', 'otherTestMethod'), array(array('test1', 'test2'), array('other1', 'other2')));+$result = iterate(new CallIterator($list, 'method'), true);
 </code> </code>
  
-=== spl_iterator_element_call_conditional() === 
-<code php> 
-array|mixed spl_iterator_element_call_conditional( 
-    array|Iterator $iterator, 
-    string|array $methods, 
-    string|array $conditions, 
-    array $params = array(), 
-    array $condition_params = array(), 
-    bool $collect = true, 
-    bool $finite = true 
-) 
-</code> 
rfc/functional-elements.1204769740.txt.gz · Last modified: 2017/09/22 13:28 (external edit)