Skip to content

Commit

Permalink
Merge pull request #13 from citrus-framework/add_map_method
Browse files Browse the repository at this point in the history
mapに指定メソッドの実行処理を追加する
  • Loading branch information
take64 authored Aug 27, 2020
2 parents 83bc06e + 6ca2777 commit e08877b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,27 @@ public function map(callable $callable): self



/**
* 指定メソッドを実行した適用した内容を積んで返却する
*
* @param string $method_name
* @return $this
*/
public function mapExecMethod(string $method_name): self
{
$this->source = Scanner::map($this->source, function ($vl) use ($method_name) {
// メソッドが存在する場合に実行する
if (true === method_exists($vl, $method_name))
{
$vl->$method_name();
}
return $vl;
});
return $this;
}



/**
* callable関数を適用した内容を積んで返却する
* keyを指定する
Expand Down
35 changes: 35 additions & 0 deletions tests/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,31 @@ public function map_データ編集して配列生成()



/**
* @test
*/
public function mapExecMethod_メソッド実行して配列生成()
{
$values = [
new CollectionObj(),
new CollectionObj(),
new CollectionObj(),
new CollectionObj(),
];

// 全部1を足す
$expected = [
(new CollectionObj())->add(),
(new CollectionObj())->add(),
(new CollectionObj())->add(),
(new CollectionObj())->add(),
];
// 検算
$this->assertEquals($expected, Collection::stream($values)->mapExecMethod('add')->toList());
}



/**
* @test
*/
Expand Down Expand Up @@ -434,3 +459,13 @@ public function one_一件だけ取得()
$this->assertSame($expected, Collection::stream($values)->one());
}
}

class CollectionObj
{
public $count = 0;
public function add()
{
$this->count++;
return $this;
}
}

0 comments on commit e08877b

Please sign in to comment.