<?php

declare(strict_types=1);

namespace _JchOptimizeVendor\Laminas\Stdlib\StringWrapper;

use _JchOptimizeVendor\Laminas\Stdlib\Exception;

class Intl extends AbstractStringWrapper
{
    /**
     * List of supported character sets (upper case).
     *
     * @var string[]
     */
    protected static $encodings = ['UTF-8'];

    /**
     * Constructor.
     *
     * @throws Exception\ExtensionNotLoadedException
     */
    public function __construct()
    {
        if (!\extension_loaded('intl')) {
            throw new Exception\ExtensionNotLoadedException('PHP extension "intl" is required for this wrapper');
        }
    }

    /**
     * Get a list of supported character encodings.
     *
     * @return string[]
     */
    public static function getSupportedEncodings()
    {
        return static::$encodings;
    }

    /**
     * Returns the length of the given string.
     *
     * @param string $str
     *
     * @return false|int
     */
    public function strlen($str)
    {
        $len = \grapheme_strlen($str);

        return $len ?? \false;
    }

    /**
     * Returns the portion of string specified by the start and length parameters.
     *
     * @param string   $str
     * @param int      $offset
     * @param null|int $length
     *
     * @return false|string
     */
    public function substr($str, $offset = 0, $length = null)
    {
        // Due fix of PHP #62759 The third argument returns an empty string if is 0 or null.
        if (null !== $length) {
            return \grapheme_substr($str, $offset, $length);
        }

        return \grapheme_substr($str, $offset);
    }

    /**
     * Find the position of the first occurrence of a substring in a string.
     *
     * @param string $haystack
     * @param string $needle
     * @param int    $offset
     *
     * @return false|int
     */
    public function strpos($haystack, $needle, $offset = 0)
    {
        return \grapheme_strpos($haystack, $needle, $offset);
    }
}
