Skip to content

Commit 8662795

Browse files
authored
Merge pull request #5 from infinityloop-dev/attribute_support
Support for disabled and title attributes
2 parents 8d37ee6 + d2ed7c9 commit 8662795

File tree

3 files changed

+161
-15
lines changed

3 files changed

+161
-15
lines changed

src/Form/ResultObject.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
/**
4+
* This file is part of Nepttune (https://www.peldax.com)
5+
*
6+
* Copyright (c) 2018 Václav Pelíšek (info@peldax.com)
7+
*
8+
* This software consists of voluntary contributions made by many individuals
9+
* and is licensed under the MIT license. For more information, see
10+
* <https://www.peldax.com>.
11+
*/
12+
13+
declare(strict_types = 1);
14+
15+
namespace Nepttune\Form;
16+
17+
class ResultObject implements \JsonSerializable
18+
{
19+
private int $id;
20+
private string $text;
21+
private ?string $title = null;
22+
private bool $disabled;
23+
24+
public function __construct(int $id, string $text, ?string $title = null, bool $disabled = false)
25+
{
26+
$this->id = $id;
27+
$this->text = $text;
28+
$this->title = $title;
29+
$this->disabled = $disabled;
30+
}
31+
32+
public function getId() : int
33+
{
34+
return $this->id;
35+
}
36+
37+
public function getText() : string
38+
{
39+
return $this->text;
40+
}
41+
42+
public function isDisabled() : bool
43+
{
44+
return $this->disabled;
45+
}
46+
47+
public function jsonSerialize()
48+
{
49+
$toReturn = [
50+
'id' => $this->id,
51+
'text' => $this->text,
52+
];
53+
54+
if ($this->title !== null) {
55+
$toReturn['title'] = $this->title;
56+
}
57+
58+
if ($this->disabled === true) {
59+
$toReturn['disabled'] = $this->disabled;
60+
}
61+
62+
return $toReturn;
63+
}
64+
}

src/Form/ResultObjectSet.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
/**
4+
* This file is part of Nepttune (https://www.peldax.com)
5+
*
6+
* Copyright (c) 2018 Václav Pelíšek (info@peldax.com)
7+
*
8+
* This software consists of voluntary contributions made by many individuals
9+
* and is licensed under the MIT license. For more information, see
10+
* <https://www.peldax.com>.
11+
*/
12+
13+
declare(strict_types = 1);
14+
15+
namespace Nepttune\Form;
16+
17+
class ResultObjectSet extends \Infinityloop\Utils\ObjectSet implements \JsonSerializable
18+
{
19+
protected const INNER_CLASS = ResultObject::class;
20+
21+
public static function fromArray(array $data) : \Nepttune\Form\ResultObjectSet
22+
{
23+
$objectSet = [];
24+
25+
foreach ($data as $key => $value) {
26+
if ($value instanceof \Nepttune\Form\ResultObject) {
27+
$objectSet[] = $value;
28+
29+
continue;
30+
}
31+
32+
$objectSet[] = new \Nepttune\Form\ResultObject($key, $value);
33+
}
34+
35+
return new self($objectSet);
36+
}
37+
38+
public function jsonSerialize()
39+
{
40+
$toReturn = [];
41+
42+
foreach ($this as $object) {
43+
$toReturn[] = $object->jsonSerialize();
44+
}
45+
46+
return $toReturn;
47+
}
48+
49+
public function getRawData() : array
50+
{
51+
$toReturn = [];
52+
53+
foreach ($this as $object) {
54+
$toReturn[$object->getId()] = $object->getText();
55+
}
56+
57+
return $toReturn;
58+
}
59+
60+
public function getDisabled() : array
61+
{
62+
$toReturn = [];
63+
64+
foreach ($this as $object) {
65+
if ($object->isDisabled()) {
66+
$toReturn[] = $object->getId();
67+
}
68+
}
69+
70+
return $toReturn;
71+
}
72+
73+
public function current() : \Nepttune\Form\ResultObject
74+
{
75+
return parent::current();
76+
}
77+
78+
public function offsetGet($offset) : \Nepttune\Form\ResultObject
79+
{
80+
return parent::offsetGet($offset);
81+
}
82+
}

src/Form/TAjaxSelect.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ trait TAjaxSelect
2828
/** @var \Nette\Caching\Cache */
2929
private $storage;
3030

31+
private $optionsRaw;
32+
3133
public function setCallback(callable $callback): self
3234
{
3335
$this->callback = $callback;
@@ -86,17 +88,7 @@ public function signalReceived(string $signal): void
8688

8789
switch ($signal) {
8890
case self::CALLBACK_SIGNAL_NAME:
89-
$data = $this->getData($presenter->getParameter('q'));
90-
$toJson = [];
91-
92-
foreach ($data as $key => $value) {
93-
$toJson[] = [
94-
'id' => $key,
95-
'text' => $value,
96-
];
97-
}
98-
99-
$presenter->sendJson($toJson);
91+
$presenter->sendJson($this->getData($presenter->getParameter('q')));
10092
break;
10193
case self::ONCHANGE_SIGNAL_NAME:
10294
$this->fireOnchange($presenter->getParameter('s'));
@@ -109,7 +101,7 @@ public function signalReceived(string $signal): void
109101
* @param array|int|null $default
110102
* @return array
111103
*/
112-
private function getData(string $query = '', $default = null): array
104+
private function getData(string $query = '', $default = null) : \Nepttune\Form\ResultObjectSet
113105
{
114106
if ($this->callback === null) {
115107
throw new \Nette\InvalidStateException('Callback for "' . $this->getHtmlId() . '" is not set.');
@@ -126,7 +118,11 @@ private function getData(string $query = '', $default = null): array
126118

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

129-
if (!\is_array($data)) {
121+
if (\is_array($data)) {
122+
$data = \Nepttune\Form\ResultObjectSet::fromArray($data);
123+
}
124+
125+
if (!$data instanceof \Nepttune\Form\ResultObjectSet) {
130126
throw new \Nette\InvalidStateException('Callback for "' . $this->getHtmlId() . '" must return array.');
131127
}
132128

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

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

149-
parent::setItems($this->getData());
146+
$data = $this->getData();
150147
} else {
151-
parent::setItems($this->getData('', $value));
148+
$data = $this->getData('', $value);
152149
}
150+
151+
parent::setItems($data->getRawData());
152+
parent::setDisabled($data->getDisabled());
153153
}
154154

155155
private function fireOnchange($selected = null) : void

0 commit comments

Comments
 (0)