From 49cc5beccc4486f69350112ff94ebaebe3b6a63c Mon Sep 17 00:00:00 2001 From: Elizabeth Danzberger Date: Fri, 12 Jul 2024 14:27:24 -0400 Subject: [PATCH] fix: `Field` and `FieldType` implementation Signed-off-by: Elizabeth Danzberger --- lib/public/Files/Template/Field.php | 34 +++++++++++++++++++++---- lib/public/Files/Template/FieldType.php | 6 ++--- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/lib/public/Files/Template/Field.php b/lib/public/Files/Template/Field.php index 143ea95c0ef..9437563cce8 100644 --- a/lib/public/Files/Template/Field.php +++ b/lib/public/Files/Template/Field.php @@ -5,14 +5,38 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ -namespace OCA\Richdocuments\Template; +namespace OCP\Files\Template; -class Field { - private FieldType $type; +class Field implements \JsonSerializable { private int $index; private string $content; + private FieldType $type; + private ?int $id; + private ?string $tag; - public function __construct(FieldType $type) { - $this->type = $type; + public function __construct($index, $content, $type, $id = null, $tag = null) { + $this->index = $index; + $this->id = $id; + $this->tag = $tag; + + // TODO: Sanitize content + $this->content = $content; + + if ($type instanceof FieldType) { + $this->type = $type; + } else { + // TODO: Throw a proper enum with descriptive message + $this->type = FieldType::tryFrom($type) ?? throw new \Exception(); + } + } + + public function jsonSerialize(): array { + return [ + "index" => $this->index, + "content" => $this->content, + "type" => $this->type->value, + "id" => $this->id, + "tag" => $this->tag, + ]; } } diff --git a/lib/public/Files/Template/FieldType.php b/lib/public/Files/Template/FieldType.php index e5e0c176d99..cb9eccae14c 100644 --- a/lib/public/Files/Template/FieldType.php +++ b/lib/public/Files/Template/FieldType.php @@ -5,8 +5,8 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ -namespace OCA\Richdocuments\Template; +namespace OCP\Files\Template; -enum FieldType { - case PlainText; +enum FieldType: string { + case PlainText = "plain-text"; }