<?php declare(strict_types=1);

require_once __DIR__ . '/../common/QrbException.php';

// Text attribute.
final class QrbPdfTextAttribute
{
    // key constants (public for unit tests)
    public const KEY_TEXT_COLOR  = "textColor";
    public const KEY_LINE_HEIGHT = "lineHeight";
    public const KEY_FONT_FAMILY = "fontFamily";
    public const KEY_FONT_STYLE  = "fontStyle";
    public const KEY_FONT_SIZE   = "fontSize";
    
    private int    $textColor;
    private int    $lineHeight;
    private string $fontFamily;
    private string $fontStyle;
    private int    $fontSize;
    
    // Validates the given associative array.
    //
    // Checks:
    // - Key "textColor" must exist and be numeric.
    // - Key "lineHeight" must exist and be numeric.
    // - Key "fontFamily" must exist.
    // - Key "fontStyle" must exist and have one of the values "", "B", "I" or "U".
    // - Key "fontSize" must exist and be numeric.
    public static function validate(array $textAttribute, string $section)
    {
        if (! array_key_exists(self::KEY_TEXT_COLOR, $textAttribute))
            throw new QrbException(static::class . "-  ({$section}): key '{self::KEY_TEXT_COLOR}' does not exist",
                                   "exception.textAttribute.textColorDoesNotExist",
                                   "{$section}, {self::KEY_TEXT_COLOR}");
        if (! is_numeric($textAttribute[self::KEY_TEXT_COLOR]))
            throw new QrbException(static::class . " - ({$section}): Invalid text color (not numeric), {self::KEY_TEXT_COLOR} = '{$textAttribute[self::KEY_TEXT_COLOR]}'",
                                   "exception.textAttribute.textColorNotNumeric",
                                   "{$section}, {self::KEY_TEXT_COLOR}");
        
        if (! array_key_exists(self::KEY_LINE_HEIGHT, $textAttribute))
            throw new QrbException(static::class . " - ({$section}): key '{self::KEY_LINE_HEIGHT}' does not exist",
                                   "exception.textAttribute.lineHeightDoesNotExist",
                                   "{$section}, {self::KEY_LINE_HEIGHT}");
        if (! is_numeric($textAttribute[self::KEY_LINE_HEIGHT]))
            throw new QrbException(static::class . " - ({$section}): Invalid line height (not numeric), {self::KEY_LINE_HEIGHT} = '{$textAttribute[self::KEY_LINE_HEIGHT]}'",
                                   "exception.textAttribute.lineHeightNotNumeric",
                                   "{$section}, {self::KEY_LINE_HEIGHT}");
        
        if (! array_key_exists(self::KEY_FONT_FAMILY, $textAttribute))
            throw new QrbException(static::class . " - ({$section}): key '{self::KEY_FONT_FAMILY}' does not exist",
                                   "exception.textAttribute.fontFamilyDoesNotExist",
                                   "{$section}, {self::KEY_FONT_FAMILY}");
        
        if (! array_key_exists(self::KEY_FONT_STYLE, $textAttribute))
            throw new QrbException(static::class . " - ({$section}): key '{self::KEY_FONT_STYLE}' does not exist",
                                   "exception.textAttribute.fontStyleDoesNotExist",
                                   "{$section}, {self::KEY_FONT_STYLE}");
        if  (  ($textAttribute[self::KEY_FONT_STYLE] != "")
            && ($textAttribute[self::KEY_FONT_STYLE] != "B")
            && ($textAttribute[self::KEY_FONT_STYLE] != "I")
            && ($textAttribute[self::KEY_FONT_STYLE] != "U")
            )
            throw new QrbException(static::class . " - ({$section}): Invalid font style (not one of '', 'B', 'I' or 'U'), '{self::KEY_FONT_STYLE}' = '{$textAttribute[self::KEY_FONT_STYLE]}'",
                                   "exception.textAttribute.fontStyleInvalid",
                                   "{$section}, {self::KEY_FONT_STYLE}, {$textAttribute[self::KEY_FONT_STYLE]}");
        
        if (! array_key_exists(self::KEY_FONT_SIZE, $textAttribute))
            throw new QrbException(static::class . " - ({$section}): key '{self::KEY_FONT_SIZE}' does not exist",
                                   "exception.textAttribute.fontSizeDoesNotExist",
                                   "{$section}, {self::KEY_FONT_SIZE}");
        if (! is_numeric($textAttribute[self::KEY_FONT_SIZE]))
            throw new QrbException(static::class . " - ({$section}): Invalid fontSize (not numeric), {self::KEY_FONT_SIZE} = '{$textAttribute[self::KEY_FONT_SIZE]}'",
                                   "exception.textAttribute.fontSizeNotNumeric",
                                   "{$section}, {self::KEY_FONT_SIZE}, {$textAttribute[self::KEY_FONT_SIZE]}");
    }
    
    // Creates an instance from the given associative array.
    //
    // "textColor":  color in grayscale (0..255, 0 = black).
    // "lineHeight": line height in mm.
    // "fontFamily": font family (should be installed on your server).
    // "fontStyle":  one of "" (regular), "B" (bold), "I" (italic), "U" (underline).
    // "fontSize":   font size (in points).
    public function __construct(array $textAttribute, string $section)
    {
        self::validate($textAttribute, $section);
        
        $this->textColor  = intval($textAttribute[self::KEY_TEXT_COLOR]);
        $this->lineHeight = intval($textAttribute[self::KEY_LINE_HEIGHT]);
        $this->fontFamily = $textAttribute[self::KEY_FONT_FAMILY];
        $this->fontStyle  = $textAttribute[self::KEY_FONT_STYLE];
        $this->fontSize   = intval($textAttribute[self::KEY_FONT_SIZE]);
    }
    
    public function getTextColor()  : int    { return $this->textColor;  }
    public function getLineHeight() : int    { return $this->lineHeight; }
    public function getFontFamily() : string { return $this->fontFamily; }
    public function getFontStyle()  : string { return $this->fontStyle;  }
    public function getFontSize()   : int    { return $this->fontSize;   }

    public function __toString()
    {
        return  "\n\ttextColor  = '" . $this->textColor  . "'"
              . "\n\tlineHeight = '" . $this->lineHeight . "'"
              . "\n\tfontFamily = '" . $this->fontFamily . "'"
              . "\n\tfontStyle  = '" . $this->fontStyle  . "'"
              . "\n\tfontSize   = '" . $this->fontSize   . "'"
              ;
    }
}

?>
