Facades
3 weeks ago
Traits
3 weeks ago
Arr.php
3 weeks ago
DataCaster.php
3 weeks ago
Flex.php
3 weeks ago
HigherOrderTapProxy.php
3 weeks ago
MediaAttachment.php
3 weeks ago
MessagesBag.php
3 weeks ago
Str.php
3 weeks ago
Url.php
3 weeks ago
Utils.php
3 weeks ago
Utils.php
65 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Miscellaneous utility methods including UUID generation, growth percentage calculation, and float-to-int rounding. |
| 5 | * Wraps wp_generate_uuid4 and provides safe numeric helpers for reporting features. |
| 6 | * Stateless static helpers with no container dependencies. |
| 7 | * |
| 8 | * @package Framework |
| 9 | * @subpackage Supports |
| 10 | * @since 1.0.0 |
| 11 | */ |
| 12 | namespace Kirki\Framework\Supports; |
| 13 | |
| 14 | \defined('ABSPATH') || exit; |
| 15 | class Utils |
| 16 | { |
| 17 | /** |
| 18 | * Calculates the percentage growth from the previous to the current value. |
| 19 | * |
| 20 | * @param mixed $current The current value. |
| 21 | * @param mixed $previous The previous value. |
| 22 | * @param int $precision The number of decimal places to round to. Defaults to 2. |
| 23 | * |
| 24 | * @return float The percentage growth. |
| 25 | * |
| 26 | * @since 1.0.0 |
| 27 | */ |
| 28 | public static function calculate_growth_in_percent($current, $previous, $precision = 2) |
| 29 | { |
| 30 | if (!\is_numeric($current) || !\is_numeric($previous)) { |
| 31 | return 0.0; |
| 32 | } |
| 33 | $current = \ctype_digit((string) $current) ? (int) $current : (float) $current; |
| 34 | $previous = \ctype_digit((string) $previous) ? (int) $previous : (float) $previous; |
| 35 | if ($previous === 0 || $previous === 0.0) { |
| 36 | return $current > 0 ? 100 : 0; |
| 37 | } |
| 38 | return \round(($current - $previous) / $previous * 100, $precision); |
| 39 | } |
| 40 | /** |
| 41 | * Generates a UUID. |
| 42 | * |
| 43 | * @return string The UUID. |
| 44 | * |
| 45 | * @since 1.0.0 |
| 46 | */ |
| 47 | public static function uuid() |
| 48 | { |
| 49 | return wp_generate_uuid4(); |
| 50 | } |
| 51 | /** |
| 52 | * Converts a float to an integer by rounding up. |
| 53 | * |
| 54 | * @param float $value The float value to convert. |
| 55 | * |
| 56 | * @return int The rounded integer value. |
| 57 | * |
| 58 | * @since 1.0.0 |
| 59 | */ |
| 60 | public static function to_int($value) |
| 61 | { |
| 62 | return (int) \round($value, 0, \PHP_ROUND_HALF_UP); |
| 63 | } |
| 64 | } |
| 65 |