<?php

namespace MightyPork\Exceptions;

use Illuminate\Contracts\Support\MessageProvider;
use Illuminate\Support\MessageBag;

class SimpleValidationException extends RuntimeException implements MessageProvider
{
    /** @var MessageBag */
    private $mb;

    /**
     * FBValidationException constructor.
     *
     * @param string|MessageProvider $key
     * @param string $message
     */
    public function __construct($key, $message = null)
    {
        if (is_null($message)) {
            $this->mb = $key->getMessageBag();
        } else {
            $mb = new MessageBag();
            $mb->add($key, $message);
            $this->mb = $mb;
        }

        $str = '';
        foreach ($this->mb->getMessages() as $key => $errors) {
            $str .= $key . ': ' . implode(', ', $errors) . "\n";
        }

        parent::__construct($str);
    }

    /**
     * Get the messages for the instance.
     *
     * @return MessageBag
     */
    public function getMessageBag()
    {
        return $this->mb;
    }
}