Native.php 3.7 KB

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