PluginProbe
Tableberg – Simple Gutenberg Table Block / 1.1.5
Tableberg – Simple Gutenberg Table Block v1.1.5
1.1.5 1.1.4 1.1.3 1.1.2 1.1.1 1.1.0 1.0.5 1.0.4 1.0.3 1.0.2 1.0.1 trunk 0.0.2 0.2.1 0.3.2 0.3.3 0.4.1 0.5.0 0.5.1 0.5.2 0.5.3 0.5.4 0.5.5 0.5.6 0.5.7 All 42 releases
tableberg / renderer / Attrs / StringAttr.php

StringAttr.php in Tableberg – Simple Gutenberg Table Block 1.1.5, at renderer/Attrs/StringAttr.php

98 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Tableberg\Renderer\Attrs;
4
5 class StringAttr {
6 /** @var string */
7 private $value;
8
9 /**
10 * @param string $value
11 * @param string $default
12 * @param array|null $allowed
13 */
14 public function __construct($value, $default, $allowed = null) {
15 if ($value === null || $value === '') {
16 $this->value = $default;
17 } elseif ($allowed !== null && !in_array($value, $allowed, true)) {
18 $this->value = $default;
19 } else {
20 $this->value = (string) $value;
21 }
22 }
23
24 /** @return string */
25 public function asAttr() {
26 return esc_attr($this->value);
27 }
28
29 /** @return string */
30 public function asUrl() {
31 return esc_url($this->value);
32 }
33
34 /** @return string */
35 public function asHtml() {
36 return wp_kses_post($this->value);
37 }
38
39 /** @return string */
40 public function asText() {
41 return esc_html($this->value);
42 }
43
44 /** @return bool */
45 public function isEmpty() {
46 return $this->value === '';
47 }
48
49 /** @return bool */
50 public function isNotEmpty() {
51 return $this->value !== '';
52 }
53
54 /**
55 * @param mixed $other
56 * @return bool
57 */
58 public function equals($other) {
59 return $this->value === (string) $other;
60 }
61
62 /** @return string */
63 public function dangerouslyUseRawValue() {
64 return $this->value;
65 }
66
67 /**
68 * @param array<string, mixed>|null $value
69 * @param string $default
70 * @return array<string, StringAttr|array>|null
71 */
72 public static function fromNestedArray($value, $default = '') {
73 if (!is_array($value)) {
74 return null;
75 }
76
77 $result = [];
78
79 foreach ($value as $key => $item) {
80 if ($item === null) {
81 continue;
82 }
83
84 if (is_array($item)) {
85 $nested = self::fromNestedArray($item, $default);
86 if (is_array($nested) && !empty($nested)) {
87 $result[$key] = $nested;
88 }
89 continue;
90 }
91
92 $result[$key] = new self($item, $default);
93 }
94
95 return !empty($result) ? $result : null;
96 }
97 }
98