<?php

namespace goals\formats;

use goals\traits\goals_criteria_progress_alert_format_t;

class sms_progress_alert_format implements \JsonSerializable
{
    use goals_criteria_progress_alert_format_t;

    private string $text;

    public const MAX_LEN = 160;

    public function __construct(string $text)
    {
        $this->text = $text;
    }

    public function render(): string
    {
        $rendered = trim($this->text);
        // Enforce SMS length hard limit
        $len = \function_exists('mb_strlen') ? \mb_strlen($rendered) : \strlen($rendered);
        if ($len > self::MAX_LEN) {
            $rendered = \function_exists('mb_substr') ? \mb_substr($rendered, 0, self::MAX_LEN) : \substr($rendered, 0, self::MAX_LEN);
        }
        return $rendered;
    }
}
