public function __set(string $key, $value = null)
{
$key = $this->mapProperty($key);
// Check if the field should be mutated into a date
if (in_array($key, $this->dates))
{
$value = $this->mutateDate($value);
}
$isNullable = false;
$castTo = false;
if (array_key_exists($key, $this->casts))
{
$isNullable = strpos($this->casts[$key], '?') === 0;
$castTo = $isNullable ? substr($this->casts[$key], 1) : $this->casts[$key];
}
if (! $isNullable || ! is_null($value))
{
// Array casting requires that we serialize the value
// when setting it so that it can easily be stored
// back to the database.
if ($castTo === 'array')
{
$value = serialize($value);
}
// JSON casting requires that we JSONize the value
// when setting it so that it can easily be stored
// back to the database.
if (($castTo === 'json' || $castTo === 'json-array') && function_exists('json_encode'))
{
$value = json_encode($value);
if (json_last_error() !== JSON_ERROR_NONE)
{
throw CastException::forInvalidJsonFormatException(json_last_error());
}
}
}
// if a set* method exists for this key,
// use that method to insert this value.
// *) should be outside $isNullable check - SO maybe wants to do sth with null value automatically
$method = 'set' . str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $key)));
if (method_exists($this, $method))
{
$this->$method($value);
return $this;
}
// Otherwise, just the value.
// This allows for creation of new class
// properties that are undefined, though
// they cannot be saved. Useful for
// grabbing values through joins,
// assigning relationships, etc.
if (gettype($value) === 'string') {
$this->attributes[$key] = trim($value);
} else {
$this->attributes[$key] = $value;
}
return $this;
}