aboutsummaryrefslogtreecommitdiff
path: root/app/Models/AttributesTrait.php
diff options
context:
space:
mode:
Diffstat (limited to 'app/Models/AttributesTrait.php')
-rw-r--r--app/Models/AttributesTrait.php40
1 files changed, 40 insertions, 0 deletions
diff --git a/app/Models/AttributesTrait.php b/app/Models/AttributesTrait.php
new file mode 100644
index 000000000..39154182b
--- /dev/null
+++ b/app/Models/AttributesTrait.php
@@ -0,0 +1,40 @@
+<?php
+declare(strict_types=1);
+
+/**
+ * Logic to work with (JSON) attributes (for entries, feeds, categories, tags...).
+ */
+trait FreshRSS_AttributesTrait {
+ /**
+ * @var array<string,mixed>
+ */
+ private array $attributes = [];
+
+ /**
+ * @phpstan-return ($key is non-empty-string ? mixed : array<string,mixed>)
+ * @return array<string,mixed>|mixed|null
+ */
+ public function attributes(string $key = '') {
+ if ($key === '') {
+ return $this->attributes;
+ } else {
+ return $this->attributes[$key] ?? null;
+ }
+ }
+
+ /** @param string|array<mixed>|bool|int|null $value Value, not HTML-encoded */
+ public function _attributes(string $key, $value = null): void {
+ if ($key == '') {
+ if (is_string($value)) {
+ $value = json_decode($value, true);
+ }
+ if (is_array($value)) {
+ $this->attributes = $value;
+ }
+ } elseif ($value === null) {
+ unset($this->attributes[$key]);
+ } else {
+ $this->attributes[$key] = $value;
+ }
+ }
+}