Skip to content

Commit d1ddc82

Browse files
authored
Remove model support from HtmlTemplate (#2171)
1 parent 6c609cf commit d1ddc82

File tree

11 files changed

+29
-87
lines changed

11 files changed

+29
-87
lines changed

docs/template.md

-2
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,6 @@ following commands:
270270

271271
```
272272
$template = HtmlTemplate::addTo($this);
273-
274273
$template->loadFromString('Hello, {name}world{/}');
275274
```
276275

@@ -320,7 +319,6 @@ Example:
320319

321320
```
322321
$template = HtmlTemplate::addTo($this);
323-
324322
$template->loadFromString('Hello, {name}world{/}');
325323
326324
$template->set('name', 'John');

src/App.php

-1
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,6 @@ public function run(): void
715715
public function loadTemplate(string $filename)
716716
{
717717
$template = new $this->templateClass();
718-
$template->setApp($this);
719718

720719
if ((['.' => true, '/' => true, '\\' => true][substr($filename, 0, 1)] ?? false) || str_contains($filename, ':\\')) {
721720
return $template->loadFromFile($filename);

src/Form/Control/Lookup.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public function outputApiResponse(): void
180180
*
181181
* @param int|bool $limit
182182
*
183-
* @return array<int, array{value: string, title: mixed}>
183+
* @return list<array{value: string, title: mixed}>
184184
*/
185185
public function getData($limit = true): array
186186
{

src/Form/Control/ScopeBuilder.php

+2
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,8 @@ public function scopeToQuery(Scope\AbstractScope $scope, array $inputsMap = []):
606606

607607
/**
608608
* Converts a Condition to VueQueryBuilder query array.
609+
*
610+
* @return array{rule: string, operator: string, value: string|null, option: array|null}
609611
*/
610612
public function conditionToQuery(Condition $condition, array $inputsMap = []): array
611613
{

src/HtmlTemplate.php

+20-45
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
namespace Atk4\Ui;
66

7-
use Atk4\Core\AppScopeTrait;
87
use Atk4\Core\WarnDynamicPropertyTrait;
9-
use Atk4\Data\Model;
108
use Atk4\Ui\HtmlTemplate\TagTree;
119
use Atk4\Ui\HtmlTemplate\Value as HtmlValue;
1210

@@ -15,7 +13,6 @@
1513
*/
1614
class HtmlTemplate
1715
{
18-
use AppScopeTrait;
1916
use WarnDynamicPropertyTrait;
2017

2118
public const TOP_TAG = '_top';
@@ -107,10 +104,6 @@ public function cloneRegion(string $tag): self
107104
// TODO prune unreachable nodes
108105
// $template->rebuildTagsIndex();
109106

110-
if ($this->issetApp()) {
111-
$template->setApp($this->getApp());
112-
}
113-
114107
return $template;
115108
}
116109

@@ -141,31 +134,13 @@ protected function emptyTagTree(TagTree $tagTree): void
141134
*
142135
* If tag contains another tag trees, these tag trees are emptied.
143136
*
144-
* @param string|array<string, string>|Model $tag
145-
* @param ($tag is array|Model ? never : string|null) $value
137+
* @param string|array<string, string> $tag
138+
* @param ($tag is array ? never : string|null) $value
146139
*/
147140
protected function _setOrAppend($tag, string $value = null, bool $encodeHtml = true, bool $append = false, bool $throwIfNotFound = true): void
148141
{
149-
if ($tag instanceof Model && $value === null) {
150-
if (!$encodeHtml) {
151-
throw new Exception('HTML is not allowed to be dangerously set from Model');
152-
}
153-
154-
// $tag passed as model
155-
// in this case we don't throw exception if tags don't exist
156-
$uiPersistence = $this->getApp()->uiPersistence;
157-
foreach ($tag->getFields() as $k => $field) {
158-
if ($this->_hasTag($k)) {
159-
$v = $uiPersistence->typecastSaveField($field, $tag->get($k));
160-
$this->_setOrAppend($k, $v, $encodeHtml, $append);
161-
}
162-
}
163-
164-
return;
165-
}
166-
167142
// $tag passed as associative array [tag => value]
168-
if (is_array($tag) && $value === null) {
143+
if (is_array($tag) && $value === null) { // @phpstan-ignore-line
169144
if ($throwIfNotFound) {
170145
foreach ($tag as $k => $v) {
171146
if (!$this->_hasTag($k)) {
@@ -182,7 +157,7 @@ protected function _setOrAppend($tag, string $value = null, bool $encodeHtml = t
182157
}
183158

184159
if (!is_string($tag) || $tag === '') {
185-
throw (new Exception('Tag must be non-empty string'))
160+
throw (new Exception('Tag must be ' . (is_string($tag) ? 'non-empty ' : '') . 'string'))
186161
->addMoreInfo('tag', $tag)
187162
->addMoreInfo('value', $value);
188163
}
@@ -216,8 +191,8 @@ protected function _setOrAppend($tag, string $value = null, bool $encodeHtml = t
216191
* If tag is found inside template several times, all occurrences are
217192
* replaced.
218193
*
219-
* @param string|array<string, string>|Model $tag
220-
* @param ($tag is array|Model ? never : string|null) $value
194+
* @param string|array<string, string> $tag
195+
* @param ($tag is array ? never : string|null) $value
221196
*
222197
* @return $this
223198
*/
@@ -232,8 +207,8 @@ public function set($tag, string $value = null): self
232207
* Same as set(), but won't generate exception for non-existing
233208
* $tag.
234209
*
235-
* @param string|array<string, string>|Model $tag
236-
* @param ($tag is array|Model ? never : string|null) $value
210+
* @param string|array<string, string> $tag
211+
* @param ($tag is array ? never : string|null) $value
237212
*
238213
* @return $this
239214
*/
@@ -248,8 +223,8 @@ public function trySet($tag, string $value = null): self
248223
* Set value of a tag to a HTML content. The value is set without
249224
* encoding, so you must be sure to sanitize.
250225
*
251-
* @param string|array<string, string>|Model $tag
252-
* @param ($tag is array|Model ? never : string|null) $value
226+
* @param string|array<string, string> $tag
227+
* @param ($tag is array ? never : string|null) $value
253228
*
254229
* @return $this
255230
*/
@@ -264,8 +239,8 @@ public function dangerouslySetHtml($tag, string $value = null): self
264239
* See dangerouslySetHtml() but won't generate exception for non-existing
265240
* $tag.
266241
*
267-
* @param string|array<string, string>|Model $tag
268-
* @param ($tag is array|Model ? never : string|null) $value
242+
* @param string|array<string, string> $tag
243+
* @param ($tag is array ? never : string|null) $value
269244
*
270245
* @return $this
271246
*/
@@ -279,8 +254,8 @@ public function tryDangerouslySetHtml($tag, string $value = null): self
279254
/**
280255
* Add more content inside a tag.
281256
*
282-
* @param string|array<string, string>|Model $tag
283-
* @param ($tag is array|Model ? never : string|null) $value
257+
* @param string|array<string, string> $tag
258+
* @param ($tag is array ? never : string|null) $value
284259
*
285260
* @return $this
286261
*/
@@ -295,8 +270,8 @@ public function append($tag, ?string $value): self
295270
* Same as append(), but won't generate exception for non-existing
296271
* $tag.
297272
*
298-
* @param string|array<string, string>|Model $tag
299-
* @param ($tag is array|Model ? never : string|null) $value
273+
* @param string|array<string, string> $tag
274+
* @param ($tag is array ? never : string|null) $value
300275
*
301276
* @return $this
302277
*/
@@ -311,8 +286,8 @@ public function tryAppend($tag, ?string $value): self
311286
* Add more content inside a tag. The content is appended without
312287
* encoding, so you must be sure to sanitize.
313288
*
314-
* @param string|array<string, string>|Model $tag
315-
* @param ($tag is array|Model ? never : string|null) $value
289+
* @param string|array<string, string> $tag
290+
* @param ($tag is array ? never : string|null) $value
316291
*
317292
* @return $this
318293
*/
@@ -327,8 +302,8 @@ public function dangerouslyAppendHtml($tag, ?string $value): self
327302
* Same as dangerouslyAppendHtml(), but won't generate exception for non-existing
328303
* $tag.
329304
*
330-
* @param string|array<string, string>|Model $tag
331-
* @param ($tag is array|Model ? never : string|null) $value
305+
* @param string|array<string, string> $tag
306+
* @param ($tag is array ? never : string|null) $value
332307
*
333308
* @return $this
334309
*/

src/Lister.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ protected function renderView(): void
176176
*/
177177
public function renderRow(): void
178178
{
179-
$this->tRow->trySet($this->currentRow);
179+
$this->tRow->trySet($this->getApp()->uiPersistence->typecastSaveRow($this->currentRow, $this->currentRow->get()));
180180

181181
if ($this->tRow->hasTag('_title')) {
182182
$this->tRow->set('_title', $this->currentRow->getTitle());

src/Table.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,6 @@ protected function renderView(): void
399399
$this->tRowMaster->dangerouslySetHtml('cells', $this->getDataRowHtml());
400400
$this->tRowMaster->set('dataId', '{$dataId}');
401401
$this->tRow = new HtmlTemplate($this->tRowMaster->renderToHtml()); // TODO reparse should not be needed
402-
$this->tRow->setApp($this->getApp());
403402

404403
if ($this->hook(self::HOOK_BEFORE_ROW) === false) {
405404
continue;
@@ -444,7 +443,7 @@ protected function renderView(): void
444443
#[\Override]
445444
public function renderRow(): void
446445
{
447-
$this->tRow->set($this->currentRow);
446+
$this->tRow->trySet($this->getApp()->uiPersistence->typecastSaveRow($this->currentRow, $this->currentRow->get()));
448447

449448
if ($this->useHtmlTags) {
450449
// prepare row-specific HTML tags

src/Table/Column/Link.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ protected function init(): void
104104

105105
if (is_string($this->url)) {
106106
$this->url = new HtmlTemplate($this->url);
107-
$this->url->setApp($this->getApp());
108107
}
109108
if (is_string($this->page)) {
110109
$this->page = [$this->page];
@@ -145,7 +144,9 @@ public function getDataCellTemplate(Field $field = null): string
145144
public function getHtmlTags(Model $row, ?Field $field): array
146145
{
147146
if ($this->url) {
148-
return ['c_' . $this->shortName => $this->url->set($row)->renderToHtml()];
147+
$this->url->trySet($this->getApp()->uiPersistence->typecastSaveRow($row, $row->get()));
148+
149+
return ['c_' . $this->shortName => $this->url->renderToHtml()];
149150
}
150151

151152
$page = $this->page ?? [];

src/Table/Column/Multiformat.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ public function getHtmlTags(Model $row, ?Field $field): array
6666
}
6767

6868
$template = new HtmlTemplate($cellHtml);
69-
$template->setApp($this->getApp());
70-
$template->set($row);
69+
$template->trySet($this->getApp()->uiPersistence->typecastSaveRow($row, $row->get()));
7170
$template->dangerouslySetHtml($htmlTags);
7271

7372
$val = $template->renderToHtml();

src/View.php

-4
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,6 @@ protected function init(): void
237237
}
238238
}
239239

240-
if ($this->template !== null && (!$this->template->issetApp() || $this->template->getApp() !== $app)) {
241-
$this->template->setApp($app);
242-
}
243-
244240
foreach ($addLater as [$object, $region]) {
245241
$this->add($object, $region);
246242
}

tests/HtmlTemplateTest.php

-27
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Atk4\Ui\Tests;
66

77
use Atk4\Core\Phpunit\TestCase;
8-
use Atk4\Data\Model;
98
use Atk4\Ui\Exception;
109
use Atk4\Ui\HtmlTemplate;
1110

@@ -183,23 +182,6 @@ public function testParseDollarTags(): void
183182
self::assertSameTemplate('{foo}Hello{/} guys and {bar}welcome{/} here', $t);
184183
}
185184

186-
public function testSetFromEntity(): void
187-
{
188-
$model = new Model();
189-
$model->addField('foo');
190-
$model->addField('bar');
191-
$model->addField('baz');
192-
$entity = $model->createEntity();
193-
$entity->set('foo', 'Hello');
194-
$entity->set('bar', '<br>');
195-
$entity->set('baz', 'not in template');
196-
197-
$t = new HtmlTemplate('{$foo} {$bar}');
198-
$t->setApp($this->createApp());
199-
$t->set($entity);
200-
self::assertSameTemplate('{foo}Hello{/foo} {bar}&lt;br&gt;{/bar}', $t);
201-
}
202-
203185
public function testSetFromArray(): void
204186
{
205187
$t = new HtmlTemplate('{$foo} {$bar}');
@@ -231,15 +213,6 @@ public function testTagNotDefinedFromArrayException(): void
231213
}
232214
}
233215

234-
public function testSetHtmlFromEntityException(): void
235-
{
236-
$t = new HtmlTemplate();
237-
238-
$this->expectException(Exception::class);
239-
$this->expectExceptionMessage('HTML is not allowed to be dangerously set from Model');
240-
$t->dangerouslySetHtml(new Model());
241-
}
242-
243216
public function testSetEmptyTagException(): void
244217
{
245218
$t = new HtmlTemplate();

0 commit comments

Comments
 (0)