Completed
Push — master ( 920308...e24c09 )
by Marcel
03:01
created

RuleDescriptionParser   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 9
c 3
b 2
f 0
lcom 1
cbo 0
dl 0
loc 64
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getDescription() 0 8 3
A with() 0 6 2
A translateWithAttributes() 0 10 2
A parse() 0 4 1
1
<?php
2
3
namespace Mpociot\ApiDoc\Parsers;
4
5
class RuleDescriptionParser
6
{
7
    private $rule;
8
9
    private $parameters = [];
10
11
    /**
12
     * @param null $rule
13
     */
14
    public function __construct($rule = null)
15
    {
16
        $this->rule = $rule;
17
    }
18
19
    /**
20
     * @return array|string
21
     */
22
    public function getDescription()
23
    {
24
        $key = "apidoc::rules.{$this->rule}";
25
26
        $description = $this->parameters ? $this->translateWithAttributes($key) : trans($key);
27
28
        return $description != $key ? $description : [];
29
    }
30
31
    /**
32
     * @param string|array $parameters
33
     *
34
     * @return $this
35
     */
36
    public function with($parameters)
37
    {
38
        is_array($parameters) ? $this->parameters += $parameters : $this->parameters[] = $parameters;
39
40
        return $this;
41
    }
42
43
    /**
44
     * @param $key
45
     *
46
     * @return string
47
     */
48
    protected function translateWithAttributes($key)
49
    {
50
        $translate = trans($key);
51
52
        foreach ($this->parameters as $parameter) {
53
            $translate = preg_replace('/:attribute/', $parameter, $translate, 1);
54
        }
55
56
        return $translate;
57
    }
58
59
    /**
60
     * @param null $rule
61
     *
62
     * @return static
63
     */
64
    public static function parse($rule = null)
65
    {
66
        return new static($rule);
67
    }
68
}
69