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

Fix usage of hasXXX(), it now return bool only #1252

Merged
merged 4 commits into from Jun 12, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/ActionExecutor/Basic.php
Expand Up @@ -98,8 +98,6 @@ public function recursiveRender()

/**
* Check if all argument values have been provided.
*
* @return true
*/
public function hasAllArguments()
{
Expand Down
3 changes: 1 addition & 2 deletions src/Form.php
Expand Up @@ -342,13 +342,12 @@ public function success($success = 'Success', $sub_header = null, $useTemplate =
/**
* Add field into current layout. If no layout, create one. If no model, create blank one.
*
* @param string|null $name
* @param array|string|object|null $decorator
* @param array|string|object|null $field
*
* @return FormField\Generic
*/
public function addField($name, $decorator = null, $field = null)
public function addField(?string $name, $decorator = null, $field = null)
{
if (!$this->model) {
$this->model = new \atk4\ui\Misc\ProxyModel();
Expand Down
4 changes: 2 additions & 2 deletions src/FormField/MultiLine.php
Expand Up @@ -320,9 +320,9 @@ public function getValue()
foreach ($this->rowFields as $fieldName) {
$field = $m->getField($fieldName);
if ($field->isEditable()) {
$value = $row->get($field);
$value = $row->get($field->short_name);
} else {
$value = $this->app->ui_persistence->_typecastSaveField($field, $row->get($field));
$value = $this->app->ui_persistence->_typecastSaveField($field, $row->get($field->short_name));
}
$d_row[$fieldName] = $value;
}
Expand Down
36 changes: 12 additions & 24 deletions src/FormLayout/_Abstract.php
Expand Up @@ -20,18 +20,13 @@ abstract class _Abstract extends \atk4\ui\View
* Places field inside a layout somewhere. Should be called
* through $form->addField().
*
* @param string $name
* @param array|string|object|null $decorator
* @param array|string|object|null $field
*
* @return \atk4\ui\FormField\Generic
*/
public function addField($name, $decorator = null, $field = null)
public function addField(string $name, $decorator = null, $field = null)
{
if (!is_string($name)) {
throw new Exception('Format for addField now require first argument to be name');
}

if (!$this->form->model) {
$this->form->model = new \atk4\ui\Misc\ProxyModel();
}
Expand All @@ -40,27 +35,20 @@ public function addField($name, $decorator = null, $field = null)
$field = ['type' => $field];
}

if ($name) {
$existingField = $this->form->model->hasField($name);
}

try {
if (!$existingField) {
// Add missing field
if ($field) {
$field = $this->form->model->addField($name, $field);
if (!$this->form->model->hasField($name)) {
$field = $this->form->model->addField($name, $field);
} else {
$existingField = $this->form->model->getField($name);

if (is_array($field)) {
$field = $existingField->setDefaults($field);
} elseif (is_object($field)) {
throw (new Exception('Duplicate field'))
->addMoreInfo('name', $name);
} else {
$field = $this->form->model->addField($name);
$field = $existingField;
}
} elseif (is_array($field)) {
// Add properties to existing field
$existingField->setDefaults($field);
$field = $existingField;
} elseif (is_object($field)) {
throw (new Exception('Duplicate field'))
->addMoreInfo('name', $name);
} else {
$field = $existingField;
}

if (is_string($decorator)) {
Expand Down
6 changes: 1 addition & 5 deletions src/Grid.php
Expand Up @@ -585,11 +585,7 @@ public function applySort()

$this->table->sortable = true;

if (
$sortBy
&& isset($this->table->columns[$sortBy])
&& $this->model->hasField($sortBy)
) {
if ($sortBy && isset($this->table->columns[$sortBy]) && $this->model->hasField($sortBy)) {
$this->model->setOrder($sortBy, $desc);
$this->table->sort_by = $sortBy;
$this->table->sort_order = $desc ? 'descending' : 'ascending';
Expand Down
7 changes: 2 additions & 5 deletions src/Persistence/UI.php
Expand Up @@ -197,21 +197,18 @@ public function typecastSaveRow(Model $m, $row)

$result = [];
foreach ($row as $key => $value) {
// Look up field object
$f = $m->hasField($key);

// Figure out the name of the destination field
$field = $key;

// We have no knowledge of the field, it wasn't defined, so
// we will leave it as-is.
if (!$f) {
if (!$m->hasField($key)) {
$result[$field] = $value;

continue;
}

$value = $this->typecastSaveField($f, $value);
$value = $this->typecastSaveField($m->getField($key), $value);

// store converted value
$result[$field] = $value;
Expand Down
35 changes: 15 additions & 20 deletions src/Table.php
Expand Up @@ -178,7 +178,7 @@ public function initChunks()
*
* @return TableColumn\Generic
*/
public function addColumn($name, $columnDecorator = null, $field = null)
public function addColumn(?string $name, $columnDecorator = null, $field = null)
{
if (!$this->_initialized) {
throw new Exception\NoRenderTree($this, 'addColumn()');
Expand All @@ -194,29 +194,24 @@ public function addColumn($name, $columnDecorator = null, $field = null)
$field = ['type' => $field];
}

if (is_string($name) && $name) {
$existingField = $this->model->hasField($name);
} else {
$existingField = null;
}

if ($existingField === null) {
if ($name === null) {
// table column without respective field in model
$field = null;
} elseif (!$existingField) {
// Add missing field
$field = $this->model->addField($name, $field ?: []);
} elseif (!$this->model->hasField($name)) {
$field = $this->model->addField($name, $field);

$field->never_persist = true;
} elseif (is_array($field)) {
// Add properties to existing field
$existingField->setDefaults($field);
$field = $existingField;
} elseif (is_object($field)) {
throw (new Exception('Duplicate field'))
->addMoreInfo('name', $name);
} else {
$field = $existingField;
$existingField = $this->model->getField($name);

if (is_array($field)) {
$field = $existingField->setDefaults($field);
} elseif (is_object($field)) {
throw (new Exception('Duplicate field'))
->addMoreInfo('name', $name);
} else {
$field = $existingField;
}
}

if ($field === null) {
Expand Down Expand Up @@ -557,7 +552,7 @@ public function renderRow()
if (!is_array($columns)) {
$columns = [$columns];
}
$field = $this->model->hasField($name);
$field = $this->model->hasField($name) ? $this->model->getField($name) : null;
foreach ($columns as $column) {
if (!method_exists($column, 'getHTMLTags')) {
continue;
Expand Down
2 changes: 1 addition & 1 deletion src/TableColumn/HTML.php
Expand Up @@ -34,6 +34,6 @@ public function getDataCellHTML(Field $field = null, $extra_tags = [])
*/
public function getHTMLTags(Model $row, $field)
{
return ['_' . $field->short_name => '<td>' . $row->get($field) . '</td>'];
return ['_' . $field->short_name => '<td>' . $row->get($field->short_name) . '</td>'];
}
}
4 changes: 1 addition & 3 deletions src/Template.php
Expand Up @@ -207,10 +207,8 @@ public function getTagRefList($tag)
* If multiple tags are passed in as array, then return true if all of them exist.
*
* @param string|array $tag
*
* @return bool
*/
public function hasTag($tag)
public function hasTag($tag): bool
{
// check if all tags exist
if (is_array($tag)) {
Expand Down