Native.php 3.6 KB

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