CustomMessagesTrait::makeMessage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Vvval\Spiral\Validation\Checkers\Traits;
4
5
trait CustomMessagesTrait
6
{
7
    /**
8
     * Return custom error message or a default one.
9
     *
10
     * @param string|null $message
11
     * @param string      $method
12
     *
13
     * @return string
14
     */
15
    protected function makeMessage(string $message = null, string $method): string
16
    {
17
        return $message ?? $this->getMessage($this->getMethod($method));
18
    }
19
20
    /**
21
     * Fetch method name.
22
     *
23
     * @param string $method
24
     *
25
     * @return string
26
     */
27
    protected function getMethod(string $method): string
28
    {
29
        if (mb_stripos($method, '::') === false) {
30
            return $method;
31
        }
32
33
        $arr = explode('::', $method);
34
35
        return $arr[1];
36
    }
37
38
    /**
39
     * Return error message for checker.
40
     *
41
     * @param string $method
42
     *
43
     * @see \Spiral\Validation\CheckerInterface::getMessage()
44
     *
45
     * @return string
46
     */
47
    abstract public function getMessage(string $method): string;
48
}