-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[5.3] [WIP] Custom Type Casting #13315
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Illuminate\Contracts\Database\TypeCaster; | ||
|
||
interface TypeCaster | ||
{ | ||
/** | ||
* Register a custom Type Caster extension. | ||
* | ||
* @param string $rule | ||
* @param \Closure|string $fromDatabase | ||
* @param \Closure|string|null $toDatabase | ||
* @return void | ||
*/ | ||
public function extend($rule, $fromDatabase, $toDatabase = null); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2812,9 +2812,13 @@ protected function castAttribute($key, $value) | |
return $this->asDateTime($value); | ||
case 'timestamp': | ||
return $this->asTimeStamp($value); | ||
default: | ||
return $value; | ||
} | ||
|
||
if ($this->castExists($key)) { | ||
return $this->asCustom($key, $value); | ||
} | ||
|
||
return $value; | ||
} | ||
|
||
/** | ||
|
@@ -2944,6 +2948,76 @@ protected function asTimeStamp($value) | |
return $this->asDateTime($value)->getTimestamp(); | ||
} | ||
|
||
/** | ||
* Return the custom cast object. | ||
* | ||
* @param mixed $value | ||
* @return mixed | ||
*/ | ||
protected function asCustom($key, $value) | ||
{ | ||
$cast = $this->castClass($key); | ||
|
||
if ($value instanceof $cast) { | ||
return $value; | ||
} | ||
|
||
$reflectionClass = new \ReflectionClass($cast); | ||
|
||
return $reflectionClass->newInstanceArgs([$value] + $this->castParameters($key)); | ||
} | ||
|
||
/** | ||
* Has a custom cast type been defined for a model attribute and does it exist. | ||
* | ||
* @param string $key | ||
* @return bool | ||
*/ | ||
protected function castExists($key) | ||
{ | ||
if (! $this->castClass($key)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Return the fully qualified custom class. | ||
* | ||
* @param string $key | ||
* @return string | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Return mixed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mixed should be avoided if possible There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this case adding |void to return will be fine |
||
*/ | ||
protected function castClass($key) | ||
{ | ||
if (strpos($cast = $this->getCasts()[$key], ',') !== false) { | ||
$parameters = explode(',', $cast); | ||
|
||
$cast = $parameters[0]; | ||
} | ||
|
||
if (class_exists($cast)) { | ||
return $cast; | ||
} | ||
} | ||
|
||
/** | ||
* Return an array of custom cast parameters. | ||
* | ||
* @param string $key | ||
* @return array | ||
*/ | ||
protected function castParameters($key) | ||
{ | ||
if (strpos($cast = $this->getCasts()[$key], ',') !== false) { | ||
$parameters = explode(',', $cast); | ||
|
||
return $parameters; | ||
} | ||
|
||
return []; | ||
} | ||
|
||
/** | ||
* Prepare a date for array / JSON serialization. | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace Illuminate\Database\Eloquent\TypeCaster; | ||
|
||
use Closure; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Contracts\Container\Container; | ||
use Illuminate\Contracts\Database\TypeCaster\Factory as FactoryContract; | ||
|
||
class Factory implements FactoryContract | ||
{ | ||
/** | ||
* The IoC container instance. | ||
* | ||
* @var \Illuminate\Contracts\Container\Container | ||
*/ | ||
protected $container; | ||
|
||
/** | ||
* All of the custom Type Caster extensions. | ||
* | ||
* @var array | ||
*/ | ||
protected $extensions = []; | ||
|
||
/** | ||
* Create a new Type Caster factory instance. | ||
* | ||
* @param \Illuminate\Contracts\Container\Container $container | ||
* @return void | ||
*/ | ||
public function __construct(Container $container = null) | ||
{ | ||
$this->container = $container; | ||
} | ||
|
||
/** | ||
* Create a new Type Caster instance. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Model $model | ||
* @return \Illuminate\Database\Eloquent\TypeCaster\TypeCaster | ||
*/ | ||
public function make(Model $model) | ||
{ | ||
$typecaster = new TypeCaster($model); | ||
|
||
// Next we'll set the IoC container instance of the type caster, which is used | ||
// to resolve out class based type casting extensions. If it is not set then | ||
// these extension types wont be possible on these type caster instances. | ||
if (! is_null($this->container)) { | ||
$typecaster->setContainer($this->container); | ||
} | ||
|
||
$typecaster->addExtensions($this->extensions); | ||
|
||
return $typecaster; | ||
} | ||
|
||
/** | ||
* Register a custom Type Caster extension. | ||
* | ||
* @param string $rule | ||
* @param \Closure|string $fromDatabase | ||
* @param \Closure|string|null $toDatabase | ||
* @return void | ||
*/ | ||
public function extend($rule, $fromDatabase, $toDatabase = null) | ||
{ | ||
$this->extensions[$rule] = [ | ||
'from' => $fromDatabase, | ||
'to' => $toDatabase, | ||
]; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return $this->castClass($key);