1
0

Template.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCP\Files\Template;
  8. use OCP\Files\File;
  9. /**
  10. * @since 21.0.0
  11. */
  12. final class Template implements \JsonSerializable {
  13. /** @var string */
  14. private $templateType;
  15. /** @var string */
  16. private $templateId;
  17. /** @var File */
  18. private $file;
  19. /** @var bool */
  20. private $hasPreview = false;
  21. /** @var string|null */
  22. private $previewUrl = null;
  23. /** @var list<Field> */
  24. private $fields = [];
  25. /**
  26. * @since 21.0.0
  27. */
  28. public function __construct(string $templateType, string $templateId, File $file) {
  29. $this->templateType = $templateType;
  30. $this->templateId = $templateId;
  31. $this->file = $file;
  32. }
  33. /**
  34. * @since 21.0.0
  35. */
  36. public function setCustomPreviewUrl(string $previewUrl): void {
  37. $this->previewUrl = $previewUrl;
  38. }
  39. /**
  40. * @since 21.0.0
  41. */
  42. public function setHasPreview(bool $hasPreview): void {
  43. $this->hasPreview = $hasPreview;
  44. }
  45. /**
  46. * @param list<Field> $fields
  47. * @since 30.0.0
  48. */
  49. public function setFields(array $fields): void {
  50. $this->fields = $fields;
  51. }
  52. /**
  53. * @return array{
  54. * templateType: string,
  55. * templateId: string,
  56. * basename: string,
  57. * etag: string,
  58. * fileid: int,
  59. * filename: string,
  60. * lastmod: int,
  61. * mime: string,
  62. * size: int|float,
  63. * type: string,
  64. * hasPreview: bool,
  65. * previewUrl: ?string,
  66. * fields: list<array{
  67. * index: string,
  68. * type: string,
  69. * alias: ?string,
  70. * tag: ?string,
  71. * id: ?int,
  72. * content?: string,
  73. * checked?: bool,
  74. * }>,
  75. * }
  76. * @since 21.0.0
  77. */
  78. public function jsonSerialize(): array {
  79. return [
  80. 'templateType' => $this->templateType,
  81. 'templateId' => $this->templateId,
  82. 'basename' => $this->file->getName(),
  83. 'etag' => $this->file->getEtag(),
  84. 'fileid' => $this->file->getId(),
  85. 'filename' => $this->templateId,
  86. 'lastmod' => $this->file->getMTime(),
  87. 'mime' => $this->file->getMimetype(),
  88. 'size' => $this->file->getSize(),
  89. 'type' => $this->file->getType(),
  90. 'hasPreview' => $this->hasPreview,
  91. 'previewUrl' => $this->previewUrl,
  92. 'fields' => array_map(static fn (Field $field) => $field->jsonSerialize(), $this->fields),
  93. ];
  94. }
  95. }