Skip to content

Commit

Permalink
Add boolean field conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
krangadurai authored Aug 23, 2024
1 parent 655bd1d commit 2584b21
Showing 1 changed file with 53 additions and 1 deletion.
54 changes: 53 additions & 1 deletion system/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ abstract class BaseModel
*/
protected $allowedFields = [];

/**
* An array of field names that should be cast to boolean.
*
* @var array<string>
*/
protected $booleanFields = [];

/**
* If true, will set created_at, and updated_at
* values during insert and update routines.
Expand Down Expand Up @@ -615,14 +622,59 @@ public function find($id = null)
if ($this->tempAllowCallbacks) {
$eventData = $this->trigger('afterFind', $eventData);
}

$eventData = $this->convertBooleanFields($eventData);
$this->tempReturnType = $this->returnType;
$this->tempUseSoftDeletes = $this->useSoftDeletes;
$this->tempAllowCallbacks = $this->allowCallbacks;

return $eventData['data'];
}


/**
* Convert specific fields to boolean after fetching the data.
*
* @param array $data
* @return array
*/
protected function convertBooleanFields(array $data)

Check failure on line 640 in system/BaseModel.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

Method CodeIgniter\BaseModel::convertBooleanFields() has parameter $data with no value type specified in iterable type array.

Check failure on line 640 in system/BaseModel.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

Method CodeIgniter\BaseModel::convertBooleanFields() return type has no value type specified in iterable type array.
{
if (empty($this->booleanFields)) {

Check failure on line 642 in system/BaseModel.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

Construct empty() is not allowed. Use more strict comparison.
return $data;
}

if (isset($data['data'])) {
if (isset($data['data'][0])) {
// Multiple rows case
foreach ($data['data'] as &$row) {
$row = $this->convertFieldsToBoolean($row, $this->booleanFields);
}
} else {
// Single row case
$data['data'] = $this->convertFieldsToBoolean($data['data'], $this->booleanFields);
}
}

return $data;
}

/**
* Helper function to convert specific fields to boolean.
*
* @param array $data
* @param array $fields
* @return array
*/
private function convertFieldsToBoolean(array $data, array $fields)

Check failure on line 668 in system/BaseModel.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

Method CodeIgniter\BaseModel::convertFieldsToBoolean() has parameter $data with no value type specified in iterable type array.

Check failure on line 668 in system/BaseModel.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

Method CodeIgniter\BaseModel::convertFieldsToBoolean() has parameter $fields with no value type specified in iterable type array.

Check failure on line 668 in system/BaseModel.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

Method CodeIgniter\BaseModel::convertFieldsToBoolean() return type has no value type specified in iterable type array.
{
foreach ($fields as $field) {
if (isset($data[$field])) {
$data[$field] = (bool) $data[$field];
}
}
return $data;
}

/**
* Fetches the column of database.
*
Expand Down

0 comments on commit 2584b21

Please sign in to comment.