annotate vendor/paragonie/random_compat/lib/cast_to_int.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2 /**
Chris@0 3 * Random_* Compatibility Library
Chris@0 4 * for using the new PHP 7 random_* API in PHP 5 projects
Chris@0 5 *
Chris@0 6 * The MIT License (MIT)
Chris@0 7 *
Chris@16 8 * Copyright (c) 2015 - 2018 Paragon Initiative Enterprises
Chris@0 9 *
Chris@0 10 * Permission is hereby granted, free of charge, to any person obtaining a copy
Chris@0 11 * of this software and associated documentation files (the "Software"), to deal
Chris@0 12 * in the Software without restriction, including without limitation the rights
Chris@0 13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Chris@0 14 * copies of the Software, and to permit persons to whom the Software is
Chris@0 15 * furnished to do so, subject to the following conditions:
Chris@0 16 *
Chris@0 17 * The above copyright notice and this permission notice shall be included in
Chris@0 18 * all copies or substantial portions of the Software.
Chris@0 19 *
Chris@0 20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Chris@0 21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Chris@0 22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Chris@0 23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Chris@0 24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Chris@0 25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
Chris@0 26 * SOFTWARE.
Chris@0 27 */
Chris@0 28
Chris@0 29 if (!is_callable('RandomCompat_intval')) {
Chris@17 30
Chris@0 31 /**
Chris@0 32 * Cast to an integer if we can, safely.
Chris@17 33 *
Chris@0 34 * If you pass it a float in the range (~PHP_INT_MAX, PHP_INT_MAX)
Chris@0 35 * (non-inclusive), it will sanely cast it to an int. If you it's equal to
Chris@0 36 * ~PHP_INT_MAX or PHP_INT_MAX, we let it fail as not an integer. Floats
Chris@0 37 * lose precision, so the <= and => operators might accidentally let a float
Chris@0 38 * through.
Chris@17 39 *
Chris@0 40 * @param int|float $number The number we want to convert to an int
Chris@12 41 * @param bool $fail_open Set to true to not throw an exception
Chris@17 42 *
Chris@0 43 * @return float|int
Chris@12 44 * @psalm-suppress InvalidReturnType
Chris@0 45 *
Chris@0 46 * @throws TypeError
Chris@0 47 */
Chris@0 48 function RandomCompat_intval($number, $fail_open = false)
Chris@0 49 {
Chris@0 50 if (is_int($number) || is_float($number)) {
Chris@0 51 $number += 0;
Chris@0 52 } elseif (is_numeric($number)) {
Chris@16 53 /** @psalm-suppress InvalidOperand */
Chris@0 54 $number += 0;
Chris@0 55 }
Chris@16 56 /** @var int|float $number */
Chris@0 57
Chris@0 58 if (
Chris@0 59 is_float($number)
Chris@16 60 &&
Chris@0 61 $number > ~PHP_INT_MAX
Chris@16 62 &&
Chris@0 63 $number < PHP_INT_MAX
Chris@0 64 ) {
Chris@0 65 $number = (int) $number;
Chris@0 66 }
Chris@0 67
Chris@0 68 if (is_int($number)) {
Chris@0 69 return (int) $number;
Chris@0 70 } elseif (!$fail_open) {
Chris@0 71 throw new TypeError(
Chris@0 72 'Expected an integer.'
Chris@0 73 );
Chris@0 74 }
Chris@0 75 return $number;
Chris@0 76 }
Chris@0 77 }