mirror of
https://github.com/alextselegidis/easyappointments.git
synced 2024-11-24 08:53:05 +03:00
Common type casting functionality
This commit is contained in:
parent
b7305be038
commit
dd546164fb
1 changed files with 38 additions and 0 deletions
|
@ -61,6 +61,11 @@
|
|||
* @property Timezones $timezones
|
||||
*/
|
||||
class EA_Model extends CI_Model {
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $casts = [];
|
||||
|
||||
/**
|
||||
* EA_Model constructor.
|
||||
*/
|
||||
|
@ -130,4 +135,37 @@ class EA_Model extends CI_Model {
|
|||
{
|
||||
return $this->save($record);
|
||||
}
|
||||
|
||||
public function cast(array &$record)
|
||||
{
|
||||
foreach ($this->casts as $attribute => $cast)
|
||||
{
|
||||
if ( ! isset($record[$attribute]))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
switch ($cast)
|
||||
{
|
||||
case 'integer':
|
||||
$record[$attribute] = (int)$record[$attribute];
|
||||
break;
|
||||
|
||||
case 'float':
|
||||
$record[$attribute] = (float)$record[$attribute];
|
||||
break;
|
||||
|
||||
case 'boolean':
|
||||
$record[$attribute] = (bool)$record[$attribute];
|
||||
break;
|
||||
|
||||
case 'string':
|
||||
$record[$attribute] = (string)$record[$attribute];
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new RuntimeException('Unsupported cast type provided: ' . $cast);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue