Completed
Push — master ( a67e7e...95640a )
by Tomasz
02:42
created

FieldMessage::merge()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Hop\Validator\Message;
6
7
final class FieldMessage implements MessageInterface
8
{
9
    /**
10
     * @var string[]
11
     */
12
    private $messages = [];
13
14
    /**
15
     * @param string $code
16
     * @param string $name
17
     * @return FieldMessage
18
     */
19
    public function attachMessage(string $code, string $name): self
20
    {
21
        $this->messages[$code] = $name;
22
        return $this;
23
    }
24
25
    /**
26
     * @inheritdoc
27
     */
28
    public function toArray(): array
29
    {
30
        return $this->messages;
31
    }
32
33
    /**
34
     * @return int
35
     */
36
    public function count(): int
37
    {
38
        return \count($this->messages);
39
    }
40
41
    public function merge(MessageInterface $message): MessageInterface
42
    {
43
        $messageContainer = clone $this;
44
        if (!$message instanceof static) {
45
            throw new \InvalidArgumentException('Message must be a field message');
46
        }
47
        foreach ($message->toArray() as $error => $text) {
48
            $messageContainer->attachMessage($error, $text);
49
        }
50
        return $messageContainer;
51
    }
52
}
53