diff --git a/src/CSVelte/Collection/TabularCollection.php b/src/CSVelte/Collection/TabularCollection.php index fb0855c..68fcdbb 100644 --- a/src/CSVelte/Collection/TabularCollection.php +++ b/src/CSVelte/Collection/TabularCollection.php @@ -46,4 +46,13 @@ protected function isConsistentDataStructure($data) return static::isTabular($data); } + + public function map(callable $callback) + { + $ret = []; + foreach ($this->data as $key => $row) { + $ret[$key] = $callback(static::factory($row)); + } + return static::factory($ret); + } } \ No newline at end of file diff --git a/tests/CSVelte/Collection/AbstractCollectionTest.php b/tests/CSVelte/Collection/AbstractCollectionTest.php index 26091dd..1e4d800 100644 --- a/tests/CSVelte/Collection/AbstractCollectionTest.php +++ b/tests/CSVelte/Collection/AbstractCollectionTest.php @@ -14,6 +14,7 @@ namespace CSVelteTest\Collection; use CSVelte\Collection\MultiCollection; +use CSVelte\Collection\TabularCollection; use CSVelteTest\UnitTestCase; use Faker; @@ -44,5 +45,34 @@ public function setUp() $this->testdata[MultiCollection::class]['words'][] = $faker->words; $this->testdata[MultiCollection::class]['userAgent'][] = $faker->userAgent; } + $this->testdata[TabularCollection::class] = [ + 'user' => [], + 'profile' => [] + ]; + for($t = 1; $t <= 5; $t++) { + $created = $faker->dateTimeThisYear->format('YmdHis'); + $profile_id = $t + 125; + $this->testdata[TabularCollection::class]['user'][] = [ + 'id' => $t, + 'profile_id' => $profile_id, + 'email' => $faker->email, + 'password' => sha1($faker->asciify('**********')), + 'role' => $faker->randomElement(['user','admin','user','user','user','user','user','moderator','moderator']), + 'is_active' => $faker->boolean, + 'created' => $created, + 'modified' => $created + ]; + $this->testdata[TabularCollection::class]['profile'][] = [ + 'id' => $profile_id, + 'address' => $faker->streetAddress, + 'city' => $faker->city, + 'state' => $faker->stateAbbr, + 'zipcode' => $faker->postcode, + 'phone' => $faker->phoneNumber, + 'bio' => $faker->paragraph, + 'created' => $created, + 'modified' => $created + ]; + } } } \ No newline at end of file diff --git a/tests/CSVelte/Collection/MultiCollectionTest.php b/tests/CSVelte/Collection/MultiCollectionTest.php index 2f43caa..9ddf5e9 100644 --- a/tests/CSVelte/Collection/MultiCollectionTest.php +++ b/tests/CSVelte/Collection/MultiCollectionTest.php @@ -121,4 +121,13 @@ public function testMultiContainsSearchesThroughoutAllDimensions() // $this->assertFalse($coll->contains($func), 'names'); } + public function testMapRunsCallbackAgainstEachItem() + { + $coll = Collection::factory($this->testdata[MultiCollection::class]['addresses']); + $coll2 = $coll->map(function($val) { + return strlen($val); + }); + $this->assertEquals([18,22,19,21,20,30,27,14,32,24], $coll2->toArray()); + } + } \ No newline at end of file diff --git a/tests/CSVelte/Collection/TabularCollectionTest.php b/tests/CSVelte/Collection/TabularCollectionTest.php new file mode 100644 index 0000000..75236fe --- /dev/null +++ b/tests/CSVelte/Collection/TabularCollectionTest.php @@ -0,0 +1,37 @@ +testdata[TabularCollection::class]['user']); + $this->assertInstanceOf(TabularCollection::class, $coll); + $coll2 = Collection::factory($this->testdata[TabularCollection::class]['profile']); + $this->assertInstanceOf(TabularCollection::class, $coll2); + } + + public function testMapTabularCollection() + { + $coll = Collection::factory($this->testdata[TabularCollection::class]['user']); + $func = function($row) { + $this->assertInstanceOf(AbstractCollection::class, $row); + return $row->get('email'); + }; + $newcoll = $coll->map($func->bindTo($this)); + $this->assertEquals([ + 'ohauck@bahringer.info', + 'larry.emard@pacocha.com', + 'jaylin.mueller@yahoo.com', + 'gfriesen@hotmail.com', + 'verla.ohara@dibbert.com' + ], $newcoll->toArray()); + } +} \ No newline at end of file