Skip to content

Commit

Permalink
refactor: replace $data with $row
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Dec 7, 2023
1 parent b2bf84d commit 7ad3c38
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 81 deletions.
107 changes: 58 additions & 49 deletions system/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,11 +400,12 @@ abstract protected function doFirst();
* Inserts data into the current database.
* This method works only with dbCalls.
*
* @param array $data Data
* @param array $row Row data
* @phpstan-param row_array $row
*
* @return bool
*/
abstract protected function doInsert(array $data);
abstract protected function doInsert(array $row);

/**
* Compiles batch insert and runs the queries, validating each row prior.
Expand All @@ -423,10 +424,11 @@ abstract protected function doInsertBatch(?array $set = null, ?bool $escape = nu
* Updates a single record in the database.
* This method works only with dbCalls.
*
* @param array|int|string|null $id ID
* @param array|null $data Data
* @param array|int|string|null $id ID
* @param array|null $row Row data
* @phpstan-param row_array|null $row
*/
abstract protected function doUpdate($id = null, $data = null): bool;
abstract protected function doUpdate($id = null, $row = null): bool;

/**
* Compiles an update and runs the query.
Expand Down Expand Up @@ -509,15 +511,16 @@ abstract protected function idValue($data);
* Public getter to return the id value using the idValue() method.
* For example with SQL this will return $data->$this->primaryKey.
*
* @param array|object $data
* @param array|object $row Row data
* @phpstan-param row_array|object $row
*
* @return array|int|string|null
*
* @todo: Make abstract in version 5.0
*/
public function getIdValue($data)
public function getIdValue($row)
{
return $this->idValue($data);
return $this->idValue($row);
}

