Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| Util | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |
100.00% |
1 / 1 |
| toBool | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @license MIT |
| 5 | * @author hazuki3417<hazuki3417@gmail.com> |
| 6 | * @copyright 2023 hazuki3417 all rights reserved. |
| 7 | */ |
| 8 | |
| 9 | namespace Selen\Str; |
| 10 | |
| 11 | use ValueError; |
| 12 | |
| 13 | class Util |
| 14 | { |
| 15 | /** |
| 16 | * 真偽値文字列をbool型に変換します |
| 17 | * |
| 18 | * @param string $value 真偽値文字列を渡します |
| 19 | * |
| 20 | * @return bool 変換した値を返します |
| 21 | * |
| 22 | * @throws ValueError 真偽値文字列が不正なときに発生します |
| 23 | */ |
| 24 | public static function toBool(string $value): bool |
| 25 | { |
| 26 | $allowType = ['true', 'false']; |
| 27 | |
| 28 | if (!\in_array($value, $allowType, true)) { |
| 29 | throw new ValueError("Invalid value. Expected value 'true' or 'false'"); |
| 30 | } |
| 31 | return $value === 'true'; |
| 32 | } |
| 33 | } |