Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model::addUserAction should accept seed /w class #1070

Merged
merged 3 commits into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/model.rst
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ You may also specify your own Field implementation::

Read more about :php:class:`Field`

.. php:method:: addFields(array $fields, $defaults = [])
.. php:method:: addFields(array $fields, $seed = [])

Creates multiple field objects in one method call. See multiple syntax examples::

Expand Down
14 changes: 7 additions & 7 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -569,19 +569,19 @@ public function addField(string $name, $seed = []): Field
* Adds multiple fields into model.
*
* @param array<string, array<mixed>|object>|array<int, string> $fields
* @param array<string, mixed> $defaults
* @param array<mixed> $seed
*
* @return $this
*/
public function addFields(array $fields, array $defaults = [])
public function addFields(array $fields, array $seed = [])
{
foreach ($fields as $name => $seed) {
if (is_int($name)) {
$name = $seed;
$seed = [];
foreach ($fields as $k => $v) {
if (is_int($k)) {
$k = $v;
$v = [];
}

$this->addField($name, Factory::mergeSeeds($seed, $defaults));
$this->addField($k, Factory::mergeSeeds($v, $seed));
}

return $this;
Expand Down
14 changes: 7 additions & 7 deletions src/Model/Join.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,19 +295,19 @@ public function addField(string $name, array $seed = []): Field
* Adds multiple fields.
*
* @param array<string, array<mixed>>|array<int, string> $fields
* @param array<string, mixed> $defaults
* @param array<mixed> $seed
*
* @return $this
*/
public function addFields(array $fields = [], array $defaults = [])
public function addFields(array $fields = [], array $seed = [])
{
foreach ($fields as $name => $seed) {
if (is_int($name)) {
$name = $seed;
$seed = [];
foreach ($fields as $k => $v) {
if (is_int($k)) {
$k = $v;
$v = [];
}

$this->addField($name, Factory::mergeSeeds($seed, $defaults));
$this->addField($k, Factory::mergeSeeds($v, $seed));
}

return $this;
Expand Down
2 changes: 1 addition & 1 deletion src/Model/UserAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,6 @@ public function getConfirmation()

public function getCaption(): string
{
return $this->caption ?? ucwords(str_replace('_', ' ', $this->shortName));
return $this->caption ?? $this->getModel()->readableCaption($this->shortName);
}
}
17 changes: 6 additions & 11 deletions src/Model/UserActionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,18 @@ trait UserActionsTrait
* Register new user action for this model. By default UI will allow users to trigger actions
* from UI.
*
* @param array<string, mixed>|\Closure $defaults
* @param array<mixed>|\Closure $seed
*/
public function addUserAction(string $name, $defaults = []): UserAction
public function addUserAction(string $name, $seed = []): UserAction
{
$this->assertIsModel();

if ($defaults instanceof \Closure) {
$defaults = ['callback' => $defaults];
if ($seed instanceof \Closure) {
$seed = ['callback' => $seed];
}

if (!isset($defaults['caption'])) {
Copy link
Member Author

@mvorisek mvorisek Sep 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems the reasons are purely historical and not intentional/needed now

$defaults['caption'] = $this->readableCaption($name);
}

/** @var UserAction $action */
$action = Factory::factory($this->_defaultSeedUserAction, $defaults);

$seed = Factory::mergeSeeds($seed, $this->_defaultSeedUserAction);
$action = UserAction::fromSeed($seed);
$this->_addIntoCollection($name, $action, 'userActions');

return $action;
Expand Down
14 changes: 8 additions & 6 deletions src/Reference/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Atk4\Data\Reference;

use Atk4\Core\Factory;
use Atk4\Data\Exception;
use Atk4\Data\Field;
use Atk4\Data\Model;
Expand Down Expand Up @@ -162,18 +163,19 @@ public function addField(string $fieldName, array $defaults = []): Field
* Adds multiple fields.
*
* @param array<string, array<mixed>>|array<int, string> $fields
* @param array<mixed> $defaults
*
* @return $this
*/
public function addFields(array $fields = [])
public function addFields(array $fields = [], array $defaults = [])
{
foreach ($fields as $name => $seed) {
if (is_int($name)) {
$name = $seed;
$seed = [];
foreach ($fields as $k => $v) {
if (is_int($k)) {
$k = $v;
$v = [];
}

$this->addField($name, $seed);
$this->addField($k, Factory::mergeSeeds($v, $defaults));
}

return $this;
Expand Down
10 changes: 10 additions & 0 deletions tests/UserActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ public function testBasic(): void
static::assertFalse($client->hasUserAction('foo'));
}

public function testCustomSeedClass(): void
{
$customClass = get_class(new class() extends Model\UserAction {});

$client = new UaClient($this->pers);
$client->addUserAction('foo', [$customClass]);

static::assertSame($customClass, get_class($client->getUserAction('foo')));
}

public function testPreview(): void
{
$client = new UaClient($this->pers);
Expand Down