<?php

namespace Openguru\OpenCartFramework\Validator;

class ErrorBag
{
    private array $errors;

    public function __construct(array $errors = [])
    {
        $this->errors = $errors;
    }

    public function count(): int
    {
        return count($this->errors);
    }

    public function put(string $field, string $message): void
    {
        $this->errors[$field][] = $message;
    }

    public function first(): ?array
    {
        foreach ($this->errors as $error) {
            return $error;
        }

        return null;
    }

    public function firstOfAll(): array
    {
        $result = [];

        foreach ($this->errors as $field => $errors) {
            if (count($errors) > 0) {
                $result[$field] = $errors[0];
            }
        }

        return $result;
    }
}
