Skip to content

Commit

Permalink
View::set() can be used to set content property only (#2075)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Jun 18, 2023
1 parent 67cfdbd commit a78b335
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 71 deletions.
2 changes: 1 addition & 1 deletion demos/form-control/upload.php
Expand Up @@ -18,7 +18,7 @@
$control = $form->addControl('file', [Form\Control\Upload::class, ['accept' => ['.png', '.jpg']]]);

// $control->set('a_generated_token', 'a-file-name');
// $control->set('a_generated_token');
// $control->set('a_generated_token', null);

$img->onDelete(function (string $fileId) use ($img) {
$img->clearThumbnail();
Expand Down
5 changes: 2 additions & 3 deletions demos/layout/layouts.php
Expand Up @@ -33,7 +33,6 @@

// add buttons in toolbar
foreach ($buttons as $k => $args) {
Button::addTo($tb)
->set([$args['title'], 'iconRight' => 'down arrow'])
->on('click', $i->js()->attr('src', $app->url($args['page'])));
$button = Button::addTo($tb, [$args['title'], 'iconRight' => 'down arrow']);
$button->on('click', $i->js()->attr('src', $app->url($args['page'])));
}
4 changes: 2 additions & 2 deletions docs/template.rst
Expand Up @@ -85,7 +85,7 @@ engine directly, but you would be able to use it through views::


$v = new View('my_template.html');
$v->set('name', 'Mr. Boss');
$v->template->set('name', 'Mr. Boss');

$lister = new Lister($v, 'Content');
$lister->setModel($userlist);
Expand All @@ -96,7 +96,7 @@ The code above will work like this:

1. View will load and parse template.

2. Using $v->set('name') will set value of the tag inside template directly.
2. Using $v->template->set('name', ...) will set value of the tag inside template directly.

3. Lister will clone region 'Content' from my_template.

Expand Down
28 changes: 5 additions & 23 deletions phpstan.neon.dist
Expand Up @@ -224,13 +224,7 @@ parameters:
# TODO fix contravariance for View::set() method
-
path: 'src/Console.php'
message: '~^Parameter #1 \$fx \(Closure\(\$this\): void\) of method Atk4\\Ui\\Console::set\(\) should be contravariant with parameter \$arg1 \(mixed\) of method Atk4\\Ui\\View::set\(\)$~'
-
path: 'src/Console.php'
message: '~^Parameter #2 \$event \(bool\|string\) of method Atk4\\Ui\\Console::set\(\) should be contravariant with parameter \$arg2 \(mixed\) of method Atk4\\Ui\\View::set\(\)$~'
-
path: 'src/Form/Control.php'
message: '~^Parameter #2 \$ignore \(\*NEVER\*\) of method Atk4\\Ui\\Form\\Control::set\(\) should be compatible with parameter \$arg2 \(mixed\) of method Atk4\\Ui\\View::set\(\)$~'
message: '~^Parameter #1 \$fx \(Closure\(\$this\): void\) of method Atk4\\Ui\\Console::set\(\) should be compatible with parameter \$content \(string\) of method Atk4\\Ui\\View::set\(\)$~'
-
path: 'src/Form/Control/Calendar.php'
message: '~^Parameter #1 \$expr \(Atk4\\Ui\\Js\\JsExpressionable\) of method Atk4\\Ui\\Form\\Control\\Calendar::onChange\(\) should be contravariant with parameter \$expr \(array\{Closure\(Atk4\\Ui\\Js\\Jquery, mixed, mixed, mixed, mixed, mixed, mixed, mixed, mixed, mixed, mixed\): \(Atk4\\Ui\\Js\\JsExpressionable\|Atk4\\Ui\\View\|string\|void\)\}\|Atk4\\Ui\\Js\\JsExpressionable\|\(Closure\(Atk4\\Ui\\Js\\Jquery, mixed, mixed, mixed, mixed, mixed, mixed, mixed, mixed, mixed, mixed\): \(Atk4\\Ui\\Js\\JsExpressionable\|Atk4\\Ui\\View\|string\|void\)\)\) of method Atk4\\Ui\\Form\\Control::onChange\(\)$~'
Expand All @@ -242,31 +236,19 @@ parameters:
message: '~^Parameter #1 \$fx \(Closure\(Atk4\\Ui\\Js\\Jquery, mixed, mixed, mixed, mixed, mixed, mixed, mixed, mixed, mixed, mixed\): \(Atk4\\Ui\\Js\\JsExpressionable\|Atk4\\Ui\\View\|string\|void\)\) of method Atk4\\Ui\\JsCallback::set\(\) should be contravariant with parameter \$fx \(Closure\(mixed, mixed, mixed, mixed, mixed, mixed, mixed, mixed, mixed, mixed\): mixed\) of method Atk4\\Ui\\Callback::set\(\)$~'
-
path: 'src/Loader.php'
message: '~^Parameter #1 \$fx \(Closure\(\$this\): void\) of method Atk4\\Ui\\Loader::set\(\) should be contravariant with parameter \$arg1 \(mixed\) of method Atk4\\Ui\\View::set\(\)$~'
-
path: 'src/Loader.php'
message: '~^Parameter #2 \$ignore \(\*NEVER\*\) of method Atk4\\Ui\\Loader::set\(\) should be compatible with parameter \$arg2 \(mixed\) of method Atk4\\Ui\\View::set\(\)$~'
message: '~^Parameter #1 \$fx \(Closure\(\$this\): void\) of method Atk4\\Ui\\Loader::set\(\) should be compatible with parameter \$content \(string\) of method Atk4\\Ui\\View::set\(\)$~'
-
path: 'src/Modal.php'
message: '~^Parameter #1 \$fx \(Closure\(Atk4\\Ui\\View\): void\) of method Atk4\\Ui\\Modal::set\(\) should be contravariant with parameter \$arg1 \(mixed\) of method Atk4\\Ui\\View::set\(\)$~'
-
path: 'src/Modal.php'
message: '~^Parameter #2 \$ignore \(\*NEVER\*\) of method Atk4\\Ui\\Modal::set\(\) should be compatible with parameter \$arg2 \(mixed\) of method Atk4\\Ui\\View::set\(\)$~'
-
path: 'src/Popup.php'
message: '~^Parameter #1 \$fx \(Closure\(Atk4\\Ui\\View\): void\) of method Atk4\\Ui\\Popup::set\(\) should be contravariant with parameter \$arg1 \(mixed\) of method Atk4\\Ui\\View::set\(\)$~'
message: '~^Parameter #1 \$fx \(Closure\(Atk4\\Ui\\View\): void\) of method Atk4\\Ui\\Modal::set\(\) should be compatible with parameter \$content \(string\) of method Atk4\\Ui\\View::set\(\)$~'
-
path: 'src/Popup.php'
message: '~^Parameter #2 \$ignore \(\*NEVER\*\) of method Atk4\\Ui\\Popup::set\(\) should be compatible with parameter \$arg2 \(mixed\) of method Atk4\\Ui\\View::set\(\)$~'
message: '~^Parameter #1 \$fx \(Closure\(Atk4\\Ui\\View\): void\) of method Atk4\\Ui\\Popup::set\(\) should be compatible with parameter \$content \(string\) of method Atk4\\Ui\\View::set\(\)$~'
-
path: 'src/VirtualPage.php'
message: '~^Parameter #1 \$fx \(Closure\(\$this, mixed, mixed, mixed, mixed, mixed, mixed, mixed, mixed, mixed, mixed\): void\) of method Atk4\\Ui\\VirtualPage::set\(\) should be contravariant with parameter \$arg1 \(mixed\) of method Atk4\\Ui\\View::set\(\)$~'
message: '~^Parameter #1 \$fx \(Closure\(\$this, mixed, mixed, mixed, mixed, mixed, mixed, mixed, mixed, mixed, mixed\): void\) of method Atk4\\Ui\\VirtualPage::set\(\) should be compatible with parameter \$content \(string\) of method Atk4\\Ui\\View::set\(\)$~'
-
path: 'src/VirtualPage.php'
message: '~^Parameter #1 \$fx of method Atk4\\Ui\\Callback::set\(\) expects \(Closure\(mixed, mixed, mixed, mixed, mixed, mixed, mixed, mixed, mixed, mixed\): void\)\|null, Closure\(\$this, mixed, mixed, mixed, mixed, mixed, mixed, mixed, mixed, mixed, mixed\): void given\.$~'
-
path: 'src/VirtualPage.php'
message: '~^Parameter #2 \$fxArgs \(array\) of method Atk4\\Ui\\VirtualPage::set\(\) should be contravariant with parameter \$arg2 \(mixed\) of method Atk4\\Ui\\View::set\(\)$~'

# TODO these rules are generated, this ignores should be fixed in the code
# for level = 3
Expand Down
10 changes: 5 additions & 5 deletions src/Form.php
Expand Up @@ -291,16 +291,16 @@ public function jsSuccess($success = 'Success', $subHeader = null, bool $useTemp
if ($success instanceof View) {
$response = $success;
} elseif ($useTemplate) {
$response = $this->getApp()->loadTemplate($this->successTemplate);
$response->set('header', $success);
$responseTemplate = $this->getApp()->loadTemplate($this->successTemplate);
$responseTemplate->set('header', $success);

if ($subHeader) {
$response->set('message', $subHeader);
$responseTemplate->set('message', $subHeader);
} else {
$response->del('p');
$responseTemplate->del('p');
}

$response = $this->js()->html($response->renderToHtml());
$response = $this->js()->html($responseTemplate->renderToHtml());
} else {
$response = new Message([$success, 'type' => 'success', 'icon' => 'check']);
$response->setApp($this->getApp());
Expand Down
2 changes: 1 addition & 1 deletion src/HelloWorld.php
Expand Up @@ -13,6 +13,6 @@ protected function init(): void
{
parent::init();

$this->set('Content', 'Hello World');
$this->set('Hello World');
}
}
2 changes: 1 addition & 1 deletion src/ItemsPerPageSelector.php
Expand Up @@ -60,7 +60,7 @@ public function onPageLengthSelect(\Closure $fx): void
$this->cb->set(function () use ($fx) {
$ipp = isset($_GET['ipp']) ? (int) $_GET['ipp'] : null;
// $this->pageLength->set(preg_replace("/\[ipp\]/", $ipp, $this->label));
$this->set($ipp);
$this->set($ipp); // @phpstan-ignore-line TODO https://github.com/atk4/ui/issues/2016
$reload = $fx($ipp);
if ($reload) {
$this->getApp()->terminateJson($reload);
Expand Down
4 changes: 2 additions & 2 deletions src/Menu.php
Expand Up @@ -94,7 +94,7 @@ public function addMenu($name)
$label = $name['title'] ?? $name['text'] ?? $name['name'] ?? $name[0] ?? null;

if ($label !== null) {
$subMenu->set('label', $label);
$subMenu->template->set('label', $label);
}

if (isset($name['icon'])) {
Expand Down Expand Up @@ -126,7 +126,7 @@ public function addGroup($name, string $template = 'menugroup.html')
$title = $name['title'] ?? $name['text'] ?? $name['name'] ?? $name[0] ?? null;

if ($title !== null) {
$group->set('title', $title);
$group->template->set('title', $title);
}

if (isset($name['icon'])) {
Expand Down
43 changes: 12 additions & 31 deletions src/View.php
Expand Up @@ -334,44 +334,25 @@ public function getClosestOwner(string $class): ?self
/**
* TODO this method is hard to override, drop it from View.
*
* Override this method without compatibility with parent, if you wish
* to set your own things your own way for your view.
*
* @param mixed $arg1
* @param mixed $arg2
* @param string $content
*
* @return $this
*/
public function set($arg1 = null, $arg2 = null)
public function set($content)
{
if ($arg2 !== null) {
if (is_string($arg1)) {
$this->template->set($arg1, $arg2);

return $this;
}
} else {
if (is_string($arg1) || $arg1 === null) {
$this->content = $arg1;

return $this;
}

if (is_array($arg1)) {
if (isset($arg1[0])) {
$this->content = $arg1[0];
unset($arg1[0]);
}
$this->setDefaults($arg1);
if (func_num_args() > 1) { // prevent bad usage
throw new Exception('Only one argument is needed by View::set()');
}

return $this;
}
if (!is_string($content) && $content !== null) { // @phpstan-ignore-line
throw (new Exception('Not sure what to do with argument'))
->addMoreInfo('this', $this)
->addMoreInfo('arg', $content);
}

throw (new Exception('Not sure what to do with argument'))
->addMoreInfo('this', $this)
->addMoreInfo('arg1', $arg1)
->addMoreInfo('arg2', $arg2);
$this->content = $content;

return $this;
}

/**
Expand Down
7 changes: 5 additions & 2 deletions tests/ViewTest.php
Expand Up @@ -148,7 +148,7 @@ public function testSetException(): void

$this->expectException(Exception::class);
$this->expectExceptionMessage('Not sure what to do with argument');
$v->set(1);
$v->set(1); // @phpstan-ignore-line
}

/**
Expand Down Expand Up @@ -182,6 +182,8 @@ public function setNotClosureProvider(): array
}

/**
* TODO remove the explicit exceptions and this test/provider once release 5.0 is made.
*
* @param class-string<View> $class
*
* @dataProvider setNotOneArgumentExceptionProvider
Expand All @@ -192,7 +194,7 @@ public function testSetNotOneArgumentException(string $class): void

$this->expectException(Exception::class);
$this->expectExceptionMessage('Only one argument is needed by ' . preg_replace('~.+\\\\~', '', $class) . '::set()');
$v->set(function () {}, null);
$v->set(function () {}, null); // @phpstan-ignore-line
}

/**
Expand All @@ -201,6 +203,7 @@ public function testSetNotOneArgumentException(string $class): void
public function setNotOneArgumentExceptionProvider(): array
{
return [
[View::class],
[Loader::class],
[Modal::class],
[Popup::class],
Expand Down

0 comments on commit a78b335

Please sign in to comment.