|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * League.Csv (https://csv.thephpleague.com) |
| 5 | + * |
| 6 | + * (c) Ignace Nyamagana Butera <nyamsprod@gmail.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace League\Csv\Schema; |
| 15 | + |
| 16 | +use ValueError; |
| 17 | + |
| 18 | +use function array_map; |
| 19 | +use function ctype_digit; |
| 20 | +use function implode; |
| 21 | +use function is_string; |
| 22 | +use function preg_match; |
| 23 | +use function strlen; |
| 24 | +use function trim; |
| 25 | + |
| 26 | +final class ClockField extends FieldEvaluator implements Field |
| 27 | +{ |
| 28 | + /** @var non-empty-string */ |
| 29 | + private readonly string $pattern; |
| 30 | + |
| 31 | + private function __construct( |
| 32 | + public readonly string $separator, |
| 33 | + public readonly ClockPrecision $clockPrecision, |
| 34 | + public readonly ClockStyle $clockStyle, |
| 35 | + float $confidenceThreshold = 0.8 |
| 36 | + ) { |
| 37 | + (1 === strlen($separator) && !ctype_digit($this->separator)) || throw new ValueError('The separator character must be a non-empty single byte string.'); |
| 38 | + |
| 39 | + parent::__construct($confidenceThreshold); |
| 40 | + |
| 41 | + $this->pattern = $this->generatePattern(); |
| 42 | + } |
| 43 | + |
| 44 | + public static function seconds(string $separator = ':', ClockStyle $clockStyle = ClockStyle::Padded, float $confidenceThreshold = 0.8): self |
| 45 | + { |
| 46 | + return new self($separator, ClockPrecision::HoursMinutesSeconds, $clockStyle, $confidenceThreshold); |
| 47 | + } |
| 48 | + |
| 49 | + public static function minutes(string $separator = ':', ClockStyle $clockStyle = ClockStyle::Padded, float $confidenceThreshold = 0.8): self |
| 50 | + { |
| 51 | + return new self($separator, ClockPrecision::HoursMinutes, $clockStyle, $confidenceThreshold); |
| 52 | + } |
| 53 | + |
| 54 | + public static function hours(string $separator = ':', ClockStyle $clockStyle = ClockStyle::Padded, float $confidenceThreshold = 0.8): self |
| 55 | + { |
| 56 | + return new self($separator, ClockPrecision::Hours, $clockStyle, $confidenceThreshold); |
| 57 | + } |
| 58 | + |
| 59 | + public function type(): FieldType |
| 60 | + { |
| 61 | + return FieldType::Time; |
| 62 | + } |
| 63 | + |
| 64 | + public function metadata(): FieldMetadata |
| 65 | + { |
| 66 | + return new FieldMetadata(); |
| 67 | + } |
| 68 | + |
| 69 | + public function name(): string |
| 70 | + { |
| 71 | + $precision = match ($this->clockPrecision) { |
| 72 | + ClockPrecision::Hours => 'hours', |
| 73 | + ClockPrecision::HoursMinutes => 'hours_minutes', |
| 74 | + ClockPrecision::HoursMinutesSeconds => 'hours_minutes_seconds', |
| 75 | + }; |
| 76 | + |
| 77 | + $style = match ($this->clockStyle) { |
| 78 | + ClockStyle::NonPadded => 'non_padded', |
| 79 | + ClockStyle::Padded => 'padded', |
| 80 | + }; |
| 81 | + |
| 82 | + return FieldType::Time->value.'(precision='.$precision.',style='.$style.',separator='.$this->separator.')'; |
| 83 | + } |
| 84 | + |
| 85 | + public function parse(mixed $value): ?string |
| 86 | + { |
| 87 | + if (!is_string($value)) { |
| 88 | + return null; |
| 89 | + } |
| 90 | + |
| 91 | + $value = trim($value); |
| 92 | + if (1 !== preg_match($this->pattern, $value, $found)) { |
| 93 | + return null; |
| 94 | + } |
| 95 | + |
| 96 | + $hour = (int) $found['hour']; |
| 97 | + $minute = (int) ($found['minute'] ?? 0); |
| 98 | + $second = (int) ($found['second'] ?? 0); |
| 99 | + |
| 100 | + return ($hour > 23 || $minute > 59 || $second > 59) |
| 101 | + ? null |
| 102 | + : $this->formatTimePart($hour) |
| 103 | + .$this->separator |
| 104 | + .$this->formatTimePart($minute) |
| 105 | + .$this->separator |
| 106 | + .$this->formatTimePart($second); |
| 107 | + } |
| 108 | + |
| 109 | + private function formatTimePart(int $value): string |
| 110 | + { |
| 111 | + return ($value < 10 ? '0' : '').$value; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * @return non-empty-string |
| 116 | + */ |
| 117 | + private function generatePattern(): string |
| 118 | + { |
| 119 | + $digit = fn () => ClockStyle::Padded === $this->clockStyle ? '\d{2}' : '\d{1,2}'; |
| 120 | + |
| 121 | + $patternParts = array_map( |
| 122 | + fn (string $part): string => "(?<{$part}>".$digit().')', |
| 123 | + match ($this->clockPrecision) { |
| 124 | + ClockPrecision::Hours => ['hour'], |
| 125 | + ClockPrecision::HoursMinutes => ['hour', 'minute'], |
| 126 | + ClockPrecision::HoursMinutesSeconds => ['hour', 'minute', 'second'], |
| 127 | + } |
| 128 | + ); |
| 129 | + |
| 130 | + return '/^'.implode($this->separator, $patternParts).'$/'; |
| 131 | + } |
| 132 | +} |
0 commit comments