[limb-svn] r6058 - in 3.x/trunk/limb/core: src tests/cases
svn at limb-project.com
svn at limb-project.com
Tue Jul 3 15:35:41 MSD 2007
Author: serega
Date: 2007-07-03 15:35:41 +0400 (Tue, 03 Jul 2007)
New Revision: 6058
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6058
Modified:
3.x/trunk/limb/core/src/lmbCollection.class.php
3.x/trunk/limb/core/tests/cases/lmbCollectionTest.class.php
Log:
-- lmbCollection :: toFlatArray now accepts optional $key_field parameter. This parameter allows to get array with $key_field values as keys in result array
Modified: 3.x/trunk/limb/core/src/lmbCollection.class.php
===================================================================
--- 3.x/trunk/limb/core/src/lmbCollection.class.php 2007-07-03 11:31:22 UTC (rev 6057)
+++ 3.x/trunk/limb/core/src/lmbCollection.class.php 2007-07-03 11:35:41 UTC (rev 6058)
@@ -50,15 +50,21 @@
return $result;
}
- static function toFlatArray($iterator)
+ static function toFlatArray($iterator, $key_field = '')
{
$result = array();
foreach($iterator as $record)
{
+ $data = null;
if(is_object($record) && method_exists($record, 'export'))
- $result[] = $record->export();
+ $data = $record->export();
else
- $result[] = $record;
+ $data = $record;
+
+ if($key_field && isset($data[$key_field]) && ($key = $data[$key_field]))
+ $result[$key] = $data;
+ else
+ $result[] = $data;
}
return $result;
}
Modified: 3.x/trunk/limb/core/tests/cases/lmbCollectionTest.class.php
===================================================================
--- 3.x/trunk/limb/core/tests/cases/lmbCollectionTest.class.php 2007-07-03 11:31:22 UTC (rev 6057)
+++ 3.x/trunk/limb/core/tests/cases/lmbCollectionTest.class.php 2007-07-03 11:35:41 UTC (rev 6058)
@@ -1,10 +1,10 @@
<?php
-/*
- * Limb PHP Framework
- *
- * @link http://limb-project.com
- * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
- * @license LGPL http://www.gnu.org/copyleft/lesser.html
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
+ * @license LGPL http://www.gnu.org/copyleft/lesser.html
*/
lmb_require('limb/core/src/lmbCollection.class.php');
lmb_require('limb/core/src/lmbSet.class.php');
@@ -154,5 +154,25 @@
$this->assertEqual(lmbCollection :: concat($col1, $col2, $col3),
new lmbCollection(array($item1, $item2, $item3, $item4)));
}
+
+ function testToFlatArrayWithKeyField()
+ {
+ $data = array (array('x' => 'C'),
+ array('x' => 'A'),
+ array('x' => 'B'));
+
+ $iterator = new lmbCollection($data);
+
+ $arr = lmbCollection :: toFlatArray($iterator, 'x');
+ $this->assertTrue(isset($arr['A']));
+ $this->assertEqual($arr['A'], array('x' => 'A'));
+
+ $this->assertTrue(isset($arr['B']));
+ $this->assertEqual($arr['B'], array('x' => 'B'));
+
+ $this->assertTrue(isset($arr['C']));
+ $this->assertEqual($arr['C'], array('x' => 'C'));
+ }
+
}
?>
More information about the limb-svn
mailing list