Skip to content

Commit

Permalink
Refactor setters in entity classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Skouat committed Oct 19, 2024
1 parent 79c9627 commit d49f206
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 591 deletions.
2 changes: 1 addition & 1 deletion actions/currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function __construct(
* Get currency data based on currency ISO code
*
* @param string $iso_code The ISO code of the currency
* @return array Currency data
* @return void
*/
public function set_currency_data_from_iso_code(string $iso_code): void
{
Expand Down
28 changes: 11 additions & 17 deletions controller/admin/currency_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,28 +138,12 @@ private function add_edit_currency_data(array $data): void
{
$this->submit = $this->is_form_submitted();

$errors = $this->validate_currency_data();

$this->set_currency_entity_data($data);

// Insert or update currency
$errors = $this->validate_currency_data();
$this->submit_data($errors);

$this->assign_template_vars($errors);
}

private function validate_currency_data(): array
{
$errors = [];

return array_merge($errors,
$this->is_invalid_form('add_edit_' . $this->module_name, $this->submit_or_preview($this->submit)),
$this->is_empty_data($this->ppde_entity, 'name', '', $this->submit_or_preview($this->submit)),
$this->is_empty_data($this->ppde_entity, 'iso_code', '', $this->submit_or_preview($this->submit)),
$this->is_empty_data($this->ppde_entity, 'symbol', '', $this->submit_or_preview($this->submit))
);
}

private function set_currency_entity_data(array $data): void
{
if ($this->ppde_locale->is_locale_configured())
Expand All @@ -177,6 +161,16 @@ private function set_currency_entity_data(array $data): void

$this->ppde_entity->set_entity_data($item_fields);
}
private function validate_currency_data(): array
{
$errors = [];
return array_merge($errors,
$this->is_invalid_form('add_edit_' . $this->module_name, $this->submit_or_preview($this->submit)),
$this->is_empty_data($this->ppde_entity, 'name', '', $this->submit_or_preview($this->submit)),
$this->is_empty_data($this->ppde_entity, 'iso_code', '', $this->submit_or_preview($this->submit)),
$this->is_empty_data($this->ppde_entity, 'symbol', '', $this->submit_or_preview($this->submit))
);
}

private function assign_template_vars(array $errors): void
{
Expand Down
1 change: 0 additions & 1 deletion controller/admin/transactions_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
class transactions_controller extends admin_main
{
protected $transactions_operator;
protected $adm_relative_path;
protected $auth;
protected $entry_count;
protected $last_page_offset;
Expand Down
48 changes: 0 additions & 48 deletions entity/currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,22 +120,6 @@ public function get_symbol(): string
return (isset($this->data['currency_symbol'])) ? html_entity_decode($this->data['currency_symbol'], ENT_COMPAT | ENT_HTML5, 'UTF-8') : '';
}

/**
* Set Currency status
*
* @param bool $on_left
*
* @return currency $this object for chaining calls; load()->set()->save()
* @access public
*/
public function set_currency_position($on_left): currency
{
// Set the item type on our data array
$this->data['currency_on_left'] = (bool) $on_left;

return $this;
}

/**
* Set Currency status
*
Expand All @@ -152,38 +136,6 @@ public function set_currency_enable($enable): currency
return $this;
}

/**
* Set Currency ISO code name
*
* @param string $iso_code
*
* @return currency $this object for chaining calls; load()->set()->save()
* @access public
*/
public function set_iso_code($iso_code): currency
{
// Set the lang_id on our data array
$this->data['currency_iso_code'] = (string) $iso_code;

return $this;
}

/**
* Set Currency symbol
*
* @param string $symbol
*
* @return currency $this object for chaining calls; load()->set()->save()
* @access public
*/
public function set_symbol($symbol): currency
{
// Set the lang_id on our data array
$this->data['currency_symbol'] = htmlentities($symbol, ENT_COMPAT | ENT_HTML5, 'UTF-8');

return $this;
}

/**
* {@inheritdoc}
*/
Expand Down
18 changes: 0 additions & 18 deletions entity/donation_pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,6 @@ public function get_lang_id(): int
return (int) ($this->data['page_lang_id'] ?? 0);
}

/**
* Set Lang identifier
*
* @param int $lang
*
* @return donation_pages $this object for chaining calls; load()->set()->save()
* @access public
*/
public function set_lang_id($lang)
{
// Set the lang_id on our data array
$this->data['page_lang_id'] = (int) $lang;

return $this;
}

/**
* Get message for edit
*
Expand Down Expand Up @@ -219,8 +203,6 @@ public function set_message($message)
$this->data['page_content_bbcode_uid'] = $uid;
$this->data['page_content_bbcode_bitfield'] = $bitfield;

// Flags are already set

return $this;
}

Expand Down
39 changes: 15 additions & 24 deletions entity/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,21 @@ public function __construct(
* @return void
* @access public
*/
public function set_entity_data($data_ary): void
public function set_entity_data(array $data_ary): void
{
foreach ($data_ary as $entity_function => $data)
foreach ($data_ary as $key => $value)
{
// Calling the set_$entity_function on the entity and passing it $currency_data
call_user_func_array([$this, 'set_' . $entity_function], [$data]);
$schema_key = 'item_' . $key;
if (isset($this->table_schema[$schema_key]))
{
settype($value, $this->table_schema[$schema_key]['type']);
$this->data[$this->table_schema[$schema_key]['name']] = $value;
}
else
{
call_user_func_array([$this, 'set_' . $key], [$value]);
}
}
unset($entity_function);
}

/**
Expand Down Expand Up @@ -133,7 +140,7 @@ public function insert($run_before_insert = '')
$this->db->sql_query($sql);

// Set the item_id using the id created by the SQL insert
$this->data[$this->table_schema['item_id']['name']] = (int) $this->db->sql_nextid();
$this->data[$this->table_schema['item_id']['name']] = (int) $this->db->sql_last_inserted_id();

return $this;
}
Expand Down Expand Up @@ -245,9 +252,9 @@ public function data_exists($sql): bool
* @return main $this object for chaining calls; load()->set()->save()
* @access public
*/
public function set_id($id)
public function set_id(int $id)
{
$this->data[$this->table_schema['item_id']['name']] = (int) $id;
$this->data[$this->table_schema['item_id']['name']] = $id;

return $this;
}
Expand Down Expand Up @@ -302,22 +309,6 @@ public function get_name(): string
return (string) ($this->data[$this->table_schema['item_name']['name']] ?? '');
}

/**
* Set Item name
*
* @param string $name
*
* @return main $this object for chaining calls; load()->set()->save()
* @access public
*/
public function set_name($name)
{
// Set the item type on our data array
$this->data[$this->table_schema['item_name']['name']] = (string) $name;

return $this;
}

/**
* Set page url
*
Expand Down
Loading

0 comments on commit d49f206

Please sign in to comment.