Param   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
eloc 34
c 1
b 0
f 0
dl 0
loc 97
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setLocale() 0 5 1
A __construct() 0 7 1
A addAliase() 0 8 2
B parse() 0 36 9
1
<?php
2
3
namespace ConsoleArgs;
4
5
/**
6
 * Объект параметров
7
 * Отвечает за объявление параметров команд
8
 */
9
class Param implements Parameter
10
{
11
    public $names;
12
    public $defaultValue;
13
    public $required;
14
    protected $locale;
15
16
    /**
17
     * Конструктор
18
     * 
19
     * @param string $name - имя парамтера
20
     * [@param string $defaultValue = null] - значение параметра по умолчанию
21
     * [@param bool $required = false] - обязательно ли указание параметра
22
     */
23
    public function __construct (string $name, string $defaultValue = null, bool $required = false)
24
    {
25
        $this->names        = [$name];
26
        $this->defaultValue = $defaultValue;
27
        $this->required     = $required;
28
29
        $this->locale = new Locale;
30
    }
31
32
    /**
33
     * Установка локализации
34
     * 
35
     * @param Locale $locale - объект локализации
36
     * 
37
     * @return Param - возвращает сам себя
38
     */
39
    public function setLocale (Locale $locale): Param
40
    {
41
        $this->locale = $locale;
42
43
        return $this;
44
    }
45
46
    /**
47
     * Добавление алиаса
48
     * 
49
     * @param string $name - алиас для добавления
50
     * 
51
     * @return Param - возвращает сам себя
52
     */
53
    public function addAliase (string $name)
54
    {
55
        if (array_search ($name, $this->names) !== false)
56
            throw new \Exception ($this->locale->aliase_exists_exception);
57
58
        $this->names[] = $name;
59
60
        return $this;
61
    }
62
63
    /**
64
     * Парсер параметров
65
     * 
66
     * @param array &$args - массив аргументов для парсинга
67
     * 
68
     * Возвращает найденый параметр или массив найдёных параметров, если их было указано несколько
69
     */
70
    public function parse (array &$args)
71
    {
72
        $args = array_values ($args);
73
74
        foreach ($this->names as $name)
75
            if (($key = array_search ($name, $args)) !== false)
76
            {
77
                if (!isset ($args[$key + 1]))
78
                    throw new \Exception ($this->locale->unselected_value_exception);
79
80
                $param = [$args[$key + 1]];
81
82
                unset ($args[$key], $args[$key + 1]);
83
                $args = array_values ($args);
84
85
                try
86
                {
87
                    while (($altParam = $this->parse ($args)) !== $this->defaultValue)
88
                    {
89
                        if (is_array ($altParam))
90
                            $param = array_merge ($param, $altParam);
91
92
                        else $param[] = $altParam;
93
                    }
94
                }
95
96
                catch (\Throwable $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
97
                
98
                return sizeof ($param) == 1 ?
99
                    $param[0] : $param;
100
            }
101
102
        if ($this->required)
103
            throw new \Exception (str_replace ('%param_name%', current ($this->names), $this->locale->undefined_param_exception));
104
105
        return $this->defaultValue;
106
    }
107
}
108