Completed
Push — master ( 2fcfdc...9f7c01 )
by De Cramer
11s
created

Factory::__call()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.0218

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 8
cts 9
cp 0.8889
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 4
nop 2
crap 4.0218
1
<?php
2
3
namespace eXpansion\Framework\Gui\Ui;
4
5
use eXpansion\Framework\Core\Model\Gui\Factory\BackGroundFactory;
6
use eXpansion\Framework\Gui\Components\uiAnimation;
7
use eXpansion\Framework\Gui\Components\uiButton;
8
use eXpansion\Framework\Gui\Components\uiCheckbox;
9
use eXpansion\Framework\Gui\Components\uiConfirmButton;
10
use eXpansion\Framework\Gui\Components\uiDropdown;
11
use eXpansion\Framework\Gui\Components\uiInput;
12
use eXpansion\Framework\Gui\Components\uiInputMasked;
13
use eXpansion\Framework\Gui\Components\uiLabel;
14
use eXpansion\Framework\Gui\Components\uiLine;
15
use eXpansion\Framework\Gui\Components\uiTextbox;
16
use eXpansion\Framework\Gui\Components\uiTooltip;
17
use eXpansion\Framework\Gui\Layouts\layoutLine;
18
use eXpansion\Framework\Gui\Layouts\layoutRow;
19
20
use eXpansion\Framework\Core\Exceptions\UnknownMethodException;
21
use eXpansion\Framework\Gui\Layouts\layoutScrollable;
22
use FML\Controls\Frame;
23
24
/**
25
 * Class Factory
26
 *
27
 * @author    de Cramer Oliver<[email protected]>
28
 * @copyright 2017 eXpansion
29
 * @package eXpansion\Framework\Gui\Ui
30
 *
31
 * @method uiButton createButton($text, $type = UiButton::TYPE_DEFAULT)
32
 * @method uiConfirmButton createConfirmButton($text, $type = UiButton::TYPE_DEFAULT)
33
 * @method uiCheckbox createCheckbox($text, $name, $checked = false, $disabled = false)
34
 * @method uiDropdown createDropdown($name, $options, $selectedIndex = -1, $isOpened = false)
35
 * @method uiInput createInput($name, $default = "", $width = 30)
36
 * @method uiInputMasked createInputMasked($name, $default = "", $width = 30)
37
 * @method uiLabel createLabel($text = "", $type = uiLabel::TYPE_NORMAL, $controlId = null)
38
 * @method uiLine createLine($x, $y)
39
 * @method uiTextbox createTextbox($name, $default = "", $lines = 1, $width = 30)
40
 * @method uiTooltip createTooltip()
41
 * @method uiAnimation createAnimation()
42
 *
43
 * @method layoutLine createLayoutLine($startX, $startY, $elements = [], $margin = 0.);
44
 * @method layoutRow createLayoutRow($startX, $startY, $elements = [], $margin = 0.);
45
 * @method layoutScrollable createLayoutScrollable($frame, $sizeX, $sizeY);
46
 *
47
 */
48
class Factory
49
{
50
    /**
51
     * @var string[]
52
     */
53
    protected $classes;
54
55
    /**
56
     * @var BackGroundFactory[]
57
     */
58
    protected $factories;
59
60
    /**
61
     * Factory constructor.
62
     *
63
     * @param $classes
64
     * @param $factories
65
     */
66 9
    public function __construct($classes, $factories = null)
67
    {
68 9
        $this->classes = $classes;
69 9
        $this->factories = $factories;
70 9
    }
71
72
    /**
73
     * @param $name
74
     * @param $arguments
75
     *
76
     * @return mixed
77
     *
78
     * @throws UnknownMethodException
79
     */
80 9
    public function __call($name, $arguments)
81
    {
82 9
        if (strpos($name, 'create') === 0) {
83 8
            $name = str_replace('create', '', $name);
84
85 8
            if (isset($this->classes[$name])) {
86 7
                $class = $this->classes[$name];
87
88 7
                return new $class(...$arguments);
89
            }
90
91 1
            if (isset($this->factories[$name])) {
92
                return $this->factories[$name]->create(...$arguments);
93
            }
94
        }
95
96 2
        throw new UnknownMethodException("$name is an unknown method");
97
    }
98
}
99