annotate vendor/zendframework/zend-stdlib/src/StringWrapper/Native.php @ 19:fa3358dc1485 tip

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