Beispiel #1
0
 public function test_common_params()
 {
     $collection = Jam::all('test_tag')->load_fields(array(array('id' => 1, 'name' => 'red', 'slug' => 'green'), array('id' => 2, 'name' => 'orange', 'slug' => 'green'), array('id' => 3, 'name' => 'green', 'slug' => 'green')));
     $this->assertEquals(array('slug' => 'green', 'name' => NULL), Jam_Form::common_params($collection, array('slug', 'name')));
     $posts = Jam::all('test_post')->load_fields(array(array('id' => 1, 'name' => 'Big Post', 'test_blog' => array('id' => 1, 'name' => 'Green blog')), array('id' => 2, 'name' => 'Big Post', 'test_blog' => array('id' => 2, 'name' => 'Green blog'))));
     $expected = array('id' => NULL, 'name' => 'Big Post', 'test_blog' => array('id' => NULL, 'name' => 'Green blog'));
     $this->assertEquals($expected, Jam_Form::common_params($posts, array('id', 'name', 'test_blog' => array('id', 'name'))));
 }
Beispiel #2
0
 public static function common_params($collection, array $params = array())
 {
     $collection = ($collection instanceof Jam_Query_Builder_Collection or $collection instanceof Jam_Array_Model) ? $collection->as_array() : $collection;
     $common = array();
     foreach ($params as $name => $param) {
         $attribute_name = is_numeric($name) ? $param : $name;
         $param_collection = array_map(function ($item) use($attribute_name) {
             return $item->{$attribute_name};
         }, $collection);
         if (is_numeric($name)) {
             $common[$param] = array_reduce($param_collection, function ($result, $item) {
                 return Jam_Form::list_id($result) !== Jam_Form::list_id($item) ? NULL : $item;
             }, reset($param_collection));
         } else {
             $common[$name] = Jam_Form::common_params($param_collection, $param);
         }
     }
     return $common;
 }