FormWrapperRegistry::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 3
Metric Value
c 5
b 0
f 3
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the WPSymfonyForm plugin.
5
 *
6
 * Copyright (c) 2015-2016 LIN3S <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace LIN3S\WPSymfonyForm\Registry;
13
14
use LIN3S\WPSymfonyForm\Wrapper\FormWrapper;
15
16
/**
17
 * FormWrapperRegistry class.
18
 *
19
 * @author Gorka Laucirica <[email protected]>
20
 */
21
class FormWrapperRegistry
22
{
23
    /**
24
     * Collection of form wrappers.
25
     *
26
     * @var FormWrapper[]
27
     */
28
    private $formWrappers;
29
30
    /**
31
     * Initializes the registry with an array of wrappers.
32
     *
33
     * @param FormWrapper[] $formWrappers The form wrappers collection
34
     */
35
    public function __construct(array $formWrappers = [])
36
    {
37
        $this->formWrappers = array_map(function (FormWrapper $formWrapper) {
38
            return $formWrapper;
39
        }, $formWrappers);
40
    }
41
42
    /**
43
     * Gets a form wrapper by form name.
44
     *
45
     * @param string $formName The form name
46
     *
47
     * @throw \InvalidArgumentException if wrapper not found
48
     *
49
     * @return FormWrapper
50
     */
51
    public function get($formName)
52
    {
53
        foreach ($this->formWrappers as $wrapper) {
54
            if ($wrapper->getName() === $formName) {
55
                return $wrapper;
56
            }
57
        }
58
        throw new \InvalidArgumentException(
59
            sprintf(
60
                'Form with name %s not found in FormWrapperRegistry',
61
                $formName
62
            )
63
        );
64
    }
65
66
    /**
67
     * Adds a form wrapper to the registry.
68
     *
69
     * @param FormWrapper $formWrapper The form wrapper
70
     */
71
    public function add(FormWrapper $formWrapper)
72
    {
73
        $this->formWrappers[] = $formWrapper;
74
    }
75
}
76