forked from laravel/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Factory.php
74 lines (64 loc) · 1.92 KB
/
Factory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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,
];
}
}