Passed
Push — master ( ed42e4...b3b06a )
by Kirill
04:44
created

HelpersTrait::argument()   A

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
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Console\Traits;
13
14
use Symfony\Component\Console\Helper\Table;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
/**
18
 * Trait expect command to set $output and $input scopes.
19
 */
20
trait HelpersTrait
21
{
22
    /**
23
     * OutputInterface is the interface implemented by all Output classes. Only exists when command
24
     * are being executed.
25
     *
26
     * @var \Symfony\Component\Console\Output\OutputInterface
27
     */
28
    protected $output = null;
29
30
    /**
31
     * InputInterface is the interface implemented by all input classes. Only exists when command
32
     * are being executed.
33
     *
34
     * @var \Symfony\Component\Console\Input\InputInterface
35
     */
36
    protected $input = null;
37
38
    /**
39
     * Check if verbosity level of output is higher or equal to VERBOSITY_VERBOSE.
40
     *
41
     * @return bool
42
     */
43
    protected function isVerbose(): bool
44
    {
45
        return $this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE;
46
    }
47
48
    /**
49
     * Input option.
50
     *
51
     * @param string $name
52
     * @return mixed
53
     */
54
    protected function option(string $name)
55
    {
56
        return $this->input->getOption($name);
57
    }
58
59
    /**
60
     * Input argument.
61
     *
62
     * @param string $name
63
     * @return mixed
64
     */
65
    protected function argument(string $name)
66
    {
67
        return $this->input->getArgument($name);
68
    }
69
70
    /**
71
     * Identical to write function but provides ability to format message. Does not add new line.
72
     *
73
     * @param string $format
74
     * @param array  ...$args
75
     */
76
    protected function sprintf(string $format, ...$args)
77
    {
78
        return $this->output->write(sprintf($format, ...$args), false);
79
    }
80
81
    /**
82
     * Writes a message to the output.
83
     *
84
     * @param string|array $messages The message as an array of lines or a single string
85
     * @param bool         $newline  Whether to add a newline
86
     *
87
     * @throws \InvalidArgumentException When unknown output type is given
88
     */
89
    protected function write($messages, bool $newline = false)
90
    {
91
        return $this->output->write($messages, $newline);
92
    }
93
94
    /**
95
     * Writes a message to the output and adds a newline at the end.
96
     *
97
     * @param string|array $messages The message as an array of lines of a single string
98
     *
99
     * @throws \InvalidArgumentException When unknown output type is given
100
     */
101
    protected function writeln($messages)
102
    {
103
        return $this->output->writeln($messages);
104
    }
105
106
    /**
107
     * Table helper instance with configured header and pre-defined set of rows.
108
     *
109
     * @param array  $headers
110
     * @param array  $rows
111
     * @param string $style
112
     * @return Table
113
     */
114
    protected function table(array $headers, array $rows = [], string $style = 'default'): Table
115
    {
116
        $table = new Table($this->output);
117
118
        return $table->setHeaders($headers)->setRows($rows)->setStyle($style);
119
    }
120
}
121