/**
Expand Down Expand Up @@ -693,20 +696,21 @@ public function first()
* you must ensure that the class will provide access to the class
* variables, even if through a magic method.
*
* @param array|object $data Data
* @param array|object $row Row data
* @phpstan-param row_array|object $row
*
* @throws ReflectionException
*/
public function save($data): bool
public function save($row): bool
{
if (empty($data)) {
if (empty($row)) {
return true;
}

if ($this->shouldUpdate($data)) {
$response = $this->update($this->getIdValue($data), $data);
if ($this->shouldUpdate($row)) {
$response = $this->update($this->getIdValue($row), $row);
} else {
$response = $this->insert($data, false);
$response = $this->insert($row, false);

if ($response !== false) {
$response = true;
Expand All @@ -720,11 +724,12 @@ public function save($data): bool
* This method is called on save to determine if entry have to be updated.
* If this method returns false insert operation will be executed
*
* @param array|object $data Data
* @param array|object $row Row data
* @phpstan-param row_array|object $row
*/
protected function shouldUpdate($data): bool
protected function shouldUpdate($row): bool
{
return ! empty($this->getIdValue($data));
return ! empty($this->getIdValue($row));
}

/**
Expand Down Expand Up @@ -776,7 +781,7 @@ public function insert($row = null, bool $returnID = true)
$row = $this->doProtectFieldsForInsert($row);

// doProtectFields() can further remove elements from
// $data so we need to check for empty dataset again
// $row, so we need to check for empty dataset again
if (! $this->allowEmptyInserts && empty($row)) {
throw DataException::forEmptyDataset('insert');
}
Expand Down Expand Up @@ -867,7 +872,7 @@ public function insertBatch(?array $set = null, ?bool $escape = null, int $batch

if (is_array($set)) {
foreach ($set as &$row) {
// If $data is using a custom class with public or protected
// If $row is using a custom class with public or protected
// properties representing the collection elements, we need to grab
// them as an array.
if (is_object($row) && ! $row instanceof stdClass) {
Expand Down Expand Up @@ -958,7 +963,7 @@ public function update($id = null, $row = null): bool
$row = $this->doProtectFields($row);

// doProtectFields() can further remove elements from
// $data, so we need to check for empty dataset again
// $row, so we need to check for empty dataset again
if (empty($row)) {
throw DataException::forEmptyDataset('update');
}
Expand Down Expand Up @@ -1007,7 +1012,7 @@ public function updateBatch(?array $set = null, ?string $index = null, int $batc
{
if (is_array($set)) {
foreach ($set as &$row) {
// If $data is using a custom class with public or protected
// If $row is using a custom class with public or protected
// properties representing the collection elements, we need to grab
// them as an array.
if (is_object($row) && ! $row instanceof stdClass) {
Expand Down Expand Up @@ -1269,46 +1274,48 @@ public function protect(bool $protect = true)
* Ensures that only the fields that are allowed to be updated are
* in the data array.
*
* Used by update() and updateBatch() to protect against mass assignment
* vulnerabilities.
* @used-by update() to protect against mass assignment vulnerabilities.
* @used-by updateBatch() to protect against mass assignment vulnerabilities.
*
* @param array $data Data
* @param array $row Row data
* @phpstan-param row_array $row
*
* @throws DataException
*/
protected function doProtectFields(array $data): array
protected function doProtectFields(array $row): array
{
if (! $this->protectFields) {
return $data;
return $row;
}

if (empty($this->allowedFields)) {
throw DataException::forInvalidAllowedFields(static::class);
}

foreach (array_keys($data) as $key) {
foreach (array_keys($row) as $key) {
if (! in_array($key, $this->allowedFields, true)) {
unset($data[$key]);
unset($row[$key]);
}
}

return $data;
return $row;
}

/**
* Ensures that only the fields that are allowed to be inserted are in
* the data array.
*
* Used by insert() and insertBatch() to protect against mass assignment
* vulnerabilities.
* @used-by insert() to protect against mass assignment vulnerabilities.
* @used-by insertBatch() to protect against mass assignment vulnerabilities.
*
* @param array $data Data
* @param array $row Row data
* @phpstan-param row_array $row
*
* @throws DataException
*/
protected function doProtectFieldsForInsert(array $data): array
protected function doProtectFieldsForInsert(array $row): array
{
return $this->doProtectFields($data);
return $this->doProtectFields($row);
}

/**
Expand Down Expand Up @@ -1493,25 +1500,26 @@ public function cleanRules(bool $choice = false)
}

/**
* Validate the data against the validation rules (or the validation group)
* Validate the row data against the validation rules (or the validation group)
* specified in the class property, $validationRules.
*
* @param array|object $data Data
* @param array|object $row Row data
* @phpstan-param row_array|object $row
*/
public function validate($data): bool
public function validate($row): bool
{
$rules = $this->getValidationRules();

if ($this->skipValidation || empty($rules) || empty($data)) {
if ($this->skipValidation || empty($rules) || empty($row)) {
return true;
}

// Validation requires array, so cast away.
if (is_object($data)) {
$data = (array) $data;
if (is_object($row)) {
$row = (array) $row;
}

$rules = $this->cleanValidationRules ? $this->cleanValidationRules($rules, $data) : $rules;
$rules = $this->cleanValidationRules ? $this->cleanValidationRules($rules, $row) : $rules;

// If no data existed that needs validation
// our job is done here.
Expand All @@ -1521,7 +1529,7 @@ public function validate($data): bool

$this->validation->reset()->setRules($rules, $this->validationMessages);

return $this->validation->run($data, null, $this->DBGroup);
return $this->validation->run($row, null, $this->DBGroup);
}

/**
Expand Down Expand Up @@ -1565,17 +1573,18 @@ public function getValidationMessages(): array
* currently so that rules don't block updating when only updating
* a partial row.
*
* @param array $rules Array containing field name and rule
* @param array|null $data Data
* @param array $rules Array containing field name and rule
* @param array $row Row data (@TODO Remove null in param type)
* @phpstan-param row_array $row
*/
protected function cleanValidationRules(array $rules, ?array $data = null): array
protected function cleanValidationRules(array $rules, ?array $row = null): array
{
if (empty($data)) {
if (empty($row)) {
return [];
}

foreach (array_keys($rules) as $field) {
if (! array_key_exists($field, $data)) {
if (! array_key_exists($field, $row)) {
unset($rules[$field]);
}
}
Expand Down Expand Up @@ -1767,7 +1776,7 @@ protected function transformDataToArray($row, string $type): array
throw DataException::forEmptyDataset($type);
}

// If $data is using a custom class with public or protected
// If $row is using a custom class with public or protected
// properties representing the collection elements, we need to grab
// them as an array.
if (is_object($row) && ! $row instanceof stdClass) {
Expand All @@ -1785,7 +1794,7 @@ protected function transformDataToArray($row, string $type): array
$row = (array) $row;
}

// If it's still empty here, means $data is no change or is empty object
// If it's still empty here, means $row is no change or is empty object
if (! $this->allowEmptyInserts && empty($row)) {
throw DataException::forEmptyDataset($type);
}
Expand Down
Loading

0 comments on commit 7ad3c38

Please sign in to comment.