Passed
Push — master ( a7050c...f62674 )
by Bruno
10:03
created

Metadata   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Test Coverage

Coverage 64.1%

Importance

Changes 0
Metric Value
eloc 52
c 0
b 0
f 0
dl 0
loc 153
rs 10
ccs 25
cts 39
cp 0.641
wmc 19

11 Methods

Rating   Name   Duplication   Size   Complexity  
A appendParameter() 0 4 1
A hasParameter() 0 8 3
A setName() 0 5 1
A toMarkdown() 0 20 2
A __construct() 0 5 1
A toGraphql() 0 18 2
A setComment() 0 5 1
A getName() 0 3 1
A getFromData() 0 9 3
A parameter() 0 8 3
A getComment() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace Formularium;
4
5
use Formularium\Exception\Exception;
6
7
/**
8
 * Class to store information about a validator, datatype, renderable or element
9
 * and its parameters.
10
 */
11
final class Metadata
12
{
13
    /**
14
     * @var string
15
     */
16
    public $name;
17
18
    /**
19
     * @var string
20
     */
21
    public $comment;
22
23
    /**
24
     * @var MetadataParameter[]
25
     */
26 4
    public $args;
27
28 4
    public static function getFromData(string $name = null, array $data) : Metadata
29 4
    {
30 4
        if (!$name) {
31 4
            $name = $data['name'] ?? null;
32
        }
33
        if (!$name) {
34
            throw new Exception("Missing name in fields");
35
        }
36
        return new Metadata($name, $data['comment'] ?? '', $data['args'] ?? []);
37
    }
38
39
    public function __construct(string $name, string $comment, array $args = [])
40
    {
41
        $this->name = $name;
42
        $this->comment = $comment;
43
        $this->args = $args;
44
    }
45
46
    public function appendParameter(MetadataParameter $p): self
47
    {
48
        $this->args[] = $p;
49
        return $this;
50
    }
51
52
    public function toMarkdown(): string
53
    {
54
        $args = array_map(
55
            function (MetadataParameter $a) {
56
                return $a->toMarkdown();
57
            },
58
            $this->args
59
        );
60
61
        $argString = '';
62
        if (!empty($args)) {
63 2
            $argString = join("\n", $args);
64
        }
65 2
66
        return <<<EOF
67 2
## {$this->name}
68 2
69 2
{$this->comment}
70
71
$argString
72 2
73 2
EOF;
74 2
    }
75
76
    public function toGraphql(): string
77
    {
78 2
        $args = array_map(
79
            function (MetadataParameter $a) {
80 2
                return $a->toGraphql();
81
            },
82
            $this->args
83
        );
84
85 1
        $argString = '';
86
        if (!empty($args)) {
87 1
            $argString = "(" . join("\n", $args) . "\n)";
88 1
        }
89 1
        return <<< EOF
90
"""
91
{$this->comment}
92 1
"""
93
directive @{$this->name}{$argString} on FIELD_DEFINITION
94
95 1
EOF;
96
    }
97 1
98 1
    public function hasParameter(string $name): bool
99 1
    {
100
        foreach ($this->args as $a) {
101
            if ($a->name === $name) {
102 1
                return true;
103
            }
104
        }
105
        return false;
106
    }
107
108
    public function parameter(string $name): ?MetadataParameter
109
    {
110
        foreach ($this->args as $a) {
111
            if ($a->name === $name) {
112
                return $a;
113
            }
114
        }
115
        return null;
116
    }
117
118
    /**
119
     * Get the value of name
120
     *
121
     * @return  string
122
     */
123
    public function getName()
124
    {
125
        return $this->name;
126
    }
127
128
    /**
129
     * Set the value of name
130
     *
131
     * @param  string  $name
132
     *
133
     * @return  self
134
     */
135
    public function setName(string $name)
136
    {
137
        $this->name = $name;
138
139
        return $this;
140
    }
141
142
    /**
143
     * Get the value of comment
144
     *
145
     * @return  string
146
     */
147
    public function getComment()
148
    {
149
        return $this->comment;
150
    }
151
152
    /**
153
     * Set the value of comment
154
     *
155
     * @param  string  $comment
156
     *
157
     * @return  self
158
     */
159
    public function setComment(string $comment)
160
    {
161
        $this->comment = $comment;
162
163
        return $this;
164
    }
165
}
166