While I consider myself a fairly experienced PHP programmer, only
recently I've found the wonderful 'create_function' function which
creates an anonymous function that can be passed around as a variable
value.

This allowed me to write a class that helps working with arrays,
although it could be easily modified to work with any value type.

For example, you can use it to check for required input arguments:

if ( ! Enum::has($_GET, array('id','user')) ) die('Required arguments
are missing';

...or to make all strings in an array uppercase:

$array = Enum::collect($array, Enum::newFilter(' uppercase($value) '));

Personally, I love it :)

<?
/**
Enum class by Michal Tatarynowicz ([EMAIL PROTECTED])
Licenced as Public Domain

Usage:

$input = array(
   'a' => '1',
   'b' => '2',
   'c' => '2',
   'd' => '3'
);

$multiply_by_two   = Enum::newFilter(' $value*2 ');
print_r( Enum::collect($input, $multiply_by_two) );
// Array ( [a] => 2 [b] => 4 [c] => 4 [d] => 6 )

$key_is_b          = Enum::newFilter(' $key=="b" ');
print_r( Enum::find($input, $key_is_b) );
// 2

$pass_a_and_c_keys = Enum::newFilter(' in_array($key, array("a","c"))
');
print_r( Enum::filter($input, $pass_a_and_c_keys) );
// Array ( [a] => 1 [c] => 2 )

$pass_odd_values   = Enum::newFilter(' $value%2 ');
print_r( Enum::filter($input, $pass_odd_values) );
// Array ( [a] => 1 [d] => 3 )

print_r( Enum::has($input, array('z')) );
// 0

*/

class Enum {

   /** Create a new anonymous function to use as a filter */
   function newFilter ($code) {
      return create_function('$value,$key', "return $code;");
   }

   /** Return array of every element passed thru $func */
   function collect ($array, $filter) {
      $output = array();
      foreach ($array as $key=>$value) {
         $output[$key] = $filter($value, $key);
      }
      return $output;
   }

   /** Return value of first element that matches $func */
   function find ($array, $selector, $default=false) {
      foreach ($array as $key=>$value) {
         if ($selector($value, $key)) return $value;
      }
      return $default;
   }

   /** Return array of all elements for which $selector returns true */
   function filter ($array, $selector) {
      $output = array();
      foreach ($array as $key=>$value) {
         if ($selector($value, $key)) $output[$key] = $value;
      }
      return $output;
   }

   /** Returns true if array has all keys in $keys */
   function has ($array, $keys, $non_empty=false) {
      foreach ($keys as $key) {
         if (!isset($array[$key]) || ($non_empty && !$array[$key]))
return 0;
      }
      return true;
   }

};

?>


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/cake-php
-~----------~----~----~----~------~----~------~--~---

Reply via email to