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

RuleDescriptionParser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 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