<?php
namespace Fifa\ConnectId\Sdk\Util\Builder\Error;

/**
 * Class CompositeErrorBuilder
 * @package Fifa\ConnectId\Sdk\Util\Builder\Error
 */
class CompositeErrorBuilder implements CompositeErrorBuilderInterface
{
    /**
     * @var null|string
     */
    private $message = null;

    /**
     * Adds a message to collection
     *
     * @param boolean $shouldAdd
     * @param string $message
     * @return CompositeErrorBuilderInterface
     */
    public function addMessageForCase(bool $shouldAdd, string $message): CompositeErrorBuilderInterface
    {
        if (!$shouldAdd || strlen($message) === 0) {
            return $this;
        }

        if (is_null($this->message)) {
            $this->message = $message;
        }
        else {
            $this->message .= "\n" . $message;
        }

        return $this;
    }

    /**
     * Returns full error message
     *
     * @return string|null
     */
    public function build(): ?string
    {
        return $this->message;
    }
}