| 1 |
<?php |
| 2 |
|
| 3 |
namespace Tableberg\Renderer\Text; |
| 4 |
|
| 5 |
use Tableberg\Renderer\Attrs\StringAttr; |
| 6 |
|
| 7 |
use function Tableberg\Renderer\getOrNull; |
| 8 |
|
| 9 |
class TextAttrs { |
| 10 |
/** @var StringAttr */ |
| 11 |
public $content; |
| 12 |
|
| 13 |
/** @var StringAttr */ |
| 14 |
public $align; |
| 15 |
|
| 16 |
/** @var TextStyles */ |
| 17 |
public $styles; |
| 18 |
|
| 19 |
/** |
| 20 |
* @param array<string, mixed>|null $data |
| 21 |
* @return self |
| 22 |
*/ |
| 23 |
public static function from_array($data) { |
| 24 |
$data = is_array($data) ? $data : []; |
| 25 |
$defaults = Defaults::get_defaults(); |
| 26 |
|
| 27 |
$instance = new self(); |
| 28 |
$instance->content = new StringAttr( |
| 29 |
getOrNull($data['content']), |
| 30 |
$defaults['content'] |
| 31 |
); |
| 32 |
$instance->align = new StringAttr( |
| 33 |
getOrNull($data['align']), |
| 34 |
$defaults['align'], |
| 35 |
['left', 'center', 'right'] |
| 36 |
); |
| 37 |
$instance->styles = TextStyles::from_array(getOrNull($data['styles'])); |
| 38 |
|
| 39 |
return $instance; |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
class TextStyles { |
| 44 |
/** @var StringAttr */ |
| 45 |
public $textColor; |
| 46 |
|
| 47 |
/** @var StringAttr */ |
| 48 |
public $linkColor; |
| 49 |
|
| 50 |
/** @var StringAttr */ |
| 51 |
public $backgroundColor; |
| 52 |
|
| 53 |
/** @var StringAttr */ |
| 54 |
public $fontSize; |
| 55 |
|
| 56 |
/** |
| 57 |
* @param array<string, mixed>|null $data |
| 58 |
* @return self |
| 59 |
*/ |
| 60 |
public static function from_array($data) { |
| 61 |
$data = is_array($data) ? $data : []; |
| 62 |
$defaults = Defaults::get_defaults(); |
| 63 |
$d = getOrNull($defaults['styles']); |
| 64 |
|
| 65 |
$defaultTextColor = getOrNull($d['textColor']); |
| 66 |
$defaultLinkColor = getOrNull($d['linkColor']); |
| 67 |
$defaultBackgroundColor = getOrNull($d['backgroundColor']); |
| 68 |
$defaultFontSize = getOrNull($d['fontSize']); |
| 69 |
|
| 70 |
$instance = new self(); |
| 71 |
$instance->textColor = new StringAttr( |
| 72 |
getOrNull($data['textColor']), |
| 73 |
$defaultTextColor |
| 74 |
); |
| 75 |
$instance->linkColor = new StringAttr( |
| 76 |
getOrNull($data['linkColor']), |
| 77 |
$defaultLinkColor |
| 78 |
); |
| 79 |
$instance->backgroundColor = new StringAttr( |
| 80 |
getOrNull($data['backgroundColor']), |
| 81 |
$defaultBackgroundColor |
| 82 |
); |
| 83 |
$instance->fontSize = new StringAttr( |
| 84 |
getOrNull($data['fontSize']), |
| 85 |
$defaultFontSize |
| 86 |
); |
| 87 |
|
| 88 |
return $instance; |
| 89 |
} |
| 90 |
} |
| 91 |
|