|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Flow\ETL\Row\Factory; |
| 6 | + |
| 7 | +use Flow\ETL\Row\Entry\Type\Uuid; |
| 8 | + |
| 9 | +final class StringTypeChecker |
| 10 | +{ |
| 11 | + private readonly string $string; |
| 12 | + |
| 13 | + public function __construct(string $string) |
| 14 | + { |
| 15 | + $this->string = \trim($string); |
| 16 | + } |
| 17 | + |
| 18 | + public function isBoolean() : bool |
| 19 | + { |
| 20 | + if ($this->string === '') { |
| 21 | + return false; |
| 22 | + } |
| 23 | + |
| 24 | + return \in_array(\strtolower($this->string), ['true', 'false'], true); |
| 25 | + } |
| 26 | + |
| 27 | + public function isDateTime() : bool |
| 28 | + { |
| 29 | + if ($this->string === '') { |
| 30 | + return false; |
| 31 | + } |
| 32 | + |
| 33 | + $dateParts = \date_parse($this->string); |
| 34 | + |
| 35 | + if ($dateParts['error_count'] > 0) { |
| 36 | + return false; |
| 37 | + } |
| 38 | + |
| 39 | + if ($dateParts['year'] === false) { |
| 40 | + return false; |
| 41 | + } |
| 42 | + |
| 43 | + if ($dateParts['month'] === false) { |
| 44 | + return false; |
| 45 | + } |
| 46 | + |
| 47 | + if ($dateParts['day'] === false) { |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + return true; |
| 52 | + } |
| 53 | + |
| 54 | + public function isFloat() : bool |
| 55 | + { |
| 56 | + if ($this->string === '') { |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + return \is_numeric($this->string) && \str_contains($this->string, '.'); |
| 61 | + } |
| 62 | + |
| 63 | + public function isInteger() : bool |
| 64 | + { |
| 65 | + if ($this->string === '') { |
| 66 | + return false; |
| 67 | + } |
| 68 | + |
| 69 | + if (\is_numeric($this->string)) { |
| 70 | + return (string) ((int) $this->string) === $this->string; |
| 71 | + } |
| 72 | + |
| 73 | + return false; |
| 74 | + } |
| 75 | + |
| 76 | + public function isJson() : bool |
| 77 | + { |
| 78 | + if ($this->string === '') { |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + if ('{' !== $this->string[0] && '[' !== $this->string[0]) { |
| 83 | + return false; |
| 84 | + } |
| 85 | + |
| 86 | + if (\function_exists('json_validate')) { |
| 87 | + return \json_validate($this->string); |
| 88 | + } |
| 89 | + |
| 90 | + if ( |
| 91 | + (!\str_starts_with($this->string, '{') || !\str_ends_with($this->string, '}')) |
| 92 | + && (!\str_starts_with($this->string, '[') || !\str_ends_with($this->string, ']')) |
| 93 | + ) { |
| 94 | + return false; |
| 95 | + } |
| 96 | + |
| 97 | + try { |
| 98 | + return \is_array(\json_decode($this->string, true, flags: \JSON_THROW_ON_ERROR)); |
| 99 | + } catch (\Exception) { |
| 100 | + return false; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + public function isNull() : bool |
| 105 | + { |
| 106 | + return \in_array(\mb_strtolower($this->string), ['null', 'nil'], true); |
| 107 | + } |
| 108 | + |
| 109 | + public function isUuid() : bool |
| 110 | + { |
| 111 | + if ($this->string === '') { |
| 112 | + return false; |
| 113 | + } |
| 114 | + |
| 115 | + if (\strlen($this->string) !== 36) { |
| 116 | + return false; |
| 117 | + } |
| 118 | + |
| 119 | + return 0 !== \preg_match(Uuid::UUID_REGEXP, $this->string); |
| 120 | + } |
| 121 | + |
| 122 | + public function isXML() : bool |
| 123 | + { |
| 124 | + if ($this->string === '') { |
| 125 | + return false; |
| 126 | + } |
| 127 | + |
| 128 | + if ('<' !== $this->string[0]) { |
| 129 | + return false; |
| 130 | + } |
| 131 | + |
| 132 | + if (\preg_match('/<(.+?)>(.+?)<\/(.+?)>/', $this->string) === 1) { |
| 133 | + try { |
| 134 | + \libxml_use_internal_errors(true); |
| 135 | + |
| 136 | + $doc = new \DOMDocument(); |
| 137 | + $result = $doc->loadXML($this->string); |
| 138 | + \libxml_clear_errors(); // Clear any errors if needed |
| 139 | + \libxml_use_internal_errors(false); // Restore standard error handling |
| 140 | + |
| 141 | + /** @psalm-suppress RedundantCastGivenDocblockType */ |
| 142 | + return (bool) $result; |
| 143 | + } catch (\Exception) { |
| 144 | + \libxml_clear_errors(); // Clear any errors if needed |
| 145 | + \libxml_use_internal_errors(false); // Restore standard error handling |
| 146 | + |
| 147 | + return false; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + return false; |
| 152 | + } |
| 153 | + |
| 154 | + public function value() : string |
| 155 | + { |
| 156 | + return $this->string; |
| 157 | + } |
| 158 | +} |
0 commit comments