@@ -5,118 +5,118 @@ |
||
5 | 5 | * Numeric validates and converts mixed types to numeric types. |
6 | 6 | */ |
7 | 7 | class Numeric { |
8 | - /** |
|
9 | - * Ensure integer or or float value by safely converting. |
|
10 | - * |
|
11 | - * Safe conversions to int or float: |
|
12 | - * |
|
13 | - * ``` |
|
14 | - * '-1' => -1 |
|
15 | - * '+00.0' => 0.0 |
|
16 | - * '1' => 1 |
|
17 | - * ' 1 ' => 1 |
|
18 | - * '01' => 1 |
|
19 | - * '0.1' => 0.1 |
|
20 | - * '.1' => 0.1 |
|
21 | - * ``` |
|
22 | - * |
|
23 | - * Invalid conversions: |
|
24 | - * |
|
25 | - * ``` |
|
26 | - * 'x' |
|
27 | - * '0a' |
|
28 | - * '' |
|
29 | - * '-' |
|
30 | - * ' ' |
|
31 | - * '0+1' |
|
32 | - * '.' |
|
33 | - * ',' |
|
34 | - * ``` |
|
35 | - * |
|
36 | - * @param mixed $value |
|
37 | - * |
|
38 | - * @throws \InvalidArgumentException If the value could not be safely converted to int or float. |
|
39 | - * |
|
40 | - * @return int|float |
|
41 | - */ |
|
42 | - public static function ensure($value) { |
|
43 | - if (is_int($value) || is_float($value)) { |
|
44 | - return $value; |
|
45 | - } |
|
46 | - $value = trim($value); |
|
47 | - if (is_numeric($value)) { |
|
48 | - // Results in either int or float |
|
49 | - return $value + 0; |
|
50 | - } |
|
51 | - throw new \InvalidArgumentException( |
|
52 | - sprintf( |
|
53 | - 'Expecting value of type int, float or compatible, got variable of type %s instead.', |
|
54 | - Type::summarize($value) |
|
55 | - ) |
|
56 | - ); |
|
57 | - } |
|
8 | + /** |
|
9 | + * Ensure integer or or float value by safely converting. |
|
10 | + * |
|
11 | + * Safe conversions to int or float: |
|
12 | + * |
|
13 | + * ``` |
|
14 | + * '-1' => -1 |
|
15 | + * '+00.0' => 0.0 |
|
16 | + * '1' => 1 |
|
17 | + * ' 1 ' => 1 |
|
18 | + * '01' => 1 |
|
19 | + * '0.1' => 0.1 |
|
20 | + * '.1' => 0.1 |
|
21 | + * ``` |
|
22 | + * |
|
23 | + * Invalid conversions: |
|
24 | + * |
|
25 | + * ``` |
|
26 | + * 'x' |
|
27 | + * '0a' |
|
28 | + * '' |
|
29 | + * '-' |
|
30 | + * ' ' |
|
31 | + * '0+1' |
|
32 | + * '.' |
|
33 | + * ',' |
|
34 | + * ``` |
|
35 | + * |
|
36 | + * @param mixed $value |
|
37 | + * |
|
38 | + * @throws \InvalidArgumentException If the value could not be safely converted to int or float. |
|
39 | + * |
|
40 | + * @return int|float |
|
41 | + */ |
|
42 | + public static function ensure($value) { |
|
43 | + if (is_int($value) || is_float($value)) { |
|
44 | + return $value; |
|
45 | + } |
|
46 | + $value = trim($value); |
|
47 | + if (is_numeric($value)) { |
|
48 | + // Results in either int or float |
|
49 | + return $value + 0; |
|
50 | + } |
|
51 | + throw new \InvalidArgumentException( |
|
52 | + sprintf( |
|
53 | + 'Expecting value of type int, float or compatible, got variable of type %s instead.', |
|
54 | + Type::summarize($value) |
|
55 | + ) |
|
56 | + ); |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * ensureInteger values by safely converting. |
|
61 | - * |
|
62 | - * These are safe conversions because no information is lost: |
|
63 | - * |
|
64 | - * ``` |
|
65 | - * 1 => 1 |
|
66 | - * '1.00' => 1 |
|
67 | - * '1' => 1 |
|
68 | - * '+1' => 1 |
|
69 | - * ``` |
|
70 | - * |
|
71 | - * These are invalid conversions because information would be lost: |
|
72 | - * |
|
73 | - * ``` |
|
74 | - * 0.1 |
|
75 | - * '0.1' |
|
76 | - * '.1' |
|
77 | - * ``` |
|
78 | - * |
|
79 | - * If you don't care about this, you should cast to int instead: `(int)$value` |
|
80 | - * |
|
81 | - * @param mixed $value |
|
82 | - * |
|
83 | - * @throws \InvalidArgumentException If floating point information would be lost, i.e. it does not look like an integer. |
|
84 | - * |
|
85 | - * @return int |
|
86 | - */ |
|
87 | - public static function ensureInteger($value) { |
|
88 | - $numeric = self::ensure($value); |
|
89 | - if ((double) (int) $numeric !== (double) $numeric) { |
|
90 | - throw new \InvalidArgumentException( |
|
91 | - sprintf( |
|
92 | - "Could not safely convert value '%s' of type '%s' to integer because of trailing decimal places.", |
|
93 | - $value, |
|
94 | - Type::summarize($value) |
|
95 | - ) |
|
96 | - ); |
|
97 | - } |
|
98 | - return (int) $numeric; |
|
99 | - } |
|
59 | + /** |
|
60 | + * ensureInteger values by safely converting. |
|
61 | + * |
|
62 | + * These are safe conversions because no information is lost: |
|
63 | + * |
|
64 | + * ``` |
|
65 | + * 1 => 1 |
|
66 | + * '1.00' => 1 |
|
67 | + * '1' => 1 |
|
68 | + * '+1' => 1 |
|
69 | + * ``` |
|
70 | + * |
|
71 | + * These are invalid conversions because information would be lost: |
|
72 | + * |
|
73 | + * ``` |
|
74 | + * 0.1 |
|
75 | + * '0.1' |
|
76 | + * '.1' |
|
77 | + * ``` |
|
78 | + * |
|
79 | + * If you don't care about this, you should cast to int instead: `(int)$value` |
|
80 | + * |
|
81 | + * @param mixed $value |
|
82 | + * |
|
83 | + * @throws \InvalidArgumentException If floating point information would be lost, i.e. it does not look like an integer. |
|
84 | + * |
|
85 | + * @return int |
|
86 | + */ |
|
87 | + public static function ensureInteger($value) { |
|
88 | + $numeric = self::ensure($value); |
|
89 | + if ((double) (int) $numeric !== (double) $numeric) { |
|
90 | + throw new \InvalidArgumentException( |
|
91 | + sprintf( |
|
92 | + "Could not safely convert value '%s' of type '%s' to integer because of trailing decimal places.", |
|
93 | + $value, |
|
94 | + Type::summarize($value) |
|
95 | + ) |
|
96 | + ); |
|
97 | + } |
|
98 | + return (int) $numeric; |
|
99 | + } |
|
100 | 100 | |
101 | - /** |
|
102 | - * ensureFloat values by safely converting. |
|
103 | - * |
|
104 | - * For example the following conversions are safe: |
|
105 | - * |
|
106 | - * ``` |
|
107 | - * '0' => 0.0 |
|
108 | - * '0.0' => 0.0 |
|
109 | - * '0.1' => 0.1 |
|
110 | - * '-5.1' => 5.1 |
|
111 | - * ``` |
|
112 | - * |
|
113 | - * @param mixed $value |
|
114 | - * |
|
115 | - * @throws \InvalidArgumentException If value could not be safely converted to float. |
|
116 | - * |
|
117 | - * @return float |
|
118 | - */ |
|
119 | - public static function ensureFloat($value) { |
|
120 | - return (double) self::ensure($value); |
|
121 | - } |
|
101 | + /** |
|
102 | + * ensureFloat values by safely converting. |
|
103 | + * |
|
104 | + * For example the following conversions are safe: |
|
105 | + * |
|
106 | + * ``` |
|
107 | + * '0' => 0.0 |
|
108 | + * '0.0' => 0.0 |
|
109 | + * '0.1' => 0.1 |
|
110 | + * '-5.1' => 5.1 |
|
111 | + * ``` |
|
112 | + * |
|
113 | + * @param mixed $value |
|
114 | + * |
|
115 | + * @throws \InvalidArgumentException If value could not be safely converted to float. |
|
116 | + * |
|
117 | + * @return float |
|
118 | + */ |
|
119 | + public static function ensureFloat($value) { |
|
120 | + return (double) self::ensure($value); |
|
121 | + } |
|
122 | 122 | } |
@@ -86,7 +86,7 @@ discard block |
||
86 | 86 | */ |
87 | 87 | public static function ensureInteger($value) { |
88 | 88 | $numeric = self::ensure($value); |
89 | - if ((double) (int) $numeric !== (double) $numeric) { |
|
89 | + if ((double)(int)$numeric !== (double)$numeric) { |
|
90 | 90 | throw new \InvalidArgumentException( |
91 | 91 | sprintf( |
92 | 92 | "Could not safely convert value '%s' of type '%s' to integer because of trailing decimal places.", |
@@ -95,7 +95,7 @@ discard block |
||
95 | 95 | ) |
96 | 96 | ); |
97 | 97 | } |
98 | - return (int) $numeric; |
|
98 | + return (int)$numeric; |
|
99 | 99 | } |
100 | 100 | |
101 | 101 | /** |
@@ -117,6 +117,6 @@ discard block |
||
117 | 117 | * @return float |
118 | 118 | */ |
119 | 119 | public static function ensureFloat($value) { |
120 | - return (double) self::ensure($value); |
|
120 | + return (double)self::ensure($value); |
|
121 | 121 | } |
122 | 122 | } |
@@ -5,122 +5,122 @@ |
||
5 | 5 | * EOL detects, converts and returns information about line-endings. |
6 | 6 | */ |
7 | 7 | final class EOL { |
8 | - /** |
|
9 | - * Carriage return: Mac OS <=v9, OS-9, Apple II, Commodore 8-bit, BBC Acorn, TRS-80. |
|
10 | - */ |
|
11 | - const EOL_CR = "\r"; |
|
12 | - /** |
|
13 | - * Line feed: Unix, Unix-like, Multics, BeOS, Amiga, RISC OS. |
|
14 | - */ |
|
15 | - const EOL_LF = "\n"; |
|
16 | - /** |
|
17 | - * Carriage return/Line feed: Windows, TOPS-10, RT-11, CP/M, MP/M, DOS, Atari TOS, OS/2, Symbian OS, Palm OS. |
|
18 | - */ |
|
19 | - const EOL_CR_LF = "\r\n"; |
|
8 | + /** |
|
9 | + * Carriage return: Mac OS <=v9, OS-9, Apple II, Commodore 8-bit, BBC Acorn, TRS-80. |
|
10 | + */ |
|
11 | + const EOL_CR = "\r"; |
|
12 | + /** |
|
13 | + * Line feed: Unix, Unix-like, Multics, BeOS, Amiga, RISC OS. |
|
14 | + */ |
|
15 | + const EOL_LF = "\n"; |
|
16 | + /** |
|
17 | + * Carriage return/Line feed: Windows, TOPS-10, RT-11, CP/M, MP/M, DOS, Atari TOS, OS/2, Symbian OS, Palm OS. |
|
18 | + */ |
|
19 | + const EOL_CR_LF = "\r\n"; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Note that EOL::detect depends on this order. |
|
23 | - * |
|
24 | - * @var array |
|
25 | - */ |
|
26 | - private static $eols = [ |
|
27 | - "\r\n" => [ |
|
28 | - 'CR+LF', |
|
29 | - 'Carriage return/line feed: Windows, TOPS-10, RT-11, CP/M, MP/M, DOS, Atari TOS, OS/2, Symbian OS, Palm OS', |
|
30 | - ], |
|
31 | - "\n" => ['LF', 'Line feed: Unix, Unix-like, Multics, BeOS, Amiga, RISC OS'], |
|
32 | - "\r" => [ |
|
33 | - 'CR', |
|
34 | - 'Carriage return: Mac OS <=v9, OS-9, Apple II, Commodore 8-bit, BBC Acorn, TRS-80', |
|
35 | - ], |
|
36 | - ]; |
|
37 | - private $eol; |
|
38 | - private $name; |
|
39 | - private $description; |
|
21 | + /** |
|
22 | + * Note that EOL::detect depends on this order. |
|
23 | + * |
|
24 | + * @var array |
|
25 | + */ |
|
26 | + private static $eols = [ |
|
27 | + "\r\n" => [ |
|
28 | + 'CR+LF', |
|
29 | + 'Carriage return/line feed: Windows, TOPS-10, RT-11, CP/M, MP/M, DOS, Atari TOS, OS/2, Symbian OS, Palm OS', |
|
30 | + ], |
|
31 | + "\n" => ['LF', 'Line feed: Unix, Unix-like, Multics, BeOS, Amiga, RISC OS'], |
|
32 | + "\r" => [ |
|
33 | + 'CR', |
|
34 | + 'Carriage return: Mac OS <=v9, OS-9, Apple II, Commodore 8-bit, BBC Acorn, TRS-80', |
|
35 | + ], |
|
36 | + ]; |
|
37 | + private $eol; |
|
38 | + private $name; |
|
39 | + private $description; |
|
40 | 40 | |
41 | - /** |
|
42 | - * @param string $eol Line ending. See the EOL_* class constants. |
|
43 | - */ |
|
44 | - public function __construct($eol) { |
|
45 | - $this->eol = $eol; |
|
46 | - if (!isset(self::$eols[$eol])) { |
|
47 | - throw new \DomainException(sprintf('Unknown line ending: %s', Strings::escapeControlChars($eol))); |
|
48 | - } |
|
49 | - $initEol = self::$eols[$eol]; |
|
50 | - $this->name = $initEol[0]; |
|
51 | - $this->description = $initEol[1]; |
|
52 | - } |
|
41 | + /** |
|
42 | + * @param string $eol Line ending. See the EOL_* class constants. |
|
43 | + */ |
|
44 | + public function __construct($eol) { |
|
45 | + $this->eol = $eol; |
|
46 | + if (!isset(self::$eols[$eol])) { |
|
47 | + throw new \DomainException(sprintf('Unknown line ending: %s', Strings::escapeControlChars($eol))); |
|
48 | + } |
|
49 | + $initEol = self::$eols[$eol]; |
|
50 | + $this->name = $initEol[0]; |
|
51 | + $this->description = $initEol[1]; |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * __toString casts to/returns the raw line ending string. |
|
56 | - * |
|
57 | - * @return string |
|
58 | - */ |
|
59 | - public function __toString() { |
|
60 | - return $this->eol; |
|
61 | - } |
|
54 | + /** |
|
55 | + * __toString casts to/returns the raw line ending string. |
|
56 | + * |
|
57 | + * @return string |
|
58 | + */ |
|
59 | + public function __toString() { |
|
60 | + return $this->eol; |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * getName of line ending, e.g. `LF`. |
|
65 | - * |
|
66 | - * @return string |
|
67 | - */ |
|
68 | - public function getName() { |
|
69 | - return $this->name; |
|
70 | - } |
|
63 | + /** |
|
64 | + * getName of line ending, e.g. `LF`. |
|
65 | + * |
|
66 | + * @return string |
|
67 | + */ |
|
68 | + public function getName() { |
|
69 | + return $this->name; |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * getDescription of line ending, e.g. `Line feed: Unix, Unix-like, Multics, BeOS, Amiga, RISC OS`. |
|
74 | - * |
|
75 | - * @return string |
|
76 | - */ |
|
77 | - public function getDescription() { |
|
78 | - return $this->description; |
|
79 | - } |
|
72 | + /** |
|
73 | + * getDescription of line ending, e.g. `Line feed: Unix, Unix-like, Multics, BeOS, Amiga, RISC OS`. |
|
74 | + * |
|
75 | + * @return string |
|
76 | + */ |
|
77 | + public function getDescription() { |
|
78 | + return $this->description; |
|
79 | + } |
|
80 | 80 | |
81 | - /** |
|
82 | - * Apply this EOL style to a string. |
|
83 | - * |
|
84 | - * @param string $input Input to be converted. |
|
85 | - * |
|
86 | - * @return string |
|
87 | - */ |
|
88 | - public function apply($input) { |
|
89 | - return (string) Multiline::create($input)->setEol($this); |
|
90 | - } |
|
81 | + /** |
|
82 | + * Apply this EOL style to a string. |
|
83 | + * |
|
84 | + * @param string $input Input to be converted. |
|
85 | + * |
|
86 | + * @return string |
|
87 | + */ |
|
88 | + public function apply($input) { |
|
89 | + return (string) Multiline::create($input)->setEol($this); |
|
90 | + } |
|
91 | 91 | |
92 | - /** |
|
93 | - * Detect the EOL style of a string and return an EOL representation. |
|
94 | - * |
|
95 | - * @param string $input Input string to be analyzed. |
|
96 | - * |
|
97 | - * @throws \RuntimeException Thrown on failure to detect any line ending. |
|
98 | - * |
|
99 | - * @return \nochso\Omni\EOL |
|
100 | - */ |
|
101 | - public static function detect($input) { |
|
102 | - $maxEol = Strings::getMostFrequentNeedle($input, array_keys(self::$eols)); |
|
103 | - $missingEOL = $maxEol === null; |
|
104 | - if ($missingEOL) { |
|
105 | - throw new \RuntimeException('Could not detect end-of-line. There are no EOL chars in the input string.'); |
|
106 | - } |
|
107 | - return new self($maxEol); |
|
108 | - } |
|
92 | + /** |
|
93 | + * Detect the EOL style of a string and return an EOL representation. |
|
94 | + * |
|
95 | + * @param string $input Input string to be analyzed. |
|
96 | + * |
|
97 | + * @throws \RuntimeException Thrown on failure to detect any line ending. |
|
98 | + * |
|
99 | + * @return \nochso\Omni\EOL |
|
100 | + */ |
|
101 | + public static function detect($input) { |
|
102 | + $maxEol = Strings::getMostFrequentNeedle($input, array_keys(self::$eols)); |
|
103 | + $missingEOL = $maxEol === null; |
|
104 | + if ($missingEOL) { |
|
105 | + throw new \RuntimeException('Could not detect end-of-line. There are no EOL chars in the input string.'); |
|
106 | + } |
|
107 | + return new self($maxEol); |
|
108 | + } |
|
109 | 109 | |
110 | - /** |
|
111 | - * DetectDefault falls back to a default EOL style on failure. |
|
112 | - * |
|
113 | - * @param string $input Input string to be analyzed. |
|
114 | - * @param string $default Optional, defaults to "\n". The default line ending to use when $strict is false. See the `EOL::EOL_*` constants. |
|
115 | - * |
|
116 | - * @return \nochso\Omni\EOL |
|
117 | - */ |
|
118 | - public static function detectDefault($input, $default = self::EOL_LF) { |
|
119 | - try { |
|
120 | - $eol = self::detect($input); |
|
121 | - } catch (\RuntimeException $e) { |
|
122 | - $eol = new self($default); |
|
123 | - } |
|
124 | - return $eol; |
|
125 | - } |
|
110 | + /** |
|
111 | + * DetectDefault falls back to a default EOL style on failure. |
|
112 | + * |
|
113 | + * @param string $input Input string to be analyzed. |
|
114 | + * @param string $default Optional, defaults to "\n". The default line ending to use when $strict is false. See the `EOL::EOL_*` constants. |
|
115 | + * |
|
116 | + * @return \nochso\Omni\EOL |
|
117 | + */ |
|
118 | + public static function detectDefault($input, $default = self::EOL_LF) { |
|
119 | + try { |
|
120 | + $eol = self::detect($input); |
|
121 | + } catch (\RuntimeException $e) { |
|
122 | + $eol = new self($default); |
|
123 | + } |
|
124 | + return $eol; |
|
125 | + } |
|
126 | 126 | } |
@@ -86,7 +86,7 @@ |
||
86 | 86 | * @return string |
87 | 87 | */ |
88 | 88 | public function apply($input) { |
89 | - return (string) Multiline::create($input)->setEol($this); |
|
89 | + return (string)Multiline::create($input)->setEol($this); |
|
90 | 90 | } |
91 | 91 | |
92 | 92 | /** |
@@ -5,100 +5,100 @@ |
||
5 | 5 | * Path helps keep the directory separator/implode/trim/replace madness away. |
6 | 6 | */ |
7 | 7 | final class Path { |
8 | - /** |
|
9 | - * Combine any amount of strings into a path. |
|
10 | - * |
|
11 | - * @param string|array ...$paths One or as many parameters as you need. Both strings and arrays of strings can be mixed. |
|
12 | - * |
|
13 | - * @return string The combined paths. Note that the directory separators will be changed to reflect the local system. |
|
14 | - */ |
|
15 | - public static function combine(...$paths) { |
|
16 | - // Flatten into simple array |
|
17 | - $paths = Arrays::flatten(...$paths); |
|
18 | - // Keep non-empty elements |
|
19 | - $paths = array_filter($paths, 'strlen'); |
|
20 | - // Split into scheme:// and rest |
|
21 | - $scheme = self::extractScheme($paths); |
|
22 | - // Implode, localize and simplify everything after scheme:// |
|
23 | - $path = implode(DIRECTORY_SEPARATOR, $paths); |
|
24 | - $path = self::localize($path); |
|
25 | - $quotedSeparator = preg_quote(DIRECTORY_SEPARATOR); |
|
26 | - $pattern = '#' . $quotedSeparator . '+#'; |
|
27 | - $path = preg_replace($pattern, $quotedSeparator, $path); |
|
28 | - return $scheme . $path; |
|
29 | - } |
|
8 | + /** |
|
9 | + * Combine any amount of strings into a path. |
|
10 | + * |
|
11 | + * @param string|array ...$paths One or as many parameters as you need. Both strings and arrays of strings can be mixed. |
|
12 | + * |
|
13 | + * @return string The combined paths. Note that the directory separators will be changed to reflect the local system. |
|
14 | + */ |
|
15 | + public static function combine(...$paths) { |
|
16 | + // Flatten into simple array |
|
17 | + $paths = Arrays::flatten(...$paths); |
|
18 | + // Keep non-empty elements |
|
19 | + $paths = array_filter($paths, 'strlen'); |
|
20 | + // Split into scheme:// and rest |
|
21 | + $scheme = self::extractScheme($paths); |
|
22 | + // Implode, localize and simplify everything after scheme:// |
|
23 | + $path = implode(DIRECTORY_SEPARATOR, $paths); |
|
24 | + $path = self::localize($path); |
|
25 | + $quotedSeparator = preg_quote(DIRECTORY_SEPARATOR); |
|
26 | + $pattern = '#' . $quotedSeparator . '+#'; |
|
27 | + $path = preg_replace($pattern, $quotedSeparator, $path); |
|
28 | + return $scheme . $path; |
|
29 | + } |
|
30 | 30 | |
31 | - /** |
|
32 | - * Localize directory separators for any file path according to current environment. |
|
33 | - * |
|
34 | - * @param string $path |
|
35 | - * @param string $directorySeparator |
|
36 | - * |
|
37 | - * @return string |
|
38 | - */ |
|
39 | - public static function localize($path, $directorySeparator = DIRECTORY_SEPARATOR) { |
|
40 | - $paths = [$path]; |
|
41 | - // Do not localize scheme:// paths |
|
42 | - $scheme = self::extractScheme($paths); |
|
43 | - if ($scheme !== '') { |
|
44 | - return $path; |
|
45 | - } |
|
46 | - $path = str_replace(['\\', '/'], $directorySeparator, $path); |
|
47 | - return $path; |
|
48 | - } |
|
31 | + /** |
|
32 | + * Localize directory separators for any file path according to current environment. |
|
33 | + * |
|
34 | + * @param string $path |
|
35 | + * @param string $directorySeparator |
|
36 | + * |
|
37 | + * @return string |
|
38 | + */ |
|
39 | + public static function localize($path, $directorySeparator = DIRECTORY_SEPARATOR) { |
|
40 | + $paths = [$path]; |
|
41 | + // Do not localize scheme:// paths |
|
42 | + $scheme = self::extractScheme($paths); |
|
43 | + if ($scheme !== '') { |
|
44 | + return $path; |
|
45 | + } |
|
46 | + $path = str_replace(['\\', '/'], $directorySeparator, $path); |
|
47 | + return $path; |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * Contains returns true if a base path contains a needle. |
|
52 | - * |
|
53 | - * Note that `realpath` is used on both base and needle: they need to exist or false is returned. |
|
54 | - * |
|
55 | - * Use this for avoiding directory traversal outside of a base path. |
|
56 | - * |
|
57 | - * @param string $base Path to base directory. |
|
58 | - * @param string $needle Needle that must exist within the base directory. |
|
59 | - * |
|
60 | - * @return bool True if both exist and needle does not escape the base folder. |
|
61 | - */ |
|
62 | - public static function contains($base, $needle) { |
|
63 | - $realBase = realpath($base); |
|
64 | - $needle = realpath($needle); |
|
65 | - if ($realBase === false || $needle === false) { |
|
66 | - return false; |
|
67 | - } |
|
68 | - return Strings::startsWith($needle, $base); |
|
69 | - } |
|
50 | + /** |
|
51 | + * Contains returns true if a base path contains a needle. |
|
52 | + * |
|
53 | + * Note that `realpath` is used on both base and needle: they need to exist or false is returned. |
|
54 | + * |
|
55 | + * Use this for avoiding directory traversal outside of a base path. |
|
56 | + * |
|
57 | + * @param string $base Path to base directory. |
|
58 | + * @param string $needle Needle that must exist within the base directory. |
|
59 | + * |
|
60 | + * @return bool True if both exist and needle does not escape the base folder. |
|
61 | + */ |
|
62 | + public static function contains($base, $needle) { |
|
63 | + $realBase = realpath($base); |
|
64 | + $needle = realpath($needle); |
|
65 | + if ($realBase === false || $needle === false) { |
|
66 | + return false; |
|
67 | + } |
|
68 | + return Strings::startsWith($needle, $base); |
|
69 | + } |
|
70 | 70 | |
71 | - /** |
|
72 | - * isAbsolute checks for an absolute UNIX, Windows or scheme:// path. |
|
73 | - * |
|
74 | - * Note that paths containing parent dots (`..`) can still be considered absolute. |
|
75 | - * |
|
76 | - * @param string $path |
|
77 | - * |
|
78 | - * @return bool True if the path is absolute i.e. it should be safe to append a relative path to it. |
|
79 | - */ |
|
80 | - public static function isAbsolute($path) { |
|
81 | - $pattern = '@^ |
|
71 | + /** |
|
72 | + * isAbsolute checks for an absolute UNIX, Windows or scheme:// path. |
|
73 | + * |
|
74 | + * Note that paths containing parent dots (`..`) can still be considered absolute. |
|
75 | + * |
|
76 | + * @param string $path |
|
77 | + * |
|
78 | + * @return bool True if the path is absolute i.e. it should be safe to append a relative path to it. |
|
79 | + */ |
|
80 | + public static function isAbsolute($path) { |
|
81 | + $pattern = '@^ |
|
82 | 82 | ( # Either.. |
83 | 83 | [/\\\\] # absolute start |
84 | 84 | | [a-z]:[/\\\\] # or Windows drive path |
85 | 85 | | [a-z][a-z0-9\.+-]+:// # or URI scheme:// - see http://tools.ietf.org/html/rfc3986#section-3.1 |
86 | 86 | )@ix'; |
87 | - return preg_match($pattern, $path) === 1; |
|
88 | - } |
|
87 | + return preg_match($pattern, $path) === 1; |
|
88 | + } |
|
89 | 89 | |
90 | - private static function extractScheme(&$parts) { |
|
91 | - if (!count($parts)) { |
|
92 | - return ''; |
|
93 | - } |
|
94 | - $first = reset($parts); |
|
95 | - if (preg_match('/^([a-z]+:\\/\\/)(.*)$/', $first, $matches)) { |
|
96 | - array_shift($parts); |
|
97 | - if ($matches[2] !== '') { |
|
98 | - array_unshift($parts, $matches[2]); |
|
99 | - } |
|
100 | - return $matches[1]; |
|
101 | - } |
|
102 | - return ''; |
|
103 | - } |
|
90 | + private static function extractScheme(&$parts) { |
|
91 | + if (!count($parts)) { |
|
92 | + return ''; |
|
93 | + } |
|
94 | + $first = reset($parts); |
|
95 | + if (preg_match('/^([a-z]+:\\/\\/)(.*)$/', $first, $matches)) { |
|
96 | + array_shift($parts); |
|
97 | + if ($matches[2] !== '') { |
|
98 | + array_unshift($parts, $matches[2]); |
|
99 | + } |
|
100 | + return $matches[1]; |
|
101 | + } |
|
102 | + return ''; |
|
103 | + } |
|
104 | 104 | } |
@@ -23,9 +23,9 @@ |
||
23 | 23 | $path = implode(DIRECTORY_SEPARATOR, $paths); |
24 | 24 | $path = self::localize($path); |
25 | 25 | $quotedSeparator = preg_quote(DIRECTORY_SEPARATOR); |
26 | - $pattern = '#' . $quotedSeparator . '+#'; |
|
26 | + $pattern = '#'.$quotedSeparator.'+#'; |
|
27 | 27 | $path = preg_replace($pattern, $quotedSeparator, $path); |
28 | - return $scheme . $path; |
|
28 | + return $scheme.$path; |
|
29 | 29 | } |
30 | 30 | |
31 | 31 | /** |
@@ -5,61 +5,61 @@ |
||
5 | 5 | * VersionInfo consists of a package name and version. |
6 | 6 | */ |
7 | 7 | final class VersionInfo { |
8 | - /** |
|
9 | - * `Omni v0.1.0`. |
|
10 | - */ |
|
11 | - const INFO_FORMAT_DEFAULT = '%s v%s'; |
|
12 | - /** |
|
13 | - * `Omni 0.1.0`. |
|
14 | - */ |
|
15 | - const INFO_FORMAT_SHORT = '%s %s'; |
|
16 | - /** |
|
17 | - * `Omni (Version 0.1.0)`. |
|
18 | - */ |
|
19 | - const INFO_FORMAT_LONG = '%s (Version %s)'; |
|
8 | + /** |
|
9 | + * `Omni v0.1.0`. |
|
10 | + */ |
|
11 | + const INFO_FORMAT_DEFAULT = '%s v%s'; |
|
12 | + /** |
|
13 | + * `Omni 0.1.0`. |
|
14 | + */ |
|
15 | + const INFO_FORMAT_SHORT = '%s %s'; |
|
16 | + /** |
|
17 | + * `Omni (Version 0.1.0)`. |
|
18 | + */ |
|
19 | + const INFO_FORMAT_LONG = '%s (Version %s)'; |
|
20 | 20 | |
21 | - /** |
|
22 | - * @var string |
|
23 | - */ |
|
24 | - private $version; |
|
25 | - /** |
|
26 | - * @var string |
|
27 | - */ |
|
28 | - private $name; |
|
29 | - /** |
|
30 | - * @var string |
|
31 | - */ |
|
32 | - private $infoFormat; |
|
21 | + /** |
|
22 | + * @var string |
|
23 | + */ |
|
24 | + private $version; |
|
25 | + /** |
|
26 | + * @var string |
|
27 | + */ |
|
28 | + private $name; |
|
29 | + /** |
|
30 | + * @var string |
|
31 | + */ |
|
32 | + private $infoFormat; |
|
33 | 33 | |
34 | - /** |
|
35 | - * @param string $name Package or application name. |
|
36 | - * @param string $version Version without a prefix. |
|
37 | - * @param string $infoFormat Optional format to use for `getInfo`. Defaults to `self::INFO_FORMAT_DEFAULT` |
|
38 | - */ |
|
39 | - public function __construct($name, $version, $infoFormat = self::INFO_FORMAT_DEFAULT) { |
|
40 | - $this->name = $name; |
|
41 | - $this->version = $version; |
|
42 | - $this->infoFormat = $infoFormat; |
|
43 | - } |
|
34 | + /** |
|
35 | + * @param string $name Package or application name. |
|
36 | + * @param string $version Version without a prefix. |
|
37 | + * @param string $infoFormat Optional format to use for `getInfo`. Defaults to `self::INFO_FORMAT_DEFAULT` |
|
38 | + */ |
|
39 | + public function __construct($name, $version, $infoFormat = self::INFO_FORMAT_DEFAULT) { |
|
40 | + $this->name = $name; |
|
41 | + $this->version = $version; |
|
42 | + $this->infoFormat = $infoFormat; |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * @return string |
|
47 | - */ |
|
48 | - public function getInfo() { |
|
49 | - return sprintf($this->infoFormat, $this->getName(), $this->getVersion()); |
|
50 | - } |
|
45 | + /** |
|
46 | + * @return string |
|
47 | + */ |
|
48 | + public function getInfo() { |
|
49 | + return sprintf($this->infoFormat, $this->getName(), $this->getVersion()); |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * @return string |
|
54 | - */ |
|
55 | - public function getVersion() { |
|
56 | - return $this->version; |
|
57 | - } |
|
52 | + /** |
|
53 | + * @return string |
|
54 | + */ |
|
55 | + public function getVersion() { |
|
56 | + return $this->version; |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * @return string |
|
61 | - */ |
|
62 | - public function getName() { |
|
63 | - return $this->name; |
|
64 | - } |
|
59 | + /** |
|
60 | + * @return string |
|
61 | + */ |
|
62 | + public function getName() { |
|
63 | + return $this->name; |
|
64 | + } |
|
65 | 65 | } |
@@ -5,87 +5,87 @@ |
||
5 | 5 | * Folder handles file system folders. |
6 | 6 | */ |
7 | 7 | final class Folder { |
8 | - /** |
|
9 | - * Ensure a folder exists by creating it if missing and throw an exception on failure. |
|
10 | - * |
|
11 | - * @param string $path |
|
12 | - * @param int $mode Optional, defaults to 0777. |
|
13 | - * |
|
14 | - * @throws \RuntimeException |
|
15 | - */ |
|
16 | - public static function ensure($path, $mode = 0777) { |
|
17 | - if (is_dir($path)) { |
|
18 | - return; |
|
19 | - } |
|
20 | - if (@mkdir($path, $mode, true)) { |
|
21 | - return; |
|
22 | - } |
|
23 | - throw new \RuntimeException(sprintf("Unable to create folder '%s': %s", $path, error_get_last()['message'])); |
|
24 | - } |
|
8 | + /** |
|
9 | + * Ensure a folder exists by creating it if missing and throw an exception on failure. |
|
10 | + * |
|
11 | + * @param string $path |
|
12 | + * @param int $mode Optional, defaults to 0777. |
|
13 | + * |
|
14 | + * @throws \RuntimeException |
|
15 | + */ |
|
16 | + public static function ensure($path, $mode = 0777) { |
|
17 | + if (is_dir($path)) { |
|
18 | + return; |
|
19 | + } |
|
20 | + if (@mkdir($path, $mode, true)) { |
|
21 | + return; |
|
22 | + } |
|
23 | + throw new \RuntimeException(sprintf("Unable to create folder '%s': %s", $path, error_get_last()['message'])); |
|
24 | + } |
|
25 | 25 | |
26 | - /** |
|
27 | - * Delete a directory and all of its contents recursively. |
|
28 | - * |
|
29 | - * @param string $path Path of folder to delete |
|
30 | - * |
|
31 | - * @throws \RuntimeException Thrown when something could not be deleted. |
|
32 | - */ |
|
33 | - public static function delete($path) { |
|
34 | - if (!is_dir($path)) { |
|
35 | - return; |
|
36 | - } |
|
37 | - self::deleteContents($path); |
|
38 | - if (@rmdir($path) === false) { |
|
39 | - throw new \RuntimeException(sprintf("Unable to delete folder '%s': %s", $path, error_get_last()['message'])); |
|
40 | - } |
|
41 | - } |
|
26 | + /** |
|
27 | + * Delete a directory and all of its contents recursively. |
|
28 | + * |
|
29 | + * @param string $path Path of folder to delete |
|
30 | + * |
|
31 | + * @throws \RuntimeException Thrown when something could not be deleted. |
|
32 | + */ |
|
33 | + public static function delete($path) { |
|
34 | + if (!is_dir($path)) { |
|
35 | + return; |
|
36 | + } |
|
37 | + self::deleteContents($path); |
|
38 | + if (@rmdir($path) === false) { |
|
39 | + throw new \RuntimeException(sprintf("Unable to delete folder '%s': %s", $path, error_get_last()['message'])); |
|
40 | + } |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * deleteContents of a folder recursively, but not the folder itself. |
|
45 | - * |
|
46 | - * On Windows systems this will try to remove the read-only attribute if needed. |
|
47 | - * |
|
48 | - * @param string $path Path of folder whose contents will be deleted |
|
49 | - * |
|
50 | - * @throws \RuntimeException Thrown when something could not be deleted. |
|
51 | - */ |
|
52 | - public static function deleteContents($path) { |
|
53 | - if (!is_dir($path)) { |
|
54 | - return; |
|
55 | - } |
|
56 | - /** @var \SplFileInfo[] $files */ |
|
57 | - $files = new \RecursiveIteratorIterator( |
|
58 | - new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS), |
|
59 | - \RecursiveIteratorIterator::CHILD_FIRST |
|
60 | - ); |
|
61 | - foreach ($files as $fileInfo) { |
|
62 | - if ($fileInfo->isDir()) { |
|
63 | - if (@rmdir($fileInfo->getRealPath()) === false) { |
|
64 | - throw new \RuntimeException( |
|
65 | - sprintf( |
|
66 | - "Unable to delete child folder '%s' in '%s': %s", |
|
67 | - $fileInfo->getFilename(), |
|
68 | - $fileInfo->getPathname(), |
|
69 | - error_get_last()['message'] |
|
70 | - ) |
|
71 | - ); |
|
72 | - } |
|
73 | - } elseif (@unlink($fileInfo->getRealPath()) === false) { |
|
74 | - if (OS::isWindows()) { |
|
75 | - Exec::create('attrib', '-R', $fileInfo->getRealPath())->run(); |
|
76 | - if (@unlink($fileInfo->getPathname())) { |
|
77 | - continue; |
|
78 | - } |
|
79 | - } |
|
80 | - throw new \RuntimeException( |
|
81 | - sprintf( |
|
82 | - "Unable to delete file '%s' in folder '%s': %s", |
|
83 | - $fileInfo->getFilename(), |
|
84 | - $fileInfo->getPathname(), |
|
85 | - error_get_last()['message'] |
|
86 | - ) |
|
87 | - ); |
|
88 | - } |
|
89 | - } |
|
90 | - } |
|
43 | + /** |
|
44 | + * deleteContents of a folder recursively, but not the folder itself. |
|
45 | + * |
|
46 | + * On Windows systems this will try to remove the read-only attribute if needed. |
|
47 | + * |
|
48 | + * @param string $path Path of folder whose contents will be deleted |
|
49 | + * |
|
50 | + * @throws \RuntimeException Thrown when something could not be deleted. |
|
51 | + */ |
|
52 | + public static function deleteContents($path) { |
|
53 | + if (!is_dir($path)) { |
|
54 | + return; |
|
55 | + } |
|
56 | + /** @var \SplFileInfo[] $files */ |
|
57 | + $files = new \RecursiveIteratorIterator( |
|
58 | + new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS), |
|
59 | + \RecursiveIteratorIterator::CHILD_FIRST |
|
60 | + ); |
|
61 | + foreach ($files as $fileInfo) { |
|
62 | + if ($fileInfo->isDir()) { |
|
63 | + if (@rmdir($fileInfo->getRealPath()) === false) { |
|
64 | + throw new \RuntimeException( |
|
65 | + sprintf( |
|
66 | + "Unable to delete child folder '%s' in '%s': %s", |
|
67 | + $fileInfo->getFilename(), |
|
68 | + $fileInfo->getPathname(), |
|
69 | + error_get_last()['message'] |
|
70 | + ) |
|
71 | + ); |
|
72 | + } |
|
73 | + } elseif (@unlink($fileInfo->getRealPath()) === false) { |
|
74 | + if (OS::isWindows()) { |
|
75 | + Exec::create('attrib', '-R', $fileInfo->getRealPath())->run(); |
|
76 | + if (@unlink($fileInfo->getPathname())) { |
|
77 | + continue; |
|
78 | + } |
|
79 | + } |
|
80 | + throw new \RuntimeException( |
|
81 | + sprintf( |
|
82 | + "Unable to delete file '%s' in folder '%s': %s", |
|
83 | + $fileInfo->getFilename(), |
|
84 | + $fileInfo->getPathname(), |
|
85 | + error_get_last()['message'] |
|
86 | + ) |
|
87 | + ); |
|
88 | + } |
|
89 | + } |
|
90 | + } |
|
91 | 91 | } |
@@ -5,43 +5,43 @@ |
||
5 | 5 | * OS. |
6 | 6 | */ |
7 | 7 | class OS { |
8 | - /** |
|
9 | - * isWindows returns true if the current OS is Windows. |
|
10 | - * |
|
11 | - * @param string $phpOs Optional, defaults to the PHP_OS constant. |
|
12 | - * |
|
13 | - * @return bool |
|
14 | - */ |
|
15 | - public static function isWindows($phpOs = PHP_OS) { |
|
16 | - return strtolower(substr($phpOs, 0, 3)) === 'win'; |
|
17 | - } |
|
8 | + /** |
|
9 | + * isWindows returns true if the current OS is Windows. |
|
10 | + * |
|
11 | + * @param string $phpOs Optional, defaults to the PHP_OS constant. |
|
12 | + * |
|
13 | + * @return bool |
|
14 | + */ |
|
15 | + public static function isWindows($phpOs = PHP_OS) { |
|
16 | + return strtolower(substr($phpOs, 0, 3)) === 'win'; |
|
17 | + } |
|
18 | 18 | |
19 | - /** |
|
20 | - * hasBinary returns true if the binary is available in any of the PATHs. |
|
21 | - * |
|
22 | - * @param string $binaryName |
|
23 | - * |
|
24 | - * @return bool |
|
25 | - */ |
|
26 | - public static function hasBinary($binaryName) { |
|
27 | - $exec = Exec::create(); |
|
28 | - if (self::isWindows()) { |
|
29 | - // 'where.exe' uses 0 for success, 1 for failure to find |
|
30 | - $exec->run('where.exe', '/q', $binaryName); |
|
31 | - return $exec->getStatus() === 0; |
|
32 | - } |
|
33 | - // 'which' uses 0 for success, 1 for failure to find |
|
34 | - $exec->run('which', $binaryName); |
|
35 | - if ($exec->getStatus() === 0) { |
|
36 | - return true; |
|
37 | - } |
|
38 | - // 'whereis' does not use status codes. Check for a matching line instead. |
|
39 | - $exec->run('whereis', '-b', $binaryName); |
|
40 | - foreach ($exec->getOutput() as $line) { |
|
41 | - if (preg_match('/^' . preg_quote($binaryName) . ': .*$/', $line) === 1) { |
|
42 | - return true; |
|
43 | - } |
|
44 | - } |
|
45 | - return false; |
|
46 | - } |
|
19 | + /** |
|
20 | + * hasBinary returns true if the binary is available in any of the PATHs. |
|
21 | + * |
|
22 | + * @param string $binaryName |
|
23 | + * |
|
24 | + * @return bool |
|
25 | + */ |
|
26 | + public static function hasBinary($binaryName) { |
|
27 | + $exec = Exec::create(); |
|
28 | + if (self::isWindows()) { |
|
29 | + // 'where.exe' uses 0 for success, 1 for failure to find |
|
30 | + $exec->run('where.exe', '/q', $binaryName); |
|
31 | + return $exec->getStatus() === 0; |
|
32 | + } |
|
33 | + // 'which' uses 0 for success, 1 for failure to find |
|
34 | + $exec->run('which', $binaryName); |
|
35 | + if ($exec->getStatus() === 0) { |
|
36 | + return true; |
|
37 | + } |
|
38 | + // 'whereis' does not use status codes. Check for a matching line instead. |
|
39 | + $exec->run('whereis', '-b', $binaryName); |
|
40 | + foreach ($exec->getOutput() as $line) { |
|
41 | + if (preg_match('/^' . preg_quote($binaryName) . ': .*$/', $line) === 1) { |
|
42 | + return true; |
|
43 | + } |
|
44 | + } |
|
45 | + return false; |
|
46 | + } |
|
47 | 47 | } |
@@ -38,7 +38,7 @@ |
||
38 | 38 | // 'whereis' does not use status codes. Check for a matching line instead. |
39 | 39 | $exec->run('whereis', '-b', $binaryName); |
40 | 40 | foreach ($exec->getOutput() as $line) { |
41 | - if (preg_match('/^' . preg_quote($binaryName) . ': .*$/', $line) === 1) { |
|
41 | + if (preg_match('/^'.preg_quote($binaryName).': .*$/', $line) === 1) { |
|
42 | 42 | return true; |
43 | 43 | } |
44 | 44 | } |
@@ -15,189 +15,189 @@ |
||
15 | 15 | * ``` |
16 | 16 | */ |
17 | 17 | class Duration { |
18 | - const MILLISECOND = '0.001'; |
|
19 | - const SECOND = 1; |
|
20 | - const MINUTE = 60; |
|
21 | - const HOUR = self::MINUTE * 60; |
|
22 | - const DAY = self::HOUR * 24; |
|
23 | - const WEEK = self::DAY * 7; |
|
24 | - const MONTH = self::DAY * 30; |
|
25 | - const YEAR = self::DAY * 365; |
|
26 | - /** |
|
27 | - * 1y 2m 3d 4h 5m 6s. |
|
28 | - */ |
|
29 | - const FORMAT_SHORT = 0; |
|
30 | - /** |
|
31 | - * 1 year 2 months 3 days 4 hours 5 minutes 6 seconds. |
|
32 | - */ |
|
33 | - const FORMAT_LONG = 1; |
|
18 | + const MILLISECOND = '0.001'; |
|
19 | + const SECOND = 1; |
|
20 | + const MINUTE = 60; |
|
21 | + const HOUR = self::MINUTE * 60; |
|
22 | + const DAY = self::HOUR * 24; |
|
23 | + const WEEK = self::DAY * 7; |
|
24 | + const MONTH = self::DAY * 30; |
|
25 | + const YEAR = self::DAY * 365; |
|
26 | + /** |
|
27 | + * 1y 2m 3d 4h 5m 6s. |
|
28 | + */ |
|
29 | + const FORMAT_SHORT = 0; |
|
30 | + /** |
|
31 | + * 1 year 2 months 3 days 4 hours 5 minutes 6 seconds. |
|
32 | + */ |
|
33 | + const FORMAT_LONG = 1; |
|
34 | 34 | |
35 | - private static $defaultFormats = [ |
|
36 | - self::FORMAT_SHORT => [ |
|
37 | - self::YEAR => 'y', |
|
38 | - self::MONTH => 'mo', |
|
39 | - self::WEEK => 'w', |
|
40 | - self::DAY => 'd', |
|
41 | - self::HOUR => 'h', |
|
42 | - self::MINUTE => 'm', |
|
43 | - self::SECOND => 's', |
|
44 | - self::MILLISECOND => 'ms', |
|
45 | - ], |
|
46 | - self::FORMAT_LONG => [ |
|
47 | - self::YEAR => ' year(s)', |
|
48 | - self::MONTH => ' month(s)', |
|
49 | - self::WEEK => ' week(s)', |
|
50 | - self::DAY => ' day(s)', |
|
51 | - self::HOUR => ' hour(s)', |
|
52 | - self::MINUTE => ' minute(s)', |
|
53 | - self::SECOND => ' second(s)', |
|
54 | - self::MILLISECOND => ' millisecond(s)', |
|
55 | - ], |
|
56 | - ]; |
|
57 | - /** |
|
58 | - * @var int|string |
|
59 | - */ |
|
60 | - private $format = self::FORMAT_SHORT; |
|
61 | - /** |
|
62 | - * @var array |
|
63 | - */ |
|
64 | - private $formats; |
|
65 | - /** |
|
66 | - * @var int |
|
67 | - */ |
|
68 | - private $limit = 0; |
|
35 | + private static $defaultFormats = [ |
|
36 | + self::FORMAT_SHORT => [ |
|
37 | + self::YEAR => 'y', |
|
38 | + self::MONTH => 'mo', |
|
39 | + self::WEEK => 'w', |
|
40 | + self::DAY => 'd', |
|
41 | + self::HOUR => 'h', |
|
42 | + self::MINUTE => 'm', |
|
43 | + self::SECOND => 's', |
|
44 | + self::MILLISECOND => 'ms', |
|
45 | + ], |
|
46 | + self::FORMAT_LONG => [ |
|
47 | + self::YEAR => ' year(s)', |
|
48 | + self::MONTH => ' month(s)', |
|
49 | + self::WEEK => ' week(s)', |
|
50 | + self::DAY => ' day(s)', |
|
51 | + self::HOUR => ' hour(s)', |
|
52 | + self::MINUTE => ' minute(s)', |
|
53 | + self::SECOND => ' second(s)', |
|
54 | + self::MILLISECOND => ' millisecond(s)', |
|
55 | + ], |
|
56 | + ]; |
|
57 | + /** |
|
58 | + * @var int|string |
|
59 | + */ |
|
60 | + private $format = self::FORMAT_SHORT; |
|
61 | + /** |
|
62 | + * @var array |
|
63 | + */ |
|
64 | + private $formats; |
|
65 | + /** |
|
66 | + * @var int |
|
67 | + */ |
|
68 | + private $limit = 0; |
|
69 | 69 | |
70 | - /** |
|
71 | - * @param int $format |
|
72 | - */ |
|
73 | - public function __construct($format = self::FORMAT_SHORT) { |
|
74 | - $this->formats = self::$defaultFormats; |
|
75 | - $this->setFormat($format); |
|
76 | - } |
|
70 | + /** |
|
71 | + * @param int $format |
|
72 | + */ |
|
73 | + public function __construct($format = self::FORMAT_SHORT) { |
|
74 | + $this->formats = self::$defaultFormats; |
|
75 | + $this->setFormat($format); |
|
76 | + } |
|
77 | 77 | |
78 | - /** |
|
79 | - * Create a new Duration. |
|
80 | - * |
|
81 | - * @param int $format |
|
82 | - * |
|
83 | - * @return \nochso\Omni\Format\Duration |
|
84 | - */ |
|
85 | - public static function create($format = self::FORMAT_SHORT) { |
|
86 | - return new self($format); |
|
87 | - } |
|
78 | + /** |
|
79 | + * Create a new Duration. |
|
80 | + * |
|
81 | + * @param int $format |
|
82 | + * |
|
83 | + * @return \nochso\Omni\Format\Duration |
|
84 | + */ |
|
85 | + public static function create($format = self::FORMAT_SHORT) { |
|
86 | + return new self($format); |
|
87 | + } |
|
88 | 88 | |
89 | - /** |
|
90 | - * addFormat to the existing defaults and set it as the current format. |
|
91 | - * |
|
92 | - * e.g. |
|
93 | - * |
|
94 | - * ```php |
|
95 | - * $format = Duration::FORMAT_LONG => [ |
|
96 | - * Duration::YEAR => ' year(s)', |
|
97 | - * Duration::MONTH => ' month(s)', |
|
98 | - * Duration::WEEK => ' week(s)', |
|
99 | - * Duration::DAY => ' day(s)', |
|
100 | - * Duration::HOUR => ' hour(s)', |
|
101 | - * Duration::MINUTE => ' minute(s)', |
|
102 | - * Duration::SECOND => ' second(s)', |
|
103 | - * ]; |
|
104 | - * $df->addFormat('my custom period format', $format); |
|
105 | - * ``` |
|
106 | - * |
|
107 | - * @param string $name |
|
108 | - * @param string[] $periodFormats |
|
109 | - * |
|
110 | - * @return $this |
|
111 | - */ |
|
112 | - public function addFormat($name, array $periodFormats) { |
|
113 | - $this->formats[$name] = $periodFormats; |
|
114 | - $this->setFormat($name); |
|
115 | - return $this; |
|
116 | - } |
|
89 | + /** |
|
90 | + * addFormat to the existing defaults and set it as the current format. |
|
91 | + * |
|
92 | + * e.g. |
|
93 | + * |
|
94 | + * ```php |
|
95 | + * $format = Duration::FORMAT_LONG => [ |
|
96 | + * Duration::YEAR => ' year(s)', |
|
97 | + * Duration::MONTH => ' month(s)', |
|
98 | + * Duration::WEEK => ' week(s)', |
|
99 | + * Duration::DAY => ' day(s)', |
|
100 | + * Duration::HOUR => ' hour(s)', |
|
101 | + * Duration::MINUTE => ' minute(s)', |
|
102 | + * Duration::SECOND => ' second(s)', |
|
103 | + * ]; |
|
104 | + * $df->addFormat('my custom period format', $format); |
|
105 | + * ``` |
|
106 | + * |
|
107 | + * @param string $name |
|
108 | + * @param string[] $periodFormats |
|
109 | + * |
|
110 | + * @return $this |
|
111 | + */ |
|
112 | + public function addFormat($name, array $periodFormats) { |
|
113 | + $this->formats[$name] = $periodFormats; |
|
114 | + $this->setFormat($name); |
|
115 | + return $this; |
|
116 | + } |
|
117 | 117 | |
118 | - /** |
|
119 | - * setFormat to use by its custom name or one of the default Duration constants. |
|
120 | - * |
|
121 | - * @param string $name One of the `Duration::FORMAT_*` constants or a name of a format added via `addFormat()` |
|
122 | - * |
|
123 | - * @return $this |
|
124 | - */ |
|
125 | - public function setFormat($name) { |
|
126 | - if (!isset($this->formats[$name])) { |
|
127 | - throw new \InvalidArgumentException(sprintf("Duration format named '%s' does not exist.", $name)); |
|
128 | - } |
|
129 | - $this->format = $name; |
|
130 | - return $this; |
|
131 | - } |
|
118 | + /** |
|
119 | + * setFormat to use by its custom name or one of the default Duration constants. |
|
120 | + * |
|
121 | + * @param string $name One of the `Duration::FORMAT_*` constants or a name of a format added via `addFormat()` |
|
122 | + * |
|
123 | + * @return $this |
|
124 | + */ |
|
125 | + public function setFormat($name) { |
|
126 | + if (!isset($this->formats[$name])) { |
|
127 | + throw new \InvalidArgumentException(sprintf("Duration format named '%s' does not exist.", $name)); |
|
128 | + } |
|
129 | + $this->format = $name; |
|
130 | + return $this; |
|
131 | + } |
|
132 | 132 | |
133 | - /** |
|
134 | - * limitPeriods limits the amount of significant periods (years, months, etc.) to keep. |
|
135 | - * |
|
136 | - * Significant periods are periods with non-zero values. |
|
137 | - * |
|
138 | - * @param int $limit 0 for keeping all significant periods or any positive integer. |
|
139 | - * |
|
140 | - * @return $this |
|
141 | - */ |
|
142 | - public function limitPeriods($limit) { |
|
143 | - $this->limit = Numeric::ensureInteger($limit); |
|
144 | - return $this; |
|
145 | - } |
|
133 | + /** |
|
134 | + * limitPeriods limits the amount of significant periods (years, months, etc.) to keep. |
|
135 | + * |
|
136 | + * Significant periods are periods with non-zero values. |
|
137 | + * |
|
138 | + * @param int $limit 0 for keeping all significant periods or any positive integer. |
|
139 | + * |
|
140 | + * @return $this |
|
141 | + */ |
|
142 | + public function limitPeriods($limit) { |
|
143 | + $this->limit = Numeric::ensureInteger($limit); |
|
144 | + return $this; |
|
145 | + } |
|
146 | 146 | |
147 | - /** |
|
148 | - * Format an amount of seconds or a `DateInterval` object. |
|
149 | - * |
|
150 | - * @param int|\DateInterval $duration |
|
151 | - * |
|
152 | - * @return string A formatted duration for human consumption. |
|
153 | - */ |
|
154 | - public function format($duration) { |
|
155 | - return $this->formatPeriods($duration, $this->formats[$this->format]); |
|
156 | - } |
|
147 | + /** |
|
148 | + * Format an amount of seconds or a `DateInterval` object. |
|
149 | + * |
|
150 | + * @param int|\DateInterval $duration |
|
151 | + * |
|
152 | + * @return string A formatted duration for human consumption. |
|
153 | + */ |
|
154 | + public function format($duration) { |
|
155 | + return $this->formatPeriods($duration, $this->formats[$this->format]); |
|
156 | + } |
|
157 | 157 | |
158 | - /** |
|
159 | - * @param int|\DateInterval $duration |
|
160 | - * @param array $steps |
|
161 | - * |
|
162 | - * @return string |
|
163 | - */ |
|
164 | - private function formatPeriods($duration, $steps) { |
|
165 | - $seconds = $this->ensureSeconds($duration); |
|
166 | - $parts = []; |
|
167 | - foreach ($steps as $minValue => $suffix) { |
|
168 | - if ($seconds >= $minValue) { |
|
169 | - $stepValue = $seconds / $minValue; |
|
170 | - if ($minValue < 1) { |
|
171 | - $stepValue = round($stepValue); |
|
172 | - } else { |
|
173 | - $stepValue = floor($stepValue); |
|
174 | - } |
|
175 | - if ($stepValue > 0) { |
|
176 | - $suffix = Quantity::format($suffix, $stepValue); |
|
177 | - $parts[] = $stepValue . $suffix; |
|
178 | - $seconds -= $stepValue * $minValue; |
|
179 | - } |
|
180 | - } |
|
181 | - } |
|
182 | - if (count($parts) === 0) { |
|
183 | - $parts[] = $seconds . Quantity::format($steps[self::SECOND], $seconds); |
|
184 | - } |
|
185 | - if ($this->limit > 0) { |
|
186 | - $parts = array_slice($parts, 0, $this->limit); |
|
187 | - } |
|
188 | - return implode(' ', $parts); |
|
189 | - } |
|
158 | + /** |
|
159 | + * @param int|\DateInterval $duration |
|
160 | + * @param array $steps |
|
161 | + * |
|
162 | + * @return string |
|
163 | + */ |
|
164 | + private function formatPeriods($duration, $steps) { |
|
165 | + $seconds = $this->ensureSeconds($duration); |
|
166 | + $parts = []; |
|
167 | + foreach ($steps as $minValue => $suffix) { |
|
168 | + if ($seconds >= $minValue) { |
|
169 | + $stepValue = $seconds / $minValue; |
|
170 | + if ($minValue < 1) { |
|
171 | + $stepValue = round($stepValue); |
|
172 | + } else { |
|
173 | + $stepValue = floor($stepValue); |
|
174 | + } |
|
175 | + if ($stepValue > 0) { |
|
176 | + $suffix = Quantity::format($suffix, $stepValue); |
|
177 | + $parts[] = $stepValue . $suffix; |
|
178 | + $seconds -= $stepValue * $minValue; |
|
179 | + } |
|
180 | + } |
|
181 | + } |
|
182 | + if (count($parts) === 0) { |
|
183 | + $parts[] = $seconds . Quantity::format($steps[self::SECOND], $seconds); |
|
184 | + } |
|
185 | + if ($this->limit > 0) { |
|
186 | + $parts = array_slice($parts, 0, $this->limit); |
|
187 | + } |
|
188 | + return implode(' ', $parts); |
|
189 | + } |
|
190 | 190 | |
191 | - /** |
|
192 | - * @param int|\DateInterval $duration |
|
193 | - * |
|
194 | - * @return int |
|
195 | - */ |
|
196 | - private function ensureSeconds($duration) { |
|
197 | - if ($duration instanceof \DateInterval) { |
|
198 | - $d1 = new \DateTime('@0'); |
|
199 | - return $d1->add($duration)->getTimestamp(); |
|
200 | - } |
|
201 | - return Numeric::ensure($duration); |
|
202 | - } |
|
191 | + /** |
|
192 | + * @param int|\DateInterval $duration |
|
193 | + * |
|
194 | + * @return int |
|
195 | + */ |
|
196 | + private function ensureSeconds($duration) { |
|
197 | + if ($duration instanceof \DateInterval) { |
|
198 | + $d1 = new \DateTime('@0'); |
|
199 | + return $d1->add($duration)->getTimestamp(); |
|
200 | + } |
|
201 | + return Numeric::ensure($duration); |
|
202 | + } |
|
203 | 203 | } |
@@ -174,13 +174,13 @@ |
||
174 | 174 | } |
175 | 175 | if ($stepValue > 0) { |
176 | 176 | $suffix = Quantity::format($suffix, $stepValue); |
177 | - $parts[] = $stepValue . $suffix; |
|
177 | + $parts[] = $stepValue.$suffix; |
|
178 | 178 | $seconds -= $stepValue * $minValue; |
179 | 179 | } |
180 | 180 | } |
181 | 181 | } |
182 | 182 | if (count($parts) === 0) { |
183 | - $parts[] = $seconds . Quantity::format($steps[self::SECOND], $seconds); |
|
183 | + $parts[] = $seconds.Quantity::format($steps[self::SECOND], $seconds); |
|
184 | 184 | } |
185 | 185 | if ($this->limit > 0) { |
186 | 186 | $parts = array_slice($parts, 0, $this->limit); |
@@ -40,36 +40,36 @@ |
||
40 | 40 | * ``` |
41 | 41 | */ |
42 | 42 | class Quantity { |
43 | - /** |
|
44 | - * Format a string depending on a quantity. |
|
45 | - * |
|
46 | - * See the class documentation for defining `$format`. |
|
47 | - * |
|
48 | - * @param string $format |
|
49 | - * @param string $quantity |
|
50 | - * |
|
51 | - * @return mixed |
|
52 | - */ |
|
53 | - public static function format($format, $quantity) { |
|
54 | - $quantity = Numeric::ensure($quantity); |
|
55 | - $callback = function ($matches) use ($quantity) { |
|
56 | - // Get available choices |
|
57 | - $choices = preg_split('/\|/', $matches[2]); |
|
58 | - // Assume plural |
|
59 | - $choice = 0; |
|
60 | - // Choose singular |
|
61 | - if ($quantity === 1.0 || $quantity === 1) { |
|
62 | - $choice = 1; |
|
63 | - } |
|
64 | - // Choose zero if it's defined, otherwise keep using plural format |
|
65 | - if (($quantity === 0 || $quantity === 0.0) && isset($choices[2])) { |
|
66 | - $choice = 2; |
|
67 | - } |
|
68 | - return Dot::get($choices, $choice, ''); |
|
69 | - }; |
|
70 | - $pattern = '/(?<!\\\\)(\\((.+?(?<!\\\\))\\))/'; |
|
71 | - $out = preg_replace_callback($pattern, $callback, $format); |
|
72 | - $out = sprintf($out, $quantity); |
|
73 | - return $out; |
|
74 | - } |
|
43 | + /** |
|
44 | + * Format a string depending on a quantity. |
|
45 | + * |
|
46 | + * See the class documentation for defining `$format`. |
|
47 | + * |
|
48 | + * @param string $format |
|
49 | + * @param string $quantity |
|
50 | + * |
|
51 | + * @return mixed |
|
52 | + */ |
|
53 | + public static function format($format, $quantity) { |
|
54 | + $quantity = Numeric::ensure($quantity); |
|
55 | + $callback = function ($matches) use ($quantity) { |
|
56 | + // Get available choices |
|
57 | + $choices = preg_split('/\|/', $matches[2]); |
|
58 | + // Assume plural |
|
59 | + $choice = 0; |
|
60 | + // Choose singular |
|
61 | + if ($quantity === 1.0 || $quantity === 1) { |
|
62 | + $choice = 1; |
|
63 | + } |
|
64 | + // Choose zero if it's defined, otherwise keep using plural format |
|
65 | + if (($quantity === 0 || $quantity === 0.0) && isset($choices[2])) { |
|
66 | + $choice = 2; |
|
67 | + } |
|
68 | + return Dot::get($choices, $choice, ''); |
|
69 | + }; |
|
70 | + $pattern = '/(?<!\\\\)(\\((.+?(?<!\\\\))\\))/'; |
|
71 | + $out = preg_replace_callback($pattern, $callback, $format); |
|
72 | + $out = sprintf($out, $quantity); |
|
73 | + return $out; |
|
74 | + } |
|
75 | 75 | } |
@@ -15,196 +15,196 @@ |
||
15 | 15 | * You can pick a base and suffix with `create()` or use the specifc setter methods. |
16 | 16 | */ |
17 | 17 | class Bytes { |
18 | - /** |
|
19 | - * 1024 binary base. |
|
20 | - */ |
|
21 | - const BASE_BINARY = 1024; |
|
22 | - /** |
|
23 | - * 1000 decimal base. |
|
24 | - */ |
|
25 | - const BASE_DECIMAL = 1000; |
|
26 | - /** |
|
27 | - * B, M, G, ... |
|
28 | - */ |
|
29 | - const SUFFIX_SIMPLE = 0; |
|
30 | - /** |
|
31 | - * KiB, MiB, GiB, ... (SHOULD be used with BASE_BINARY). |
|
32 | - */ |
|
33 | - const SUFFIX_IEC = 1; |
|
34 | - /** |
|
35 | - * kibibytes, mebibytes, gibibytes, ... (SHOULD be used with BASE_BINARY). |
|
36 | - */ |
|
37 | - const SUFFIX_IEC_LONG = 2; |
|
38 | - /** |
|
39 | - * kB, MB, GB, ... (SHOULD be used with BASE_DECIMAL). |
|
40 | - */ |
|
41 | - const SUFFIX_SI = 3; |
|
42 | - /** |
|
43 | - * kilobytes, megabytes, gigabytes, ... (SHOULD be used with BASE_DECIMAL). |
|
44 | - */ |
|
45 | - const SUFFIX_SI_LONG = 4; |
|
18 | + /** |
|
19 | + * 1024 binary base. |
|
20 | + */ |
|
21 | + const BASE_BINARY = 1024; |
|
22 | + /** |
|
23 | + * 1000 decimal base. |
|
24 | + */ |
|
25 | + const BASE_DECIMAL = 1000; |
|
26 | + /** |
|
27 | + * B, M, G, ... |
|
28 | + */ |
|
29 | + const SUFFIX_SIMPLE = 0; |
|
30 | + /** |
|
31 | + * KiB, MiB, GiB, ... (SHOULD be used with BASE_BINARY). |
|
32 | + */ |
|
33 | + const SUFFIX_IEC = 1; |
|
34 | + /** |
|
35 | + * kibibytes, mebibytes, gibibytes, ... (SHOULD be used with BASE_BINARY). |
|
36 | + */ |
|
37 | + const SUFFIX_IEC_LONG = 2; |
|
38 | + /** |
|
39 | + * kB, MB, GB, ... (SHOULD be used with BASE_DECIMAL). |
|
40 | + */ |
|
41 | + const SUFFIX_SI = 3; |
|
42 | + /** |
|
43 | + * kilobytes, megabytes, gigabytes, ... (SHOULD be used with BASE_DECIMAL). |
|
44 | + */ |
|
45 | + const SUFFIX_SI_LONG = 4; |
|
46 | 46 | |
47 | - private static $suffixes = [ |
|
48 | - self::SUFFIX_SIMPLE => ['B', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'], |
|
49 | - self::SUFFIX_IEC => ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'], |
|
50 | - self::SUFFIX_IEC_LONG => [ |
|
51 | - 'byte(s)', |
|
52 | - 'kibibyte(s)', |
|
53 | - 'mebibyte(s)', |
|
54 | - 'gibibyte(s)', |
|
55 | - 'tebibyte(s)', |
|
56 | - 'pebibyte(s)', |
|
57 | - 'exbibyte(s)', |
|
58 | - 'zebibyte(s)', |
|
59 | - 'yobibyte(s)', |
|
60 | - ], |
|
61 | - self::SUFFIX_SI => ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'], |
|
62 | - self::SUFFIX_SI_LONG => [ |
|
63 | - 'byte(s)', |
|
64 | - 'kilobyte(s)', |
|
65 | - 'megabyte(s)', |
|
66 | - 'gigabyte(s)', |
|
67 | - 'terabyte(s)', |
|
68 | - 'petabyte(s)', |
|
69 | - 'exabyte(s)', |
|
70 | - 'zettabyte(s)', |
|
71 | - 'yottabyte(s)', |
|
72 | - ], |
|
73 | - ]; |
|
74 | - /** |
|
75 | - * @var int |
|
76 | - */ |
|
77 | - private $base = self::BASE_BINARY; |
|
78 | - /** |
|
79 | - * @var int |
|
80 | - */ |
|
81 | - private $suffix = self::SUFFIX_IEC; |
|
82 | - /** |
|
83 | - * @var int |
|
84 | - */ |
|
85 | - private $precision = 2; |
|
86 | - /** |
|
87 | - * @var bool |
|
88 | - */ |
|
89 | - private $precisionTrimming = true; |
|
47 | + private static $suffixes = [ |
|
48 | + self::SUFFIX_SIMPLE => ['B', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'], |
|
49 | + self::SUFFIX_IEC => ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'], |
|
50 | + self::SUFFIX_IEC_LONG => [ |
|
51 | + 'byte(s)', |
|
52 | + 'kibibyte(s)', |
|
53 | + 'mebibyte(s)', |
|
54 | + 'gibibyte(s)', |
|
55 | + 'tebibyte(s)', |
|
56 | + 'pebibyte(s)', |
|
57 | + 'exbibyte(s)', |
|
58 | + 'zebibyte(s)', |
|
59 | + 'yobibyte(s)', |
|
60 | + ], |
|
61 | + self::SUFFIX_SI => ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'], |
|
62 | + self::SUFFIX_SI_LONG => [ |
|
63 | + 'byte(s)', |
|
64 | + 'kilobyte(s)', |
|
65 | + 'megabyte(s)', |
|
66 | + 'gigabyte(s)', |
|
67 | + 'terabyte(s)', |
|
68 | + 'petabyte(s)', |
|
69 | + 'exabyte(s)', |
|
70 | + 'zettabyte(s)', |
|
71 | + 'yottabyte(s)', |
|
72 | + ], |
|
73 | + ]; |
|
74 | + /** |
|
75 | + * @var int |
|
76 | + */ |
|
77 | + private $base = self::BASE_BINARY; |
|
78 | + /** |
|
79 | + * @var int |
|
80 | + */ |
|
81 | + private $suffix = self::SUFFIX_IEC; |
|
82 | + /** |
|
83 | + * @var int |
|
84 | + */ |
|
85 | + private $precision = 2; |
|
86 | + /** |
|
87 | + * @var bool |
|
88 | + */ |
|
89 | + private $precisionTrimming = true; |
|
90 | 90 | |
91 | - /** |
|
92 | - * Create a new Bytes instance. |
|
93 | - * |
|
94 | - * @param int $base The base to use when converting to different units. Must be one of the `Bytes::BASE_*` |
|
95 | - * constants. Optional, defaults to `BASE_BINARY`. |
|
96 | - * @param int $suffix The suffix style for units. Must be one of the `Bytes::SUFFIX_*` constants. Optional, |
|
97 | - * defaults to SUFFIX_IEC (KiB, MiB, etc.) |
|
98 | - * |
|
99 | - * @return \nochso\Omni\Bytes |
|
100 | - */ |
|
101 | - public static function create($base = self::BASE_BINARY, $suffix = self::SUFFIX_IEC) { |
|
102 | - $bytes = new self(); |
|
103 | - $bytes->setBase($base)->setSuffix($suffix); |
|
104 | - return $bytes; |
|
105 | - } |
|
91 | + /** |
|
92 | + * Create a new Bytes instance. |
|
93 | + * |
|
94 | + * @param int $base The base to use when converting to different units. Must be one of the `Bytes::BASE_*` |
|
95 | + * constants. Optional, defaults to `BASE_BINARY`. |
|
96 | + * @param int $suffix The suffix style for units. Must be one of the `Bytes::SUFFIX_*` constants. Optional, |
|
97 | + * defaults to SUFFIX_IEC (KiB, MiB, etc.) |
|
98 | + * |
|
99 | + * @return \nochso\Omni\Bytes |
|
100 | + */ |
|
101 | + public static function create($base = self::BASE_BINARY, $suffix = self::SUFFIX_IEC) { |
|
102 | + $bytes = new self(); |
|
103 | + $bytes->setBase($base)->setSuffix($suffix); |
|
104 | + return $bytes; |
|
105 | + } |
|
106 | 106 | |
107 | - /** |
|
108 | - * setBase to use when converting to different units. |
|
109 | - * |
|
110 | - * @param int $base Must be one of the `Bytes::BASE_*` constants. |
|
111 | - * |
|
112 | - * @return $this |
|
113 | - */ |
|
114 | - public function setBase($base) { |
|
115 | - if ($base !== self::BASE_BINARY && $base !== self::BASE_DECIMAL) { |
|
116 | - throw new \InvalidArgumentException('Unknown base. Use either Bytes::BASE_BINARY or Bytes::BASE_DECIMAL'); |
|
117 | - } |
|
118 | - $this->base = $base; |
|
119 | - return $this; |
|
120 | - } |
|
107 | + /** |
|
108 | + * setBase to use when converting to different units. |
|
109 | + * |
|
110 | + * @param int $base Must be one of the `Bytes::BASE_*` constants. |
|
111 | + * |
|
112 | + * @return $this |
|
113 | + */ |
|
114 | + public function setBase($base) { |
|
115 | + if ($base !== self::BASE_BINARY && $base !== self::BASE_DECIMAL) { |
|
116 | + throw new \InvalidArgumentException('Unknown base. Use either Bytes::BASE_BINARY or Bytes::BASE_DECIMAL'); |
|
117 | + } |
|
118 | + $this->base = $base; |
|
119 | + return $this; |
|
120 | + } |
|
121 | 121 | |
122 | - /** |
|
123 | - * setSuffix style for units. |
|
124 | - * |
|
125 | - * @param int $suffix Must be one of the `Bytes::SUFFIX_*` constants. |
|
126 | - * |
|
127 | - * @return $this |
|
128 | - */ |
|
129 | - public function setSuffix($suffix) { |
|
130 | - if (!isset(self::$suffixes[$suffix])) { |
|
131 | - throw new \InvalidArgumentException('Unknown suffix. Use one of the Bytes::SUFFIX_* constants.'); |
|
132 | - } |
|
133 | - $this->suffix = $suffix; |
|
134 | - return $this; |
|
135 | - } |
|
122 | + /** |
|
123 | + * setSuffix style for units. |
|
124 | + * |
|
125 | + * @param int $suffix Must be one of the `Bytes::SUFFIX_*` constants. |
|
126 | + * |
|
127 | + * @return $this |
|
128 | + */ |
|
129 | + public function setSuffix($suffix) { |
|
130 | + if (!isset(self::$suffixes[$suffix])) { |
|
131 | + throw new \InvalidArgumentException('Unknown suffix. Use one of the Bytes::SUFFIX_* constants.'); |
|
132 | + } |
|
133 | + $this->suffix = $suffix; |
|
134 | + return $this; |
|
135 | + } |
|
136 | 136 | |
137 | - /** |
|
138 | - * setPrecision of floating point values after the decimal point. |
|
139 | - * |
|
140 | - * @param int $precision Any non-negative integer. |
|
141 | - * |
|
142 | - * @return $this |
|
143 | - */ |
|
144 | - public function setPrecision($precision) { |
|
145 | - $this->precision = Numeric::ensureInteger($precision); |
|
146 | - return $this; |
|
147 | - } |
|
137 | + /** |
|
138 | + * setPrecision of floating point values after the decimal point. |
|
139 | + * |
|
140 | + * @param int $precision Any non-negative integer. |
|
141 | + * |
|
142 | + * @return $this |
|
143 | + */ |
|
144 | + public function setPrecision($precision) { |
|
145 | + $this->precision = Numeric::ensureInteger($precision); |
|
146 | + return $this; |
|
147 | + } |
|
148 | 148 | |
149 | - /** |
|
150 | - * enablePrecisionTrimming to remove trailing zeroes and decimal points. |
|
151 | - * |
|
152 | - * @return $this |
|
153 | - */ |
|
154 | - public function enablePrecisionTrimming() { |
|
155 | - $this->precisionTrimming = true; |
|
156 | - return $this; |
|
157 | - } |
|
149 | + /** |
|
150 | + * enablePrecisionTrimming to remove trailing zeroes and decimal points. |
|
151 | + * |
|
152 | + * @return $this |
|
153 | + */ |
|
154 | + public function enablePrecisionTrimming() { |
|
155 | + $this->precisionTrimming = true; |
|
156 | + return $this; |
|
157 | + } |
|
158 | 158 | |
159 | - /** |
|
160 | - * disablePrecisionTrimming to keep trailing zeroes. |
|
161 | - * |
|
162 | - * @return $this |
|
163 | - */ |
|
164 | - public function disablePrecisionTrimming() { |
|
165 | - $this->precisionTrimming = false; |
|
166 | - return $this; |
|
167 | - } |
|
159 | + /** |
|
160 | + * disablePrecisionTrimming to keep trailing zeroes. |
|
161 | + * |
|
162 | + * @return $this |
|
163 | + */ |
|
164 | + public function disablePrecisionTrimming() { |
|
165 | + $this->precisionTrimming = false; |
|
166 | + return $this; |
|
167 | + } |
|
168 | 168 | |
169 | - /** |
|
170 | - * Format a quantity of bytes for human consumption. |
|
171 | - * |
|
172 | - * @param int $bytes |
|
173 | - * |
|
174 | - * @return string |
|
175 | - */ |
|
176 | - public function format($bytes) { |
|
177 | - $bytes = Numeric::ensure($bytes); |
|
178 | - if (is_float($bytes) && $bytes > 0.0 && $bytes < 1.0) { |
|
179 | - throw new \InvalidArgumentException('Floats smaller than one can not be formatted.'); |
|
180 | - } |
|
181 | - // 0 bytes won't work with log(), so set defaults for this case |
|
182 | - $exponent = 0; |
|
183 | - $normBytes = 0; |
|
184 | - if ($bytes !== 0) { |
|
185 | - $exponent = log(abs($bytes), $this->base); |
|
186 | - $normBytes = pow($this->base, $exponent - floor($exponent)); |
|
187 | - // Make bytes negative again if needed |
|
188 | - $normBytes *= $bytes >= 0 ? 1 : -1; |
|
189 | - } |
|
190 | - $suffix = self::$suffixes[$this->suffix][$exponent]; |
|
191 | - $number = number_format($normBytes, $this->precision, '.', ''); |
|
192 | - $number = $this->trimPrecision($number); |
|
193 | - $suffix = Quantity::format($suffix, $number); |
|
194 | - return sprintf('%s %s', $number, $suffix); |
|
195 | - } |
|
169 | + /** |
|
170 | + * Format a quantity of bytes for human consumption. |
|
171 | + * |
|
172 | + * @param int $bytes |
|
173 | + * |
|
174 | + * @return string |
|
175 | + */ |
|
176 | + public function format($bytes) { |
|
177 | + $bytes = Numeric::ensure($bytes); |
|
178 | + if (is_float($bytes) && $bytes > 0.0 && $bytes < 1.0) { |
|
179 | + throw new \InvalidArgumentException('Floats smaller than one can not be formatted.'); |
|
180 | + } |
|
181 | + // 0 bytes won't work with log(), so set defaults for this case |
|
182 | + $exponent = 0; |
|
183 | + $normBytes = 0; |
|
184 | + if ($bytes !== 0) { |
|
185 | + $exponent = log(abs($bytes), $this->base); |
|
186 | + $normBytes = pow($this->base, $exponent - floor($exponent)); |
|
187 | + // Make bytes negative again if needed |
|
188 | + $normBytes *= $bytes >= 0 ? 1 : -1; |
|
189 | + } |
|
190 | + $suffix = self::$suffixes[$this->suffix][$exponent]; |
|
191 | + $number = number_format($normBytes, $this->precision, '.', ''); |
|
192 | + $number = $this->trimPrecision($number); |
|
193 | + $suffix = Quantity::format($suffix, $number); |
|
194 | + return sprintf('%s %s', $number, $suffix); |
|
195 | + } |
|
196 | 196 | |
197 | - /** |
|
198 | - * @param string $number |
|
199 | - * |
|
200 | - * @return float |
|
201 | - */ |
|
202 | - private function trimPrecision($number) { |
|
203 | - if ($this->precisionTrimming && strpos((string) $number, '.') !== false) { |
|
204 | - $number = rtrim($number, '0'); |
|
205 | - $number = rtrim($number, '.'); |
|
206 | - $number = (double) $number; |
|
207 | - } |
|
208 | - return $number; |
|
209 | - } |
|
197 | + /** |
|
198 | + * @param string $number |
|
199 | + * |
|
200 | + * @return float |
|
201 | + */ |
|
202 | + private function trimPrecision($number) { |
|
203 | + if ($this->precisionTrimming && strpos((string) $number, '.') !== false) { |
|
204 | + $number = rtrim($number, '0'); |
|
205 | + $number = rtrim($number, '.'); |
|
206 | + $number = (double) $number; |
|
207 | + } |
|
208 | + return $number; |
|
209 | + } |
|
210 | 210 | } |
@@ -200,10 +200,10 @@ |
||
200 | 200 | * @return float |
201 | 201 | */ |
202 | 202 | private function trimPrecision($number) { |
203 | - if ($this->precisionTrimming && strpos((string) $number, '.') !== false) { |
|
203 | + if ($this->precisionTrimming && strpos((string)$number, '.') !== false) { |
|
204 | 204 | $number = rtrim($number, '0'); |
205 | 205 | $number = rtrim($number, '.'); |
206 | - $number = (double) $number; |
|
206 | + $number = (double)$number; |
|
207 | 207 | } |
208 | 208 | return $number; |
209 | 209 | } |