Completed
Pull Request — master (#85)
by Greg
02:10
created

CommandInfoSerializer::serialize()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 1
eloc 19
nc 1
nop 1
1
<?php
2
namespace Consolidation\AnnotatedCommand\Parser;
3
4
use Symfony\Component\Console\Input\InputOption;
5
use Consolidation\AnnotatedCommand\Parser\Internal\CommandDocBlockParser;
6
use Consolidation\AnnotatedCommand\Parser\Internal\CommandDocBlockParserFactory;
7
use Consolidation\AnnotatedCommand\AnnotationData;
8
9
/**
10
 * Serialize a CommandInfo object
11
 */
12
class CommandInfoSerializer
13
{
14
    public function serialize(CommandInfo $commandInfo)
15
    {
16
        $allAnnotations = $commandInfo->getAnnotations();
17
        $path = $allAnnotations['_path'];
18
        $className = $allAnnotations['_classname'];
19
20
        $info = [
21
            'schema' => CommandInfo::SERIALIZATION_SCHEMA_VERSION,
22
            'class' => $className,
23
            'method_name' => $commandInfo->getMethodName(),
24
            'name' => $commandInfo->getName(),
25
            'description' => $commandInfo->getDescription(),
26
            'help' => $commandInfo->getHelp(),
27
            'aliases' => $commandInfo->getAliases(),
28
            'annotations' => $commandInfo->getRawAnnotations()->getArrayCopy(),
29
            'example_usages' => $commandInfo->getExampleUsages(),
30
            'return_type' => $commandInfo->getReturnType(),
31
            'mtime' => filemtime($path),
32
        ];
33
        $info['arguments'] = $this->serializeDefaultsWithDescriptions($commandInfo->arguments());
34
        $info['options'] = $this->serializeDefaultsWithDescriptions($commandInfo->options());
35
36
        return $info;
37
    }
38
39
    protected function serializeDefaultsWithDescriptions(DefaultsWithDescriptions $defaults)
40
    {
41
        $result = [];
42
        foreach ($defaults->getValues() as $key => $val) {
43
            $result[$key] = [
44
                'description' => $defaults->getDescription($key),
45
            ];
46
            if ($defaults->hasDefault($key)) {
47
                $result[$key]['default'] = $val;
48
            }
49
        }
50
        return $result;
51
    }
52
}
53