Passed
Pull Request — 3.x (#71)
by Hari
06:16
created

FormFactory::newInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * 
4
 * This file is part of the Aura project for PHP.
5
 * 
6
 * @package Aura.Input
7
 * 
8
 * @license http://opensource.org/licenses/MIT-license.php MIT
9
 * 
10
 */
11
namespace Aura\Input;
12
13
use Aura\Input\Exception;
14
15
/**
16
 * 
17
 * A factory to create top-level form objects by name.
18
 * 
19
 * @package Aura.Input
20
 * 
21
 */
22
class FormFactory
23
{
24
    /**
25
     * 
26
     * A map of form names to factory callables.
27
     * 
28
     * @var array
29
     * 
30
     */
31
    protected $map = [];
32
33
    /**
34
     * 
35
     * Constructor.
36
     * 
37
     * @param array $map A map of form names to factory callables.
38
     * 
39
     */
40
    public function __construct($map = [])
41
    {
42
        $this->map = $map;
43
    }
44
45
    /**
46
     *
47
     * Returns a new instance of a named form.
48
     *
49
     * @param string $name The name of the form to create.
50
     *
51
     * @param mixed $options
52
     *
53
     * @return Form
54
     *
55
     * @throws Exception\NoSuchForm When the named form does not exist.
56
     */
57
    public function newInstance($name, $options = null)
58
    {
59
        if (! isset($this->map[$name])) {
60
            throw new Exception\NoSuchForm($name);
61
        }
62
        
63
        $factory = $this->map[$name];
64
        $form = $factory($options);
65
        return $form;
66
    }
67
}
68