1<?php
2
3/**
4 * @see       https://github.com/laminas/laminas-stdlib for the canonical source repository
5 * @copyright https://github.com/laminas/laminas-stdlib/blob/master/COPYRIGHT.md
6 * @license   https://github.com/laminas/laminas-stdlib/blob/master/LICENSE.md New BSD License
7 */
8
9namespace Laminas\Stdlib\StringWrapper;
10
11use Laminas\Stdlib\Exception;
12use Laminas\Stdlib\StringUtils;
13
14class Native extends AbstractStringWrapper
15{
16    /**
17     * The character encoding working on
18     * (overwritten to change default encoding)
19     *
20     * @var string
21     */
22    protected $encoding = 'ASCII';
23
24    /**
25     * Check if the given character encoding is supported by this wrapper
26     * and the character encoding to convert to is also supported.
27     *
28     * @param  string      $encoding
29     * @param  string|null $convertEncoding
30     * @return bool
31     */
32    public static function isSupported($encoding, $convertEncoding = null)
33    {
34        $encodingUpper      = strtoupper($encoding);
35        $supportedEncodings = static::getSupportedEncodings();
36
37        if (! in_array($encodingUpper, $supportedEncodings)) {
38            return false;
39        }
40
41        // This adapter doesn't support to convert between encodings
42        if ($convertEncoding !== null && $encodingUpper !== strtoupper($convertEncoding)) {
43            return false;
44        }
45
46        return true;
47    }
48
49    /**
50     * Get a list of supported character encodings
51     *
52     * @return string[]
53     */
54    public static function getSupportedEncodings()
55    {
56        return StringUtils::getSingleByteEncodings();
57    }
58
59    /**
60     * Set character encoding working with and convert to
61     *
62     * @param string      $encoding         The character encoding to work with
63     * @param string|null $convertEncoding  The character encoding to convert to
64     * @return StringWrapperInterface
65     */
66    public function setEncoding($encoding, $convertEncoding = null)
67    {
68        $supportedEncodings = static::getSupportedEncodings();
69
70        $encodingUpper = strtoupper($encoding);
71        if (! in_array($encodingUpper, $supportedEncodings)) {
72            throw new Exception\InvalidArgumentException(
73                'Wrapper doesn\'t support character encoding "' . $encoding . '"'
74            );
75        }
76
77        if ($encodingUpper !== strtoupper($convertEncoding)) {
78            $this->convertEncoding = $encodingUpper;
79        }
80
81        if ($convertEncoding !== null) {
82            if ($encodingUpper !== strtoupper($convertEncoding)) {
83                throw new Exception\InvalidArgumentException(
84                    'Wrapper doesn\'t support to convert between character encodings'
85                );
86            }
87
88            $this->convertEncoding = $encodingUpper;
89        } else {
90            $this->convertEncoding = null;
91        }
92        $this->encoding = $encodingUpper;
93
94        return $this;
95    }
96
97    /**
98     * Returns the length of the given string
99     *
100     * @param string $str
101     * @return int|false
102     */
103    public function strlen($str)
104    {
105        return strlen($str);
106    }
107
108    /**
109     * Returns the portion of string specified by the start and length parameters
110     *
111     * @param string   $str
112     * @param int      $offset
113     * @param int|null $length
114     * @return string|false
115     */
116    public function substr($str, $offset = 0, $length = null)
117    {
118        return substr($str, $offset, $length);
119    }
120
121    /**
122     * Find the position of the first occurrence of a substring in a string
123     *
124     * @param string $haystack
125     * @param string $needle
126     * @param int    $offset
127     * @return int|false
128     */
129    public function strpos($haystack, $needle, $offset = 0)
130    {
131        return strpos($haystack, $needle, $offset);
132    }
133}
134