CustomMessagesTrait   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 0
dl 0
loc 44
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A makeMessage() 0 4 1
A getMethod() 0 10 2
getMessage() 0 1 ?
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
}