php-gdlabel/src/Element.php

94 lines
1.9 KiB
PHP

<?php
namespace NoccyLabs\GdLabel;
abstract class Element
{
/** @var Element[] $children */
protected $children = [];
/** @var array $props */
protected $props = [];
/**
* Add a new child
*
* @param Element $element
*/
public function addChild(Element $element)
{
$this->children[] = $element;
}
/**
* Get all children
*
* @return Element[] Child elements
*/
public function getChildren():iterable
{
return $this->children;
}
/**
* Render the element
*
* @param gd $canvas
* @param int $x
* @param int $y
* @param int $width
* @param int $height
* @param array $params
*/
public function drawElement($canvas, int $x, int $y, int $width, int $height, array $params)
{
$this->draw($canvas, $x, $y, $width, $height, $params);
}
protected function draw($canvas, int $x, int $y, int $width, int $height, array $params)
{
}
protected function rgb($color)
{
if (is_integer($color)) {
return $color;
} elseif (preg_match('/^#[0-9A-F]{6}$/i', $color)) {
[$r,$g,$b] = \sscanf($color, "#%02x%02x%02x");
return $r | $g << 8 | $b << 16;
} else {
return null;
}
}
public function setProp(string $key, $value)
{
$this->props[$key] = $value;
}
public function getProp(string $key, $default=null)
{
return array_key_exists($key, $this->props)
?$this->props[$key]
:$default
;
}
public function getPropRgb(string $key, $default=null)
{
$color = $this->getProp($key, $default);
return $this->rgb($color);
}
public function getProps():iterable
{
return $this->props;
}
protected function warning($msg)
{
defined("STDERR") && fprintf(STDERR, "%s\n", rtrim($msg));
}
}