Flag::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
nc 1
nop 1
cc 1
1
<?php
2
3
namespace ConsoleArgs;
4
5
/**
6
 * Объект флагов
7
 * Отвечает за создание флагов для команд
8
 */
9
class Flag implements Parameter
10
{
11
    public $names;
12
    protected $locale;
13
14
    /**
15
     * Конструктор
16
     * 
17
     * @param string $name - имя флага
18
     */
19
    public function __construct (string $name)
20
    {
21
        $this->names = [$name];
22
    }
23
24
    /**
25
     * Установка локализации
26
     * 
27
     * @param Locale $locale - объект локализации
28
     * 
29
     * @return Flag - возвращает сам себя
30
     */
31
    public function setLocale (Locale $locale): Param
32
    {
33
        $this->locale = $locale;
34
35
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type ConsoleArgs\Flag which is incompatible with the type-hinted return ConsoleArgs\Param.
Loading history...
36
    }
37
38
    /**
39
     * Добавление алиаса
40
     * 
41
     * @param string $name - алиас для добавления
42
     * 
43
     * @return Flag - возвращает сам себя
44
     */
45
    public function addAliase (string $name)
46
    {
47
        if (array_search ($name, $this->names) !== false)
48
            throw new \Exception ($this->locale->aliase_exists_exception);
49
50
        $this->names[] = $name;
51
52
        return $this;
53
    }
54
55
    /**
56
     * Парсер флагов
57
     * 
58
     * @param array &$args - массив аргументов для парсинга
59
     * 
60
     * Возвращает состояние флага
61
     */
62
    public function parse (array &$args)
63
    {
64
        $args = array_values ($args);
65
66
        foreach ($this->names as $name)
67
            if (($key = array_search ($name, $args)) !== false)
68
            {
69
                unset ($args[$key]);
70
                $args = array_values ($args);
71
72
                while ($this->parse ($args) !== false);
73
                
74
                return true;
75
            }
76
77
        return false;
78
    }
79
}
80