Completed
Push — master ( b86bbe...5e765d )
by wen
02:51
created

ElementFactory::getAlias()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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