<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

class Parser_rtform_message
{
    const SHARED_FORM_CODE = 'rtform_shared_form_code';

    private $messages = [
        self::SHARED_FORM_CODE => '',
    ];

    public function set_message($message, $form_code = null)
    {
        /**
         * Share last message between all rtforms
         */
        $this->messages[self::SHARED_FORM_CODE] = $message;

        if (!is_null($form_code)) {
            $this->messages[$form_code] = $message;
        }

        return $this;
    }

    public function get_message($form_code = null)
    {
        if (is_null($form_code)) {
            $form_code = self::SHARED_FORM_CODE;
        }

        return !empty($this->messages[$form_code]) ? $this->messages[$form_code] : '';
    }

    public function parse($prefix = '', $suffix = '', $form_code = null)
    {
        $message = $this->get_message($form_code);
        return empty($message) ? '' : ($prefix . $message . $suffix);
    }

}