Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / symfony / console / Helper / DescriptorHelper.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Console\Helper;
13
14 use Symfony\Component\Console\Descriptor\DescriptorInterface;
15 use Symfony\Component\Console\Descriptor\JsonDescriptor;
16 use Symfony\Component\Console\Descriptor\MarkdownDescriptor;
17 use Symfony\Component\Console\Descriptor\TextDescriptor;
18 use Symfony\Component\Console\Descriptor\XmlDescriptor;
19 use Symfony\Component\Console\Exception\InvalidArgumentException;
20 use Symfony\Component\Console\Output\OutputInterface;
21
22 /**
23  * This class adds helper method to describe objects in various formats.
24  *
25  * @author Jean-François Simon <contact@jfsimon.fr>
26  */
27 class DescriptorHelper extends Helper
28 {
29     /**
30      * @var DescriptorInterface[]
31      */
32     private $descriptors = array();
33
34     public function __construct()
35     {
36         $this
37             ->register('txt', new TextDescriptor())
38             ->register('xml', new XmlDescriptor())
39             ->register('json', new JsonDescriptor())
40             ->register('md', new MarkdownDescriptor())
41         ;
42     }
43
44     /**
45      * Describes an object if supported.
46      *
47      * Available options are:
48      * * format: string, the output format name
49      * * raw_text: boolean, sets output type as raw
50      *
51      * @param OutputInterface $output
52      * @param object          $object
53      * @param array           $options
54      *
55      * @throws InvalidArgumentException when the given format is not supported
56      */
57     public function describe(OutputInterface $output, $object, array $options = array())
58     {
59         $options = array_merge(array(
60             'raw_text' => false,
61             'format' => 'txt',
62         ), $options);
63
64         if (!isset($this->descriptors[$options['format']])) {
65             throw new InvalidArgumentException(sprintf('Unsupported format "%s".', $options['format']));
66         }
67
68         $descriptor = $this->descriptors[$options['format']];
69         $descriptor->describe($output, $object, $options);
70     }
71
72     /**
73      * Registers a descriptor.
74      *
75      * @param string              $format
76      * @param DescriptorInterface $descriptor
77      *
78      * @return $this
79      */
80     public function register($format, DescriptorInterface $descriptor)
81     {
82         $this->descriptors[$format] = $descriptor;
83
84         return $this;
85     }
86
87     /**
88      * {@inheritdoc}
89      */
90     public function getName()
91     {
92         return 'descriptor';
93     }
94 }