Skip to content
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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Illuminate/Contracts/Database/TypeCaster/Factory.php
@@ -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);
}
28 changes: 28 additions & 0 deletions src/Illuminate/Database/DatabaseServiceProvider.php
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Database\Eloquent\QueueEntityResolver;
use Illuminate\Database\Connectors\ConnectionFactory;
use Illuminate\Database\Eloquent\Factory as EloquentFactory;
use Illuminate\Database\Eloquent\TypeCaster as TypeCasterFactory;

class DatabaseServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -37,6 +38,8 @@ public function register()

$this->registerQueueableEntityResolver();

$this->registerTypeCasterFactory();

// The connection factory is used to create the actual connection instances on
// the database. We will inject the factory into the manager so that it may
// make the connections while they are actually needed and not of before.
Expand Down Expand Up @@ -85,4 +88,29 @@ protected function registerQueueableEntityResolver()
return new QueueEntityResolver;
});
}

/**
* Register the Type Caster factory.
*
* @return void
*/
protected function registerTypeCasterFactory()
{
$this->app->singleton('typecaster', function ($app) {
return new TypeCasterFactory($app);
});
}

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return [
'typecaster',
];
}
}

78 changes: 76 additions & 2 deletions src/Illuminate/Database/Eloquent/Model.php
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return $this->castClass($key);

return false;
}

return true;
}

/**
* Return the fully qualified custom class.
*
* @param string $key
* @return string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return mixed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mixed should be avoided if possible

Copy link
Contributor

Choose a reason for hiding this comment

The 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.
*
Expand Down
74 changes: 74 additions & 0 deletions src/Illuminate/Database/Eloquent/TypeCaster/Factory.php
@@ -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,
];
}
}