Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace Dotenv\Util;
6
 
7
use GrahamCampbell\ResultType\Error;
8
use GrahamCampbell\ResultType\Success;
9
use PhpOption\Option;
10
 
11
/**
12
 * @internal
13
 */
14
final class Str
15
{
16
    /**
17
     * This class is a singleton.
18
     *
19
     * @codeCoverageIgnore
20
     *
21
     * @return void
22
     */
23
    private function __construct()
24
    {
25
        //
26
    }
27
 
28
    /**
29
     * Convert a string to UTF-8 from the given encoding.
30
     *
31
     * @param string      $input
32
     * @param string|null $encoding
33
     *
34
     * @return \GrahamCampbell\ResultType\Result<string,string>
35
     */
36
    public static function utf8(string $input, string $encoding = null)
37
    {
38
        if ($encoding !== null && !\in_array($encoding, \mb_list_encodings(), true)) {
39
            /** @var \GrahamCampbell\ResultType\Result<string,string> */
40
            return Error::create(
41
                \sprintf('Illegal character encoding [%s] specified.', $encoding)
42
            );
43
        }
44
        $converted = $encoding === null ?
45
            @\mb_convert_encoding($input, 'UTF-8') :
46
            @\mb_convert_encoding($input, 'UTF-8', $encoding);
47
        /**
48
         * this is for support UTF-8 with BOM encoding
49
         * @see https://en.wikipedia.org/wiki/Byte_order_mark
50
         * @see https://github.com/vlucas/phpdotenv/issues/500
51
         */
52
        if (\substr($converted, 0, 3) == "\xEF\xBB\xBF") {
53
            $converted = \substr($converted, 3);
54
        }
55
        /** @var \GrahamCampbell\ResultType\Result<string,string> */
56
        return Success::create($converted);
57
    }
58
 
59
    /**
60
     * Search for a given substring of the input.
61
     *
62
     * @param string $haystack
63
     * @param string $needle
64
     *
65
     * @return \PhpOption\Option<int>
66
     */
67
    public static function pos(string $haystack, string $needle)
68
    {
69
        /** @var \PhpOption\Option<int> */
70
        return Option::fromValue(\mb_strpos($haystack, $needle, 0, 'UTF-8'), false);
71
    }
72
 
73
    /**
74
     * Grab the specified substring of the input.
75
     *
76
     * @param string   $input
77
     * @param int      $start
78
     * @param int|null $length
79
     *
80
     * @return string
81
     */
82
    public static function substr(string $input, int $start, int $length = null)
83
    {
84
        return \mb_substr($input, $start, $length, 'UTF-8');
85
    }
86
 
87
    /**
88
     * Compute the length of the given string.
89
     *
90
     * @param string $input
91
     *
92
     * @return int
93
     */
94
    public static function len(string $input)
95
    {
96
        return \mb_strlen($input, 'UTF-8');
97
    }
98
}