Implement type checking for settings objects

This commit is contained in:
David Bomba 2019-10-10 08:36:50 +11:00
parent 056b2cbdb7
commit e56aac5e73
3 changed files with 21 additions and 20 deletions

View File

@ -60,7 +60,7 @@ class Company extends BaseModel
// 'vat_number',
// 'id_number',
'size_id',
'settings',
//'settings',
];
protected $hidden = [

View File

@ -35,8 +35,7 @@ trait CompanySettingsSaver
//make sure the inbound settings have the correct casts!
//$settings = CompanySettings::setCasts($settings, CompanySettings::$casts);
//todo checks are here
// $settings = $this->checkSettingType($settings, CompanySettings::$casts);
$settings = $this->checkSettingType($settings, CompanySettings::$casts);
//iterate through set properties with new values;
foreach($settings as $key => $value)
@ -53,38 +52,37 @@ trait CompanySettingsSaver
foreach ($casts as $key => $value){
\Log::error("the gettype of {$key} = ". gettype($settings->{$key}));
if(substr($key, -3) == '_id'){
/*Separate loop if it is a _id field which is an integer cast as a string*/
if(substr($key, -3) == '_id' || substr($key, -8) == '_counter'){
$value = "integer";
if($this->checkAttribute($value, (int)$settings->{$key})){
//throw new \Exception($settings->{$key}. " " . $key . " is not type ". $value);
\Log::error($settings->{$key}. " " . $key . " is type ". $value);
if($this->checkAttribute($value, $settings->{$key})){
\Log::error("System says true {$key} a {$value} = ".$settings->{$key});
}
else {
\Log::error($settings->{$key}. " " . $key . " is nottype ". $value);
\Log::error('popping '.$key.' '.$value.' '.$settings->{$key}.' off the stack');
unset($settings->{$key});
}
continue;
}
/* Handles unset settings or blank strings */
if(is_null($settings->{$key}) || !isset($settings->{$key}) || $settings->{$key} == ''){
\Log::error("skipping ".$settings->{$key}. " " . $key . " is type ". $value);
continue;
}
\Log::error("checking ".$settings->{$key}. " " . $key . " is type ". $value);
/*Catch all filter */
if($this->checkAttribute($value, $settings->{$key})){
//throw new \Exception($settings->{$key}. " " . $key . " is not type ". $value);
\Log::error($settings->{$key}. " " . $key . " is type ". $value);
}
else {
\Log::error($settings->{$key}. " " . $key . " is nottype ". $value);
unset($settings->{$key});
}
}
\Log::error(print_r($settings,1));
return $settings;
}
@ -94,7 +92,9 @@ trait CompanySettingsSaver
{
case 'int':
case 'integer':
return is_int($value);
//return is_int($value);
//return strval($value) === strval(intval($value)) ;
return ctype_digit(strval($value));
case 'real':
case 'float':
case 'double':

View File

@ -70,6 +70,7 @@ class CompanySettingsTest extends TestCase
$settings = $this->company->settings;
$settings->client_number_counter = "a";
$settings->invoice_number_counter = 1000;
$this->company->settings = $settings;
@ -78,12 +79,12 @@ class CompanySettingsTest extends TestCase
'X-API-Token' => $this->token,
])->put('/api/v1/companies/'.$this->encodePrimaryKey($this->company->id), $this->company->toArray());
$response->assertStatus(200);
$arr = $response->json();
//\Log::error($arr);
\Log::error($arr);
// $this->assertEquals($arr['data']['settings']['client_number_counter'],1);
$this->assertEquals($arr['data']['settings']['client_number_counter'],1);
$this->assertEquals($arr['data']['settings']['invoice_number_counter'],1000);
}
}