diff --git a/src/Validation/Validation.php b/src/Validation/Validation.php index 2b7fc27..0274d74 100644 --- a/src/Validation/Validation.php +++ b/src/Validation/Validation.php @@ -27,64 +27,13 @@ public static function setInstance(): void public static function getInstance(): self { + self::$instance->rules = []; + self::$instance->errors = []; + self::$instance->data = []; + return self::$instance; } - protected function existsData(array $array, string|int $key): bool - { - return array_key_exists($key, $array); - } - - protected function collapseData(array $array): array - { - $results = []; - - foreach ($array as $values) { - $results[] = $values; - } - - return array_merge([], ...$results); - } - - protected function getData(mixed $target, string|array|int|null $key, mixed $default = null): mixed - { - if (is_null($key)) { - return $target; - } - - $key = is_array($key) ? $key : explode('.', $key); - - foreach ($key as $i => $segment) { - unset($key[$i]); - - if (is_null($segment)) { - return $target; - } - - if ($segment === '*') { - if (! is_array($target)) { - return value($default); - } - - $result = []; - - foreach ($target as $item) { - $result[] = $this->getData($item, $key); - } - - return in_array('*', $key) ? $this->collapseData($result) : $result; - } - - if (is_array($target) && $this->existsData($target, $segment)) { - $target = $target[$segment]; - } else { - return value($default); - } - } - - return $target; - } - public function validate(array $data , array $rules): self { $this->data = $data; @@ -156,4 +105,59 @@ public function firstError(): array { return $this->errors[0]; } + + protected function existsData(array $array, string|int $key): bool + { + return array_key_exists($key, $array); + } + + protected function collapseData(array $array): array + { + $results = []; + + foreach ($array as $values) { + $results[] = $values; + } + + return array_merge([], ...$results); + } + + protected function getData(mixed $target, string|array|int|null $key, mixed $default = null): mixed + { + if (is_null($key)) { + return $target; + } + + $key = is_array($key) ? $key : explode('.', $key); + + foreach ($key as $i => $segment) { + unset($key[$i]); + + if (is_null($segment)) { + return $target; + } + + if ($segment === '*') { + if (! is_array($target)) { + return value($default); + } + + $result = []; + + foreach ($target as $item) { + $result[] = $this->getData($item, $key); + } + + return in_array('*', $key) ? $this->collapseData($result) : $result; + } + + if (is_array($target) && $this->existsData($target, $segment)) { + $target = $target[$segment]; + } else { + return value($default); + } + } + + return $target; + } } \ No newline at end of file