More tidying.
[yaffs-website] / vendor / consolidation / annotated-command / src / Parser / CommandInfoDeserializer.php
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  * Deserialize a CommandInfo object
11  */
12 class CommandInfoDeserializer
13 {
14     // TODO: in a future version, move CommandInfo::deserialize here
15     public function deserialize($data)
16     {
17         return CommandInfo::deserialize((array)$data);
18     }
19
20     protected static function cachedMethodExists($cache)
21     {
22         return method_exists($cache['class'], $cache['method_name']);
23     }
24
25     public static function isValidSerializedData($cache)
26     {
27         return
28             isset($cache['schema']) &&
29             isset($cache['method_name']) &&
30             isset($cache['mtime']) &&
31             ($cache['schema'] > 0) &&
32             ($cache['schema'] <= CommandInfo::SERIALIZATION_SCHEMA_VERSION) &&
33             self::cachedMethodExists($cache);
34     }
35
36     public function constructFromCache(CommandInfo $commandInfo, $info_array)
37     {
38         $info_array += $this->defaultSerializationData();
39
40         $commandInfo
41             ->setName($info_array['name'])
42             ->replaceRawAnnotations($info_array['annotations'])
43             ->setAliases($info_array['aliases'])
44             ->setHelp($info_array['help'])
45             ->setDescription($info_array['description'])
46             ->replaceExampleUsages($info_array['example_usages'])
47             ->setReturnType($info_array['return_type'])
48             ;
49
50         $this->constructDefaultsWithDescriptions($commandInfo->arguments(), (array)$info_array['arguments']);
51         $this->constructDefaultsWithDescriptions($commandInfo->options(), (array)$info_array['options']);
52     }
53
54     protected function constructDefaultsWithDescriptions(DefaultsWithDescriptions $defaults, $data)
55     {
56         foreach ($data as $key => $info) {
57             $info = (array)$info;
58             $defaults->add($key, $info['description']);
59             if (array_key_exists('default', $info)) {
60                 $defaults->setDefaultValue($key, $info['default']);
61             }
62         }
63     }
64
65
66     /**
67      * Default data. Everything should be provided during serialization;
68      * this is just as a fallback for unusual circumstances.
69      * @return array
70      */
71     protected function defaultSerializationData()
72     {
73         return [
74             'name' => '',
75             'description' => '',
76             'help' => '',
77             'aliases' => [],
78             'annotations' => [],
79             'example_usages' => [],
80             'return_type' => [],
81             'parameters' => [],
82             'arguments' => [],
83             'options' => [],
84             'mtime' => 0,
85         ];
86     }
87 }