Completed
Push — master ( c239db...b86bbe )
by wen
02:42
created

FieldFactory::makeClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
4
namespace Sco\Admin\Fields;
5
6
use Illuminate\Foundation\Application;
7
use Sco\Admin\Contracts\FieldFactory as FieldFactoryContract;
8
use Sco\Admin\Exceptions\BadMethodCallException;
9
10
class FieldFactory implements FieldFactoryContract
11
{
12
    protected $app;
13
14
    protected $aliases = [
15
        'text' => Text::class,
16
    ];
17
18
    public function __construct(Application $app)
19
    {
20
        $this->app = $app;
21
    }
22
23
    public function getAlias($key)
24
    {
25
        return $this->aliases[$key];
26
    }
27
28
    /**
29
     * Check if alias is registered.
30
     *
31
     * @param string $alias
32
     *
33
     * @return bool
34
     */
35
    public function hasAlias($alias)
36
    {
37
        return array_key_exists($alias, $this->aliases);
38
    }
39
40
    /**
41
     * @param string $alias
42
     * @param array $arguments
43
     *
44
     * @return object
45
     */
46
    public function makeClass($alias, array $arguments)
47
    {
48
        $reflection = new \ReflectionClass($this->getAlias($alias));
49
50
        return $reflection->newInstanceArgs($arguments);
51
    }
52
53
    public function __call($method, $parameters)
54
    {
55
        if (!$this->hasAlias($method)) {
56
            throw new BadMethodCallException("Not Found {$method} Field");
57
        }
58
59
        return $this->makeClass($method, $parameters);
60
    }
61
}
62