|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WebTheory\Saveyour\Factories; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use WebTheory\GuctilityBelt\Traits\ClassResolverTrait; |
|
7
|
|
|
use WebTheory\GuctilityBelt\Traits\SmartFactoryTrait; |
|
8
|
|
|
use WebTheory\Saveyour\Contracts\FieldDataManagerInterface; |
|
9
|
|
|
use WebTheory\Saveyour\Contracts\FieldDataManagerResolverFactoryInterface; |
|
10
|
|
|
use WebTheory\Saveyour\Managers\FieldDataManagerCallback; |
|
11
|
|
|
|
|
12
|
|
|
class DataManagerFactory implements FieldDataManagerResolverFactoryInterface |
|
13
|
|
|
{ |
|
14
|
|
|
use SmartFactoryTrait; |
|
|
|
|
|
|
15
|
|
|
use ClassResolverTrait; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* |
|
19
|
|
|
*/ |
|
20
|
|
|
private $managers = []; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $namespaces = []; |
|
26
|
|
|
|
|
27
|
|
|
public const NAMESPACES = [ |
|
28
|
|
|
'webtheory.saveyour' => "WebTheory\\Saveyour\\Managers", |
|
29
|
|
|
]; |
|
30
|
|
|
|
|
31
|
|
|
public const MANAGERS = [ |
|
32
|
|
|
'callback' => FieldDataManagerCallback::class |
|
33
|
|
|
]; |
|
34
|
|
|
|
|
35
|
|
|
protected const CONVENTION = '%sFieldDataManager'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* |
|
39
|
|
|
*/ |
|
40
|
9 |
|
public function __construct(array $namespaces = [], array $managers = []) |
|
41
|
|
|
{ |
|
42
|
9 |
|
$this->namespaces = $namespaces + static::NAMESPACES; |
|
43
|
9 |
|
$this->managers = $managers + static::MANAGERS; |
|
44
|
9 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Get the value of managers |
|
48
|
|
|
* |
|
49
|
|
|
* @return mixed |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getManagers() |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->managers; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Set the value of managers |
|
58
|
|
|
* |
|
59
|
|
|
* @param mixed $managers |
|
60
|
|
|
* |
|
61
|
|
|
* @return self |
|
62
|
|
|
*/ |
|
63
|
|
|
public function addManager(string $arg, string $manager) |
|
64
|
|
|
{ |
|
65
|
|
|
$this->managers[$arg] = $manager; |
|
66
|
|
|
|
|
67
|
|
|
return $this; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* |
|
72
|
|
|
*/ |
|
73
|
|
|
public function addManagers(array $managers) |
|
74
|
|
|
{ |
|
75
|
|
|
$this->managers = $managers + $this->managers; |
|
76
|
|
|
|
|
77
|
|
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* |
|
82
|
|
|
*/ |
|
83
|
9 |
|
public function create(string $manager, array $args = []): FieldDataManagerInterface |
|
84
|
|
|
{ |
|
85
|
9 |
|
$manager = $this->managers[$manager] ?? null; |
|
86
|
|
|
|
|
87
|
9 |
|
if (isset($manager)) { |
|
88
|
9 |
|
$manager = $this->build($manager, $args); |
|
89
|
|
|
} elseif ($class = $this->getClass($manager)) { |
|
|
|
|
|
|
90
|
|
|
$manager = $this->build($class, $args); |
|
91
|
|
|
} else { |
|
92
|
|
|
throw new Exception("{$manager} is not a recognized data manager"); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
9 |
|
return $manager; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|