Completed
Push — master ( 35235e...bb4db2 )
by Greg
01:14
created

OpCommands::filterOutput()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace Consolidation\Filter\Cli;
4
5
use Consolidation\AnnotatedCommand\CommandData;
6
use Consolidation\Filter\LogicalOpFactory;
7
use Consolidation\Filter\FilterOutputData;
8
use Symfony\Component\Yaml\Yaml;
9
10
class OpCommands extends \Robo\Tasks
11
{
12
    /**
13
     * Test the expression parser
14
     *
15
     * @command parse
16
     * @return array
17
     */
18
    public function parse($expr, $options = ['format' => 'yaml', 'dump' => false])
19
    {
20
        $factory = LogicalOpFactory::get();
21
        $op = $factory->evaluate($expr);
22
23
        $result = (string)$op;
24
25
        if ($options['dump']) {
26
            $result = var_export($op, true) . "\n$result";
27
        }
28
29
        return $result;
30
    }
31
32
    /**
33
     * Convert a command from one format to another, potentially with filtering.
34
     *
35
     * @command edit
36
     * @aliases ed
37
     * @filter-output
38
     * @return array
39
     */
40
    public function edit($data, $options = ['format' => 'yaml', 'in' => 'auto'])
41
    {
42
        return $this->read($data, $options['in']);
43
    }
44
45
    /**
46
     * Read the data provided to this command.
47
     */
48
    protected function read($data, $in)
49
    {
50
        // If our input spec is '-' then read from stdin
51
        if ($data == '-') {
52
            $data = 'php://stdin';
53
        }
54
        // If our input spec is a file or a url then read it. Otherwise
55
        // we'll presume the data was provided directly on the command line.
56
        if (file_exists($data) || preg_match('#^[a-z]*://#', $data)) {
57
            $data = file_get_contents($data);
58
        }
59
60
        return $this->parseData($data, $in);
61
    }
62
63
    /**
64
     * Convert our provided input data to a php array.
65
     */
66
    protected function parseData($data, $in)
67
    {
68
        $in = $this->inferInputFormat($data, $in);
69
70
        switch ($in) {
71
            case 'json':
72
                return json_decode($data, true);
73
74
            case 'yaml':
75
            default:
76
                return (array) Yaml::parse($data);
77
        }
78
    }
79
80
    /**
81
     * If the data type is 'auto', then try to infer what data type
82
     * we should parse as based on the contents of the data.
83
     */
84
    protected function inferInputFormat($data, $in)
85
    {
86
        // If the user explicitly set the data type then use that
87
        if ($in != 'auto') {
88
            return $in;
89
        }
90
91
        // If data begins with '{' then presume it is json
92
        if ($data[0] == '{') {
93
            return 'json';
94
        }
95
96
        // We don't know what the data type should be.
97
        return $in;
98
    }
99
}
100