Yaffs site version 1.1
[yaffs-website] / vendor / symfony / console / Helper / Helper.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\Formatter\OutputFormatterInterface;
15
16 /**
17  * Helper is the base class for all helper classes.
18  *
19  * @author Fabien Potencier <fabien@symfony.com>
20  */
21 abstract class Helper implements HelperInterface
22 {
23     protected $helperSet = null;
24
25     /**
26      * Sets the helper set associated with this helper.
27      *
28      * @param HelperSet $helperSet A HelperSet instance
29      */
30     public function setHelperSet(HelperSet $helperSet = null)
31     {
32         $this->helperSet = $helperSet;
33     }
34
35     /**
36      * Gets the helper set associated with this helper.
37      *
38      * @return HelperSet|null
39      */
40     public function getHelperSet()
41     {
42         return $this->helperSet;
43     }
44
45     /**
46      * Returns the length of a string, using mb_strwidth if it is available.
47      *
48      * @param string $string The string to check its length
49      *
50      * @return int The length of the string
51      */
52     public static function strlen($string)
53     {
54         if (false === $encoding = mb_detect_encoding($string, null, true)) {
55             return strlen($string);
56         }
57
58         return mb_strwidth($string, $encoding);
59     }
60
61     public static function formatTime($secs)
62     {
63         static $timeFormats = array(
64             array(0, '< 1 sec'),
65             array(1, '1 sec'),
66             array(2, 'secs', 1),
67             array(60, '1 min'),
68             array(120, 'mins', 60),
69             array(3600, '1 hr'),
70             array(7200, 'hrs', 3600),
71             array(86400, '1 day'),
72             array(172800, 'days', 86400),
73         );
74
75         foreach ($timeFormats as $index => $format) {
76             if ($secs >= $format[0]) {
77                 if ((isset($timeFormats[$index + 1]) && $secs < $timeFormats[$index + 1][0])
78                     || $index == count($timeFormats) - 1
79                 ) {
80                     if (2 == count($format)) {
81                         return $format[1];
82                     }
83
84                     return floor($secs / $format[2]).' '.$format[1];
85                 }
86             }
87         }
88     }
89
90     public static function formatMemory($memory)
91     {
92         if ($memory >= 1024 * 1024 * 1024) {
93             return sprintf('%.1f GiB', $memory / 1024 / 1024 / 1024);
94         }
95
96         if ($memory >= 1024 * 1024) {
97             return sprintf('%.1f MiB', $memory / 1024 / 1024);
98         }
99
100         if ($memory >= 1024) {
101             return sprintf('%d KiB', $memory / 1024);
102         }
103
104         return sprintf('%d B', $memory);
105     }
106
107     public static function strlenWithoutDecoration(OutputFormatterInterface $formatter, $string)
108     {
109         return self::strlen(self::removeDecoration($formatter, $string));
110     }
111
112     public static function removeDecoration(OutputFormatterInterface $formatter, $string)
113     {
114         $isDecorated = $formatter->isDecorated();
115         $formatter->setDecorated(false);
116         // remove <...> formatting
117         $string = $formatter->format($string);
118         // remove already formatted characters
119         $string = preg_replace("/\033\[[^m]*m/", '', $string);
120         $formatter->setDecorated($isDecorated);
121
122         return $string;
123     }
124 }