Fields::__construct()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.9297
c 0
b 0
f 0
cc 6
nc 5
nop 2
1
<?php
2
3
/*
4
 * This file is part of the WPFoundation library.
5
 *
6
 * Copyright (c) 2015-present 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\WPFoundation\PostTypes\Fields;
13
14
use LIN3S\WPFoundation\PostTypes\Fields\Components\FieldComponent;
15
16
/**
17
 * Abstract class of base custom fields that implements the interface.
18
 * This class avoids the redundant task of create the same Fields constructor.
19
 *
20
 * @author Beñat Espiña <[email protected]>
21
 */
22
class Fields implements FieldsInterface
0 ignored issues
show
Deprecated Code introduced by
The interface LIN3S\WPFoundation\PostT...\Fields\FieldsInterface has been deprecated with message: since version 1.7, will be removed in 2.0. Use constructor in Fields

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
23
{
24
    /**
25
     * The template name.
26
     *
27
     * @var string
28
     */
29
    protected $name;
30
31
    /**
32
     * @var array
33
     */
34
    private $components;
35
36
    /**
37
     * @var FieldConnector|null
38
     */
39
    private $connector;
40
41
    /**
42
     * Constructor.
43
     */
44
    public function __construct(
45
        $components = [],
46
        $connector = null
47
    )
48
    {
49
        if ($connector !== null && !$connector instanceof FieldConnector) {
50
            throw new \Exception('Connector must implement FieldConnector');
51
        }
52
53
        if ($connector !== null && $connector instanceof FieldConnector) {
54
            $this->name = $connector->name();
55
        }
56
57
        $this->connector = $connector;
58
        $this->components = $components;
59
60
        $this->fields();
61
        $this->connector();
62
63
        if (false === is_admin()) {
64
            return;
65
        }
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function fields()
72
    {
73
        foreach ($this->components() as $component) {
74
            if (false === class_exists($component)) {
75
                throw new \Exception(sprintf('The %s class does not exist', $component));
76
            }
77
            if ($component instanceof FieldComponent) {
78
                throw new \Exception('The %s class must be extend the FieldComponent', $component);
79
            }
80
            $component::register($this->name, $this->connector());
81
        }
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function components()
88
    {
89
        return $this->components;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function connector()
96
    {
97
        if ($this->connector === null) {
98
            return [];
99
        }
100
101
        return $this->connector->connector();
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function addScreenAttributes()
108
    {
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    public function removeScreenAttributes()
115
    {
116
    }
117
}
118