-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from citrus-framework/add_first
first,lastメソッドの追加
- Loading branch information
Showing
5 changed files
with
118 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @copyright Copyright 2020, CitrusCollection. All Rights Reserved. | ||
* @author take64 <take64@citrus.tk> | ||
* @license http://www.citrus.tk/ | ||
*/ | ||
|
||
namespace Citrus\Collection; | ||
|
||
/** | ||
* コレクションメソッド(取得系) | ||
*/ | ||
class Fetcher | ||
{ | ||
/** | ||
* 先頭の要素を取得 | ||
* | ||
* @param array $source | ||
* @param callable|null $callable function($value, $key) | ||
* @return mixed | ||
*/ | ||
public static function first(array $source, callable|null $callable = null): mixed | ||
{ | ||
$filtered = Filter::filter($source, $callable); | ||
$count = Measurer::count($filtered); | ||
// 無ければnull | ||
if ($count === 0) | ||
{ | ||
return null; | ||
} | ||
return array_values($filtered)[0]; | ||
} | ||
|
||
/** | ||
* 最後の要素を取得 | ||
* | ||
* @param array $source | ||
* @param callable|null $callable function($value, $key) | ||
* @return mixed | ||
*/ | ||
public static function last(array $source, callable|null $callable = null): mixed | ||
{ | ||
$filtered = Filter::filter($source, $callable); | ||
$count = Measurer::count($filtered); | ||
// 無ければnull | ||
if ($count === 0) | ||
{ | ||
return null; | ||
} | ||
return array_values($filtered)[$count - 1]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters