From d61de8c3834499087054203ca3391ab9a74ebe4c Mon Sep 17 00:00:00 2001 From: Rark7040 Date: Tue, 1 Feb 2022 14:49:42 +0900 Subject: [PATCH] add lib --- src/brokiem/snpc/libs/EasyUI/Form.php | 47 ++++++++++ .../snpc/libs/EasyUI/element/Button.php | 53 +++++++++++ .../snpc/libs/EasyUI/element/Dropdown.php | 25 +++++ .../snpc/libs/EasyUI/element/Element.php | 53 +++++++++++ .../snpc/libs/EasyUI/element/Input.php | 47 ++++++++++ .../snpc/libs/EasyUI/element/Label.php | 30 ++++++ .../snpc/libs/EasyUI/element/ModalOption.php | 31 +++++++ .../snpc/libs/EasyUI/element/Option.php | 34 +++++++ .../snpc/libs/EasyUI/element/Selector.php | 78 ++++++++++++++++ .../snpc/libs/EasyUI/element/Slider.php | 83 +++++++++++++++++ .../snpc/libs/EasyUI/element/StepSlider.php | 25 +++++ .../snpc/libs/EasyUI/element/Toggle.php | 47 ++++++++++ .../snpc/libs/EasyUI/icon/ButtonIcon.php | 41 +++++++++ .../snpc/libs/EasyUI/utils/Closable.php | 24 +++++ .../snpc/libs/EasyUI/utils/CloseListener.php | 37 ++++++++ .../snpc/libs/EasyUI/utils/FormResponse.php | 64 +++++++++++++ .../snpc/libs/EasyUI/utils/SubmitListener.php | 36 ++++++++ .../snpc/libs/EasyUI/utils/Submittable.php | 24 +++++ .../snpc/libs/EasyUI/variant/CustomForm.php | 91 +++++++++++++++++++ .../snpc/libs/EasyUI/variant/ModalForm.php | 84 +++++++++++++++++ .../snpc/libs/EasyUI/variant/SimpleForm.php | 71 +++++++++++++++ 21 files changed, 1025 insertions(+) create mode 100644 src/brokiem/snpc/libs/EasyUI/Form.php create mode 100644 src/brokiem/snpc/libs/EasyUI/element/Button.php create mode 100644 src/brokiem/snpc/libs/EasyUI/element/Dropdown.php create mode 100644 src/brokiem/snpc/libs/EasyUI/element/Element.php create mode 100644 src/brokiem/snpc/libs/EasyUI/element/Input.php create mode 100644 src/brokiem/snpc/libs/EasyUI/element/Label.php create mode 100644 src/brokiem/snpc/libs/EasyUI/element/ModalOption.php create mode 100644 src/brokiem/snpc/libs/EasyUI/element/Option.php create mode 100644 src/brokiem/snpc/libs/EasyUI/element/Selector.php create mode 100644 src/brokiem/snpc/libs/EasyUI/element/Slider.php create mode 100644 src/brokiem/snpc/libs/EasyUI/element/StepSlider.php create mode 100644 src/brokiem/snpc/libs/EasyUI/element/Toggle.php create mode 100644 src/brokiem/snpc/libs/EasyUI/icon/ButtonIcon.php create mode 100644 src/brokiem/snpc/libs/EasyUI/utils/Closable.php create mode 100644 src/brokiem/snpc/libs/EasyUI/utils/CloseListener.php create mode 100644 src/brokiem/snpc/libs/EasyUI/utils/FormResponse.php create mode 100644 src/brokiem/snpc/libs/EasyUI/utils/SubmitListener.php create mode 100644 src/brokiem/snpc/libs/EasyUI/utils/Submittable.php create mode 100644 src/brokiem/snpc/libs/EasyUI/variant/CustomForm.php create mode 100644 src/brokiem/snpc/libs/EasyUI/variant/ModalForm.php create mode 100644 src/brokiem/snpc/libs/EasyUI/variant/SimpleForm.php diff --git a/src/brokiem/snpc/libs/EasyUI/Form.php b/src/brokiem/snpc/libs/EasyUI/Form.php new file mode 100644 index 0000000..e5df5ad --- /dev/null +++ b/src/brokiem/snpc/libs/EasyUI/Form.php @@ -0,0 +1,47 @@ +title = $title; + $this->onCreation(); + } + + public function getTitle(): string { + return $this->title; + } + + public function setTitle(string $title): void { + $this->title = $title; + } + + abstract protected function getType(): string; + + abstract protected function serializeBody(): array; + + public function jsonSerialize(): array { + $body = $this->serializeBody(); + $body["title"] = $this->title; + $body["type"] = $this->getType(); + return $body; + } + + protected function onCreation(): void {} +} \ No newline at end of file diff --git a/src/brokiem/snpc/libs/EasyUI/element/Button.php b/src/brokiem/snpc/libs/EasyUI/element/Button.php new file mode 100644 index 0000000..183edbb --- /dev/null +++ b/src/brokiem/snpc/libs/EasyUI/element/Button.php @@ -0,0 +1,53 @@ +text = $text; + $this->icon = $icon; + $this->setSubmitListener($listener); + } + + public function hasIcon(): bool { + return $this->icon !== null; + } + + public function getIcon(): ?ButtonIcon { + return $this->icon; + } + + public function setIcon(?ButtonIcon $icon): void { + $this->icon = $icon; + } + + public function jsonSerialize(): array { + $data = [ + "text" => $this->text + ]; + + if($this->hasIcon()) { + $data["image"] = $this->icon->jsonSerialize(); + } + + return $data; + } +} diff --git a/src/brokiem/snpc/libs/EasyUI/element/Dropdown.php b/src/brokiem/snpc/libs/EasyUI/element/Dropdown.php new file mode 100644 index 0000000..5e8199f --- /dev/null +++ b/src/brokiem/snpc/libs/EasyUI/element/Dropdown.php @@ -0,0 +1,25 @@ + $this->getOptionsTexts(), + "default" => $this->getDefaultIndex() + ]; + } +} \ No newline at end of file diff --git a/src/brokiem/snpc/libs/EasyUI/element/Element.php b/src/brokiem/snpc/libs/EasyUI/element/Element.php new file mode 100644 index 0000000..355f7fc --- /dev/null +++ b/src/brokiem/snpc/libs/EasyUI/element/Element.php @@ -0,0 +1,53 @@ +headerText = $headerText; + } + + public function getHeaderText(): ?string { + return $this->headerText; + } + + public function setHeaderText(?string $headerText): void { + $this->headerText = $headerText; + } + + abstract public function getType(): string; + + abstract public function serializeBody(): array; + + abstract public function assignResult($result): void; + + public function jsonSerialize(): array { + return array_merge($this->serializeBody(), [ + "type" => $this->getType(), + "text" => $this->headerText ?? "" + ]); + } +} \ No newline at end of file diff --git a/src/brokiem/snpc/libs/EasyUI/element/Input.php b/src/brokiem/snpc/libs/EasyUI/element/Input.php new file mode 100644 index 0000000..71fde08 --- /dev/null +++ b/src/brokiem/snpc/libs/EasyUI/element/Input.php @@ -0,0 +1,47 @@ +defaultText = $defaultText; + $this->placeholder = $placeholder; + parent::__construct($headerText); + } + + /** + * @return string|null + */ + public function getSubmittedText(): ?string { + return $this->submittedText; + } + + public function getType(): string { + return Element::TYPE_INPUT; + } + + public function assignResult($result): void { + $this->submittedText = $result; + } + + public function serializeBody(): array { + return [ + "default" => $this->defaultText ?? "", + "placeholder" => $this->placeholder ?? "" + ]; + } +} \ No newline at end of file diff --git a/src/brokiem/snpc/libs/EasyUI/element/Label.php b/src/brokiem/snpc/libs/EasyUI/element/Label.php new file mode 100644 index 0000000..dd1839f --- /dev/null +++ b/src/brokiem/snpc/libs/EasyUI/element/Label.php @@ -0,0 +1,30 @@ +text = $text; + } + + public function getText(): string { + return $this->text; + } + + public function setText(string $text): void { + $this->text = $text; + } +} \ No newline at end of file diff --git a/src/brokiem/snpc/libs/EasyUI/element/Option.php b/src/brokiem/snpc/libs/EasyUI/element/Option.php new file mode 100644 index 0000000..9991a0e --- /dev/null +++ b/src/brokiem/snpc/libs/EasyUI/element/Option.php @@ -0,0 +1,34 @@ +id = $id; + $this->text = $text; + } + + public function getId(): string { + return $this->id; + } + + public function getText(): string { + return $this->text; + } + + public function setText(string $text): void { + $this->text = $text; + } +} \ No newline at end of file diff --git a/src/brokiem/snpc/libs/EasyUI/element/Selector.php b/src/brokiem/snpc/libs/EasyUI/element/Selector.php new file mode 100644 index 0000000..9de45f4 --- /dev/null +++ b/src/brokiem/snpc/libs/EasyUI/element/Selector.php @@ -0,0 +1,78 @@ +submittedOptionId; + } + + public function getDefaultIndex(): int { + return $this->defaultIndex; + } + + public function setDefaultIndex(int $defaultIndex): void { + $this->defaultIndex = $defaultIndex; + } + + /** + * @return Option[] + */ + public function getOptions(): array { + return $this->options; + } + + protected function getOptionsTexts(): array { + $texts = []; + foreach($this->options as $option) { + $texts[] = $option->getText(); + } + return $texts; + } + + public function getOption(string $id): ?Option { + foreach($this->options as $option) { + if($option->getId() === $id) { + return $option; + } + } + return null; + } + + public function getOptionByIndex(int $index): ?Option { + return $this->options[$index] ?? null; + } + + public function addOption(Option $option): void { + if($this->getOption($option->getId()) !== null) { + throw new InvalidArgumentException("There's an option with that name already!"); + } + $this->options[] = $option; + } + + public function removeOption(string $id): void { + foreach($this->options as $key => $option) { + if($option->getId() === $id) { + unset($this->options[$key]); + } + } + } + + public function assignResult($result): void { + $this->submittedOptionId = $this->getOptionByIndex($result)->getId(); + } +} \ No newline at end of file diff --git a/src/brokiem/snpc/libs/EasyUI/element/Slider.php b/src/brokiem/snpc/libs/EasyUI/element/Slider.php new file mode 100644 index 0000000..ddd2003 --- /dev/null +++ b/src/brokiem/snpc/libs/EasyUI/element/Slider.php @@ -0,0 +1,83 @@ +minStep = $minStep; + $this->maxStep = $maxStep; + $this->defaultStep = $defaultStep; + $this->stepLength = $stepLength; + parent::__construct($headerText); + } + + /** + * @return float|null + */ + public function getSubmittedStep(): ?float { + return $this->submittedStep; + } + + public function getType(): string { + return Element::TYPE_SLIDER; + } + + public function getMinStep(): float { + return $this->minStep; + } + + public function setMinStep(float $minStep): void { + $this->minStep = $minStep; + } + + public function getMaxStep(): float { + return $this->maxStep; + } + + public function setMaxStep(float $maxStep): void { + $this->maxStep = $maxStep; + } + + public function getDefaultStep(): float { + return $this->defaultStep; + } + + public function setDefaultStep(float $defaultStep): void { + $this->defaultStep = $defaultStep; + } + + public function getStepLength(): float { + return $this->stepLength; + } + + public function setStepLength(float $stepLength): void { + $this->stepLength = $stepLength; + } + + public function assignResult($result): void { + $this->submittedStep = $result; + } + + public function serializeBody(): array { + return [ + "min" => $this->minStep, + "max" => $this->maxStep, + "default" => $this->defaultStep, + "step" => $this->stepLength + ]; + } +} \ No newline at end of file diff --git a/src/brokiem/snpc/libs/EasyUI/element/StepSlider.php b/src/brokiem/snpc/libs/EasyUI/element/StepSlider.php new file mode 100644 index 0000000..90b2f18 --- /dev/null +++ b/src/brokiem/snpc/libs/EasyUI/element/StepSlider.php @@ -0,0 +1,25 @@ + $this->getOptionsTexts(), + "default" => $this->getDefaultIndex() + ]; + } +} \ No newline at end of file diff --git a/src/brokiem/snpc/libs/EasyUI/element/Toggle.php b/src/brokiem/snpc/libs/EasyUI/element/Toggle.php new file mode 100644 index 0000000..017d3d2 --- /dev/null +++ b/src/brokiem/snpc/libs/EasyUI/element/Toggle.php @@ -0,0 +1,47 @@ +defaultChoice = $defaultChoice; + parent::__construct($headerText); + } + + public function getSubmittedChoice(): ?bool { + return $this->submittedChoice; + } + + public function getType(): string { + return Element::TYPE_TOGGLE; + } + + public function getDefaultChoice(): bool { + return $this->defaultChoice; + } + + public function setDefaultChoice(bool $defaultChoice): void { + $this->defaultChoice = $defaultChoice; + } + + public function assignResult($result): void { + $this->submittedChoice = $result; + } + + public function serializeBody(): array { + return [ + "default" => $this->defaultChoice + ]; + } +} \ No newline at end of file diff --git a/src/brokiem/snpc/libs/EasyUI/icon/ButtonIcon.php b/src/brokiem/snpc/libs/EasyUI/icon/ButtonIcon.php new file mode 100644 index 0000000..db4a4ab --- /dev/null +++ b/src/brokiem/snpc/libs/EasyUI/icon/ButtonIcon.php @@ -0,0 +1,41 @@ +address = $address; + $this->type = $type; + } + + public function getAddress(): string { + return $this->address; + } + + public function getType(): string { + return $this->type; + } + + public function jsonSerialize(): array { + return [ + "type" => $this->type, + "data" => $this->address + ]; + } +} \ No newline at end of file diff --git a/src/brokiem/snpc/libs/EasyUI/utils/Closable.php b/src/brokiem/snpc/libs/EasyUI/utils/Closable.php new file mode 100644 index 0000000..719d0f8 --- /dev/null +++ b/src/brokiem/snpc/libs/EasyUI/utils/Closable.php @@ -0,0 +1,24 @@ +executeCloseListener($player); + $this->onClose($player); + } + + protected function onClose(Player $player): void {} +} \ No newline at end of file diff --git a/src/brokiem/snpc/libs/EasyUI/utils/CloseListener.php b/src/brokiem/snpc/libs/EasyUI/utils/CloseListener.php new file mode 100644 index 0000000..9feadf8 --- /dev/null +++ b/src/brokiem/snpc/libs/EasyUI/utils/CloseListener.php @@ -0,0 +1,37 @@ +closeListener; + } + + public function setCloseListener(?Closure $listener): void { + if($listener !== null) { + Utils::validateCallableSignature(function(Player $player) {}, $listener); + } + $this->closeListener = $listener; + } + + public function executeCloseListener(Player $player): void { + if($this->closeListener !== null) { + ($this->closeListener)($player); + } + } +} \ No newline at end of file diff --git a/src/brokiem/snpc/libs/EasyUI/utils/FormResponse.php b/src/brokiem/snpc/libs/EasyUI/utils/FormResponse.php new file mode 100644 index 0000000..959c503 --- /dev/null +++ b/src/brokiem/snpc/libs/EasyUI/utils/FormResponse.php @@ -0,0 +1,64 @@ +elements = $elements; + } + + public function getInputSubmittedText(string $inputId): ?string { + $element = $this->getElement($inputId); + return $element === null ? null : $element->getSubmittedText(); + } + + public function getToggleSubmittedChoice(string $toggleId): ?bool { + $element = $this->getElement($toggleId); + return $element === null ? null : $element->getSubmittedChoice(); + } + + public function getSliderSubmittedStep(string $sliderId): ?float { + $element = $this->getElement($sliderId); + return $element === null ? null : $element->getSubmittedStep(); + } + + public function getStepSliderSubmittedOptionId(string $sliderId): ?string { + $element = $this->getElement($sliderId); + return $element === null ? null : $element->getSubmittedOptionId(); + } + + public function getDropdownSubmittedOptionId(string $dropdownId): ?string { + $element = $this->getElement($dropdownId); + return $element === null ? null : $element->getSubmittedOptionId(); + } + + /** + * @param string $id + * @return Element|Input|Label|Option|Slider|StepSlider|Toggle + */ + private function getElement(string $id): ?Element { + return $this->elements[$id] ?? null; + } +} diff --git a/src/brokiem/snpc/libs/EasyUI/utils/SubmitListener.php b/src/brokiem/snpc/libs/EasyUI/utils/SubmitListener.php new file mode 100644 index 0000000..4e9501e --- /dev/null +++ b/src/brokiem/snpc/libs/EasyUI/utils/SubmitListener.php @@ -0,0 +1,36 @@ +submitListener; + } + + public function setSubmitListener(?Closure $listener): void { + if($listener !== null) { + Utils::validateCallableSignature(function(Player $player) {}, $listener); + } + $this->submitListener = $listener; + } + + public function executeSubmitListener(Player $player): void { + if($this->submitListener !== null) { + ($this->submitListener)($player); + } + } +} \ No newline at end of file diff --git a/src/brokiem/snpc/libs/EasyUI/utils/Submittable.php b/src/brokiem/snpc/libs/EasyUI/utils/Submittable.php new file mode 100644 index 0000000..28f78aa --- /dev/null +++ b/src/brokiem/snpc/libs/EasyUI/utils/Submittable.php @@ -0,0 +1,24 @@ +executeSubmitListener($player); + $this->onSubmit($player); + } + + protected function onSubmit(Player $player): void {} +} \ No newline at end of file diff --git a/src/brokiem/snpc/libs/EasyUI/variant/CustomForm.php b/src/brokiem/snpc/libs/EasyUI/variant/CustomForm.php new file mode 100644 index 0000000..99414be --- /dev/null +++ b/src/brokiem/snpc/libs/EasyUI/variant/CustomForm.php @@ -0,0 +1,91 @@ +setSubmitListener($submitListener); + parent::__construct($title); + } + + protected function getType(): string { + return Form::TYPE_CUSTOM_FORM; + } + + /** + * @return Element[] + */ + public function getElements(): array { + return $this->elements; + } + + public function addElement(string $id, Element $element): void { + $this->elements[$id] = $element; + } + + public function getSubmitListener(): ?Closure { + return $this->submitListener; + } + + public function setSubmitListener(?Closure $submitListener): void { + if($submitListener !== null) { + Utils::validateCallableSignature(function(Player $player, FormResponse $response) {}, $submitListener); + } + $this->submitListener = $submitListener; + } + + public function executeSubmitListener(Player $player, FormResponse $response): void { + if($this->submitListener !== null) { + ($this->submitListener)($player, $response); + } + $this->onSubmit($player, $response); + } + + public function handleResponse(Player $player, $data): void { + if($data === null) { + $this->notifyClose($player); + } else { + $elementCopies = []; + + $index = 0; + foreach($this->elements as $id => $element) { + $copy = clone $element; + $copy->assignResult($data[$index]); + $elementCopies[$id] = $copy; + + $index++; + } + + $this->executeSubmitListener($player, new FormResponse($elementCopies)); + } + } + + protected function serializeBody(): array { + return [ + "content" => array_values($this->elements) + ]; + } + + protected function onSubmit(Player $player, FormResponse $response): void {} +} \ No newline at end of file diff --git a/src/brokiem/snpc/libs/EasyUI/variant/ModalForm.php b/src/brokiem/snpc/libs/EasyUI/variant/ModalForm.php new file mode 100644 index 0000000..0803006 --- /dev/null +++ b/src/brokiem/snpc/libs/EasyUI/variant/ModalForm.php @@ -0,0 +1,84 @@ +contentText = $contentText; + $this->acceptOption = $acceptOption ?? new ModalOption("Accept"); + $this->denyOption = $denyOption ?? new ModalOption("Deny"); + parent::__construct($title); + } + + public function getAcceptOption(): ModalOption { + return $this->acceptOption; + } + + public function getDenyOption(): ModalOption { + return $this->denyOption; + } + + public function setAcceptListener(?Closure $closure): void { + $this->acceptOption->setSubmitListener($closure); + } + + public function setAcceptText(string $text): void { + $this->acceptOption->setText($text); + } + + public function setDenyListener(?Closure $closure): void { + $this->denyOption->setSubmitListener($closure); + } + + public function setDenyText(string $text): void { + $this->denyOption->setText($text); + } + + protected function getType(): string { + return Form::TYPE_MODAL_FORM; + } + + public function handleResponse(Player $player, $data): void { + if(!is_bool($data)) { + throw new FormValidationException("$data is not a valid response"); + } + + if($data) { + $this->onAccept($player); + $this->acceptOption->notifySubmit($player); + } else { + $this->onDeny($player); + $this->denyOption->notifySubmit($player); + } + } + + protected function serializeBody(): array { + return [ + "content" => $this->contentText, + "button1" => $this->acceptOption->getText(), + "button2" => $this->denyOption->getText() + ]; + } + + protected function onAccept(Player $player): void {} + + protected function onDeny(Player $player): void {} +} \ No newline at end of file diff --git a/src/brokiem/snpc/libs/EasyUI/variant/SimpleForm.php b/src/brokiem/snpc/libs/EasyUI/variant/SimpleForm.php new file mode 100644 index 0000000..8532ea8 --- /dev/null +++ b/src/brokiem/snpc/libs/EasyUI/variant/SimpleForm.php @@ -0,0 +1,71 @@ +headerText = $headerText; + parent::__construct($title); + } + + /** + * @return Button[] + */ + public function getButtons(): array { + return $this->buttons; + } + + public function addButton(Button $button): void { + $this->buttons[] = $button; + } + + public function getHeaderText(): ?string { + return $this->headerText; + } + + public function setHeaderText(?string $headerText): void { + $this->headerText = $headerText; + } + + protected function getType(): string { + return Form::TYPE_SIMPLE_FORM; + } + + public function handleResponse(Player $player, $data): void { + if($data === null) { + $this->notifyClose($player); + } elseif(!is_int($data) or !isset($this->buttons[$data])) { + throw new FormValidationException( "Couldn't find the option $data"); + } else { + $this->buttons[$data]->notifySubmit($player); + } + } + + public function serializeBody(): array { + return [ + "buttons" => array_map(function(Button $button) { + return $button->jsonSerialize(); + }, $this->buttons), + "content" => $this->headerText ?? "" + ]; + } +} \ No newline at end of file