aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/vendor/splitbrain/lesserphp/src/Utils/Util.php
blob: c638d801f0e96eeee7ce63c9fd8f8fa483db82e9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php

namespace LesserPHP\Utils;

use Exception;
use LesserPHP\Constants;

/**
 * Miscelaneous utility methods
 */
class Util
{
    /**
     * Clamps a value between a minimum and maximum value.
     *
     * This function takes a value and two boundary values (maximum and minimum).
     * It ensures that the provided value does not exceed the boundaries.
     * If the value is less than the minimum, the minimum is returned.
     * If the value is greater than the maximum, the maximum is returned.
     * Otherwise, the original value is returned.
     */
    public static function clamp(float $v, float $max = 1, float $min = 0): float
    {
        return min($max, max($min, $v));
    }

    /**
     * Quote a string for use in a regular expression
     */
    public static function pregQuote(string $what): string
    {
        return preg_quote($what, '/');
    }

    /**
     * Return a boolean type
     *
     * @param mixed $a
     */
    public static function toBool($a): array
    {
        if ($a) return Constants::TRUE;
        return Constants::FALSE;
    }

    /**
     * Converts numbers between different types of units
     *
     * @throws Exception
     */
    public static function convert($number, $to): array
    {
        $value = Asserts::assertNumber($number);
        $from = $number[2];

        // easy out
        if ($from == $to)
            return $number;

        // check if the from value is a length
        if (($from_index = array_search($from, Constants::LENGTH_UNITS)) !== false) {
            // make sure to value is too
            if (in_array($to, Constants::LENGTH_UNITS)) {
                // do the actual conversion
                $to_index = array_search($to, Constants::LENGTH_UNITS);
                $px = $value * Constants::LENGTH_BASES[$from_index];
                $result = $px * (1 / Constants::LENGTH_BASES[$to_index]);

                $result = round($result, 8);
                return ['number', $result, $to];
            }
        }

        // do the same check for times
        if (in_array($from, Constants::TIME_UNITS)) {
            if (in_array($to, Constants::TIME_UNITS)) {
                // currently only ms and s are valid
                if ($to == 'ms')
                    $result = $value * 1000;
                else $result = $value / 1000;

                $result = round($result, 8);
                return ['number', $result, $to];
            }
        }

        // lastly check for an angle
        if (in_array($from, Constants::ANGLE_UNITS)) {
            // convert whatever angle it is into degrees
            switch ($from) {
                case 'rad':
                    $deg = rad2deg($value);
                    break;
                case 'turn':
                    $deg = $value * 360;
                    break;
                case 'grad':
                    $deg = $value / (400 / 360);
                    break;
                default:
                    $deg = $value;
                    break;
            }

            // Then convert it from degrees into desired unit
            switch ($to) {
                case 'rad':
                    $result = deg2rad($deg);
                    break;
                case 'turn':
                    $result = $deg / 360;
                    break;
                case 'grad':
                    $result = $deg * (400 / 360);
                    break;
                default:
                    $result = $deg;
                    break;
            }

            $result = round($result, 8);
            return ['number', $result, $to];
        }

        // we don't know how to convert these
        throw new Exception("Cannot convert $from to $to");
    }
}