aboutsummaryrefslogtreecommitdiffstats
path: root/lib/public/Files/Template/Field.php
blob: 49c6a033e5ce4483ca5c4e3682a67ef1be646269 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php

/**
 * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

namespace OCP\Files\Template;

/**
 * @since 30.0.0
 */
class Field implements \JsonSerializable {
	private string $index;
	private string $content;
	private FieldType $type;
	private ?string $alias;
	private ?int $id;
	private ?string $tag;

	/**
	 * @param string $index
	 * @param string $content
	 * @param FieldType $type
	 * @param ?string $alias
	 * @param ?int $id
	 * @param ?string $tag
	 * 
	 * @since 30.0.0
	 */
	public function __construct($index, $content, $type, $alias = null, $id = null, $tag = null) {
		$this->index = $index;
		$this->alias = $alias;
		$this->id = $id;
		$this->tag = $tag;
		$this->content = $content;

		if ($type instanceof FieldType) {
			$this->type = $type;
		} else {
			$this->type = FieldType::tryFrom($type) ?? throw new InvalidFieldTypeException();
		}
	}

	/**
	 * @return array
	 * 
	 * @since 30.0.0
	 */
	public function jsonSerialize(): array {
		return [
			"index" => $this->index,
			"content" => $this->content,
			"type" => $this->type->value,
			"alias" => $this->alias,
			"id" => $this->id,
			"tag" => $this->tag,
		];
	}
}