| Total Complexity | 14 |
| Total Lines | 70 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 13 | abstract class ValueTransform |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Converts arbitrary value into string. |
||
| 17 | * |
||
| 18 | * @param mixed $value |
||
| 19 | * |
||
| 20 | * @return string |
||
| 21 | */ |
||
| 22 | public static function toString($value) |
||
| 23 | { |
||
| 24 | if (is_string($value)) { |
||
| 25 | return $value; |
||
| 26 | } |
||
| 27 | |||
| 28 | if (is_bool($value)) { |
||
| 29 | return $value ? 'true' : 'false'; |
||
| 30 | } |
||
| 31 | |||
| 32 | if ($value instanceof DateTimeInterface) { |
||
| 33 | return $value->format(DateTime::W3C); |
||
| 34 | } |
||
| 35 | |||
| 36 | return (string) $value; |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Converts arbitrary value into DateTimeImmutable. |
||
| 41 | * |
||
| 42 | * @param mixed $value |
||
| 43 | * |
||
| 44 | * @throws Exception |
||
| 45 | * |
||
| 46 | * @return DateTimeImmutable |
||
| 47 | */ |
||
| 48 | public static function toDateTimeImmutable($value) |
||
| 49 | { |
||
| 50 | if ($value instanceof DateTimeImmutable) { |
||
| 51 | return $value; |
||
| 52 | } |
||
| 53 | |||
| 54 | if ($value instanceof DateTime) { |
||
| 55 | return DateTimeImmutable::createFromMutable($value); |
||
| 56 | } |
||
| 57 | |||
| 58 | return new DateTimeImmutable((string) $value); |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Transform the value into a boolean type. |
||
| 63 | * |
||
| 64 | * @param mixed $value The value to transform |
||
| 65 | * |
||
| 66 | * @return bool |
||
| 67 | */ |
||
| 68 | public static function toBool($value) |
||
| 83 | } |
||
| 84 | } |
||
| 85 |