123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- <?php
- namespace OCA\Files_External\Lib;
- class DefinitionParameter implements \JsonSerializable {
-
-
- public const UNMODIFIED_PLACEHOLDER = '__unmodified__';
-
- public const VALUE_TEXT = 0;
- public const VALUE_BOOLEAN = 1;
- public const VALUE_PASSWORD = 2;
- public const VALUE_HIDDEN = 3;
-
- public const FLAG_NONE = 0;
- public const FLAG_OPTIONAL = 1;
- public const FLAG_USER_PROVIDED = 2;
-
- private string $tooltip = '';
-
- private int $type = self::VALUE_TEXT;
-
- private int $flags = self::FLAG_NONE;
-
- public function __construct(
- private string $name,
- private string $text,
- private $defaultValue = null,
- ) {
- }
-
- public function getName(): string {
- return $this->name;
- }
-
- public function getText(): string {
- return $this->text;
- }
-
- public function getType(): int {
- return $this->type;
- }
-
- public function setType(int $type) {
- $this->type = $type;
- return $this;
- }
-
- public function getDefaultValue() {
- return $this->defaultValue;
- }
-
- public function setDefaultValue($defaultValue) {
- $this->defaultValue = $defaultValue;
- return $this;
- }
-
- public function getTypeName(): string {
- switch ($this->type) {
- case self::VALUE_BOOLEAN:
- return 'boolean';
- case self::VALUE_TEXT:
- return 'text';
- case self::VALUE_PASSWORD:
- return 'password';
- default:
- return 'unknown';
- }
- }
-
- public function getFlags(): int {
- return $this->flags;
- }
-
- public function setFlags(int $flags) {
- $this->flags = $flags;
- return $this;
- }
-
- public function setFlag(int $flag) {
- $this->flags |= $flag;
- return $this;
- }
-
- public function isFlagSet(int $flag): bool {
- return (bool)($this->flags & $flag);
- }
-
- public function getTooltip(): string {
- return $this->tooltip;
- }
-
- public function setTooltip(string $tooltip) {
- $this->tooltip = $tooltip;
- return $this;
- }
-
- public function jsonSerialize(): array {
- $result = [
- 'value' => $this->getText(),
- 'flags' => $this->getFlags(),
- 'type' => $this->getType(),
- 'tooltip' => $this->getTooltip(),
- ];
- $defaultValue = $this->getDefaultValue();
- if ($defaultValue) {
- $result['defaultValue'] = $defaultValue;
- }
- return $result;
- }
- public function isOptional(): bool {
- return $this->isFlagSet(self::FLAG_OPTIONAL) || $this->isFlagSet(self::FLAG_USER_PROVIDED);
- }
-
- public function validateValue(&$value): bool {
- switch ($this->getType()) {
- case self::VALUE_BOOLEAN:
- if (!is_bool($value)) {
- switch ($value) {
- case 'true':
- $value = true;
- break;
- case 'false':
- $value = false;
- break;
- default:
- return false;
- }
- }
- break;
- default:
- if (!$value && !$this->isOptional()) {
- return false;
- }
- break;
- }
- return true;
- }
- }
|