Skip to content

Commit

Permalink
Merge pull request #5 from infinityloop-dev/attribute_support
Browse files Browse the repository at this point in the history
Support for disabled and title attributes
  • Loading branch information
peldax authored Jul 22, 2020
2 parents 8d37ee6 + d2ed7c9 commit 8662795
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 15 deletions.
64 changes: 64 additions & 0 deletions src/Form/ResultObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/**
* This file is part of Nepttune (https://www.peldax.com)
*
* Copyright (c) 2018 Václav Pelíšek ([email protected])
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <https://www.peldax.com>.
*/

declare(strict_types = 1);

namespace Nepttune\Form;

class ResultObject implements \JsonSerializable
{
private int $id;
private string $text;
private ?string $title = null;
private bool $disabled;

public function __construct(int $id, string $text, ?string $title = null, bool $disabled = false)
{
$this->id = $id;
$this->text = $text;
$this->title = $title;
$this->disabled = $disabled;
}

public function getId() : int
{
return $this->id;
}

public function getText() : string
{
return $this->text;
}

public function isDisabled() : bool
{
return $this->disabled;
}

public function jsonSerialize()
{
$toReturn = [
'id' => $this->id,
'text' => $this->text,
];

if ($this->title !== null) {
$toReturn['title'] = $this->title;
}

if ($this->disabled === true) {
$toReturn['disabled'] = $this->disabled;
}

return $toReturn;
}
}
82 changes: 82 additions & 0 deletions src/Form/ResultObjectSet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

/**
* This file is part of Nepttune (https://www.peldax.com)
*
* Copyright (c) 2018 Václav Pelíšek ([email protected])
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <https://www.peldax.com>.
*/

declare(strict_types = 1);

namespace Nepttune\Form;

class ResultObjectSet extends \Infinityloop\Utils\ObjectSet implements \JsonSerializable
{
protected const INNER_CLASS = ResultObject::class;

public static function fromArray(array $data) : \Nepttune\Form\ResultObjectSet
{
$objectSet = [];

foreach ($data as $key => $value) {
if ($value instanceof \Nepttune\Form\ResultObject) {
$objectSet[] = $value;

continue;
}

$objectSet[] = new \Nepttune\Form\ResultObject($key, $value);
}

return new self($objectSet);
}

public function jsonSerialize()
{
$toReturn = [];

foreach ($this as $object) {
$toReturn[] = $object->jsonSerialize();
}

return $toReturn;
}

public function getRawData() : array
{
$toReturn = [];

foreach ($this as $object) {
$toReturn[$object->getId()] = $object->getText();
}

return $toReturn;
}

public function getDisabled() : array
{
$toReturn = [];

foreach ($this as $object) {
if ($object->isDisabled()) {
$toReturn[] = $object->getId();
}
}

return $toReturn;
}

public function current() : \Nepttune\Form\ResultObject
{
return parent::current();
}

public function offsetGet($offset) : \Nepttune\Form\ResultObject
{
return parent::offsetGet($offset);
}
}
30 changes: 15 additions & 15 deletions src/Form/TAjaxSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ trait TAjaxSelect
/** @var \Nette\Caching\Cache */
private $storage;

private $optionsRaw;

public function setCallback(callable $callback): self
{
$this->callback = $callback;
Expand Down Expand Up @@ -86,17 +88,7 @@ public function signalReceived(string $signal): void

switch ($signal) {
case self::CALLBACK_SIGNAL_NAME:
$data = $this->getData($presenter->getParameter('q'));
$toJson = [];

foreach ($data as $key => $value) {
$toJson[] = [
'id' => $key,
'text' => $value,
];
}

$presenter->sendJson($toJson);
$presenter->sendJson($this->getData($presenter->getParameter('q')));
break;
case self::ONCHANGE_SIGNAL_NAME:
$this->fireOnchange($presenter->getParameter('s'));
Expand All @@ -109,7 +101,7 @@ public function signalReceived(string $signal): void
* @param array|int|null $default
* @return array
*/
private function getData(string $query = '', $default = null): array
private function getData(string $query = '', $default = null) : \Nepttune\Form\ResultObjectSet
{
if ($this->callback === null) {
throw new \Nette\InvalidStateException('Callback for "' . $this->getHtmlId() . '" is not set.');
Expand All @@ -126,7 +118,11 @@ private function getData(string $query = '', $default = null): array

$data = \call_user_func($this->callback, $query, $default);

if (!\is_array($data)) {
if (\is_array($data)) {
$data = \Nepttune\Form\ResultObjectSet::fromArray($data);
}

if (!$data instanceof \Nepttune\Form\ResultObjectSet) {
throw new \Nette\InvalidStateException('Callback for "' . $this->getHtmlId() . '" must return array.');
}

Expand All @@ -140,16 +136,20 @@ private function getData(string $query = '', $default = null): array
private function initiateItems($value = null): void
{
$value = $value ?? $this->value;
$data = [];

if (\in_array($value, [null, '', []], true)) {
if (\count($this->items) > 0) {
return;
}

parent::setItems($this->getData());
$data = $this->getData();
} else {
parent::setItems($this->getData('', $value));
$data = $this->getData('', $value);
}

parent::setItems($data->getRawData());
parent::setDisabled($data->getDisabled());
}

private function fireOnchange($selected = null) : void
Expand Down

0 comments on commit 8662795

Please sign in to comment.