vendor/symfony/polyfill-php80/Php80.php

115 lines · 3578 bytes

  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
 12namespace Symfony\Polyfill\Php80;
 13
 14/**
 15 * @author Ion Bazan <ion.bazan@gmail.com>
 16 * @author Nico Oelgart <nicoswd@gmail.com>
 17 * @author Nicolas Grekas <p@tchwork.com>
 18 *
 19 * @internal
 20 */
 21final class Php80
 22{
 23    public static function fdiv(float $dividend, float $divisor): float
 24    {
 25        return @($dividend / $divisor);
 26    }
 27
 28    public static function get_debug_type($value): string
 29    {
 30        switch (true) {
 31            case null === $value: return 'null';
 32            case \is_bool($value): return 'bool';
 33            case \is_string($value): return 'string';
 34            case \is_array($value): return 'array';
 35            case \is_int($value): return 'int';
 36            case \is_float($value): return 'float';
 37            case \is_object($value): break;
 38            case $value instanceof \__PHP_Incomplete_Class: return '__PHP_Incomplete_Class';
 39            default:
 40                if (null === $type = @get_resource_type($value)) {
 41                    return 'unknown';
 42                }
 43
 44                if ('Unknown' === $type) {
 45                    $type = 'closed';
 46                }
 47
 48                return "resource ($type)";
 49        }
 50
 51        $class = \get_class($value);
 52
 53        if (false === strpos($class, '@')) {
 54            return $class;
 55        }
 56
 57        return (get_parent_class($class) ?: key(class_implements($class)) ?: 'class').'@anonymous';
 58    }
 59
 60    public static function get_resource_id($res): int
 61    {
 62        if (!\is_resource($res) && null === @get_resource_type($res)) {
 63            throw new \TypeError(\sprintf('Argument 1 passed to get_resource_id() must be of the type resource, %s given', get_debug_type($res)));
 64        }
 65
 66        return (int) $res;
 67    }
 68
 69    public static function preg_last_error_msg(): string
 70    {
 71        switch (preg_last_error()) {
 72            case \PREG_INTERNAL_ERROR:
 73                return 'Internal error';
 74            case \PREG_BAD_UTF8_ERROR:
 75                return 'Malformed UTF-8 characters, possibly incorrectly encoded';
 76            case \PREG_BAD_UTF8_OFFSET_ERROR:
 77                return 'The offset did not correspond to the beginning of a valid UTF-8 code point';
 78            case \PREG_BACKTRACK_LIMIT_ERROR:
 79                return 'Backtrack limit exhausted';
 80            case \PREG_RECURSION_LIMIT_ERROR:
 81                return 'Recursion limit exhausted';
 82            case \PREG_JIT_STACKLIMIT_ERROR:
 83                return 'JIT stack limit exhausted';
 84            case \PREG_NO_ERROR:
 85                return 'No error';
 86            default:
 87                return 'Unknown error';
 88        }
 89    }
 90
 91    public static function str_contains(string $haystack, string $needle): bool
 92    {
 93        return '' === $needle || false !== strpos($haystack, $needle);
 94    }
 95
 96    public static function str_starts_with(string $haystack, string $needle): bool
 97    {
 98        return 0 === strncmp($haystack, $needle, \strlen($needle));
 99    }
100
101    public static function str_ends_with(string $haystack, string $needle): bool
102    {
103        if ('' === $needle || $needle === $haystack) {
104            return true;
105        }
106
107        if ('' === $haystack) {
108            return false;
109        }
110
111        $needleLength = \strlen($needle);
112
113        return $needleLength <= \strlen($haystack) && 0 === substr_compare($haystack, $needle, -$needleLength);
114    }
115}