MB.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. <?php
  2. namespace ZN\EncodingSupport;
  3. class __USE_STATIC_ACCESS__MB implements MBInterface
  4. {
  5. //----------------------------------------------------------------------------------------------------
  6. //
  7. // Yazar : Ozan UYKUN <ozanbote@windowslive.com> | <ozanbote@gmail.com>
  8. // Site : www.zntr.net
  9. // Lisans : The MIT License
  10. // Telif Hakkı: Copyright (c) 2012-2016, zntr.net
  11. //
  12. //----------------------------------------------------------------------------------------------------
  13. use \CallUndefinedMethodTrait;
  14. //----------------------------------------------------------------------------------------------------
  15. // Error Control
  16. //----------------------------------------------------------------------------------------------------
  17. //
  18. // $error
  19. // $success
  20. //
  21. // error()
  22. // success()
  23. //
  24. //----------------------------------------------------------------------------------------------------
  25. use \ErrorControlTrait;
  26. /******************************************************************************************
  27. * SPLIT *
  28. *******************************************************************************************
  29. | Genel Kullanım: Çok baytlı bir dizgeyi düzenli ifade ile parçalara ayırır. |
  30. @param string $string
  31. @param string $pattern
  32. @param numeric $limit -1
  33. @return array
  34. | |
  35. ******************************************************************************************/
  36. public function split($string = '', $pattern = '', $limit = -1)
  37. {
  38. if( ! is_string($string) || ! is_string($pattern) )
  39. {
  40. return \Errors::set('Error', 'stringParameter', '1.(string) & 2.(pattern)');
  41. }
  42. return mb_split($pattern, $string, $limit);
  43. }
  44. /******************************************************************************************
  45. * SEARCH *
  46. *******************************************************************************************
  47. | Genel Kullanım: Dizge içerisinde istenilen karakter kümesini arar. |
  48. @param string $str
  49. @param string $neddle
  50. @param string $type str/string, pos/position
  51. @param bool $case true
  52. @return mixed
  53. | |
  54. ******************************************************************************************/
  55. public function search($str = '', $needle = '', $type = "str", $case = true)
  56. {
  57. return \Strings::search($str, $needle, $type, $case);
  58. }
  59. /******************************************************************************************
  60. * SECTION *
  61. *******************************************************************************************
  62. | Genel Kullanım: Dizgenin bir alt dizgesini alır. |
  63. @param string $str
  64. @param string $starting
  65. @param numeric $count NULL
  66. @param string $encoding utf-8
  67. @return string
  68. | |
  69. ******************************************************************************************/
  70. public function section($str = '', $starting = 0, $count = NULL, $encoding = "utf-8")
  71. {
  72. return \Strings::section($str, $starting, $count, $encoding);
  73. }
  74. /******************************************************************************************
  75. * PARSE GET *
  76. *******************************************************************************************
  77. | Genel Kullanım: GET verisini çözümler ve küresel değişkenleri tanımlar. |
  78. @param string $string
  79. @return array
  80. | |
  81. ******************************************************************************************/
  82. public function parseGet($string = '')
  83. {
  84. if( ! is_string($string) )
  85. {
  86. return \Errors::set('Error', 'stringParameter', '1.(string)');
  87. }
  88. mb_parse_str($string, $result);
  89. return $result;
  90. }
  91. /******************************************************************************************
  92. * CHECK *
  93. *******************************************************************************************
  94. | Genel Kullanım: Dizgenin belirtilen kodlama için geçerli olup olmadığını sınar. |
  95. @param string $string
  96. @param string $encoding
  97. @return string
  98. | |
  99. ******************************************************************************************/
  100. public function check($string = '', $encoding = 'UTF-8')
  101. {
  102. if( ! is_string($string) )
  103. {
  104. return \Errors::set('Error', 'stringParameter', '1.(string)');
  105. }
  106. if( ! isCharset($encoding) )
  107. {
  108. return \Errors::set('Error', 'charsetParameter', '2.($encoding)');
  109. }
  110. return mb_check_encoding($string, $encoding);
  111. }
  112. /******************************************************************************************
  113. * CONVERT CASE *
  114. *******************************************************************************************
  115. | Genel Kullanım: Bir dizgeye büyük-küçük harf dönüşümü uygular. |
  116. @param string $string
  117. @param string $flag upper, lower, title
  118. @param string $encoding
  119. @return string
  120. | |
  121. ******************************************************************************************/
  122. public function casing($string = '', $flag = 'upper', $encoding = 'UTF-8')
  123. {
  124. if( ! is_string($string) )
  125. {
  126. return \Errors::set('Error', 'stringParameter', '1.(string)');
  127. }
  128. if( ! isCharset($encoding) )
  129. {
  130. return \Errors::set('Error', 'charsetParameter', '3.($encoding)');
  131. }
  132. return mb_convert_case($string, \Convert::toConstant($flag, 'MB_CASE_'), $encoding);
  133. }
  134. /******************************************************************************************
  135. * CONVERT ENCODING *
  136. *******************************************************************************************
  137. | Genel Kullanım: Karakter kodlaması dönüşümü yapar. |
  138. @param string $string
  139. @param string $toEncoding UTF-8
  140. @param string $fromEncoding ASCII, UTF-8
  141. @return string
  142. | |
  143. ******************************************************************************************/
  144. public function convert($string = '', $toEncoding = 'UTF-8', $fromEncoding = 'ASCII, UTF-8')
  145. {
  146. if( ! is_string($string) )
  147. {
  148. return \Errors::set('Error', 'stringParameter', '1.(string)');
  149. }
  150. if( ! isCharset($toEncoding) )
  151. {
  152. return \Errors::set('Error', 'charsetParameter', '2.(toEncoding)');
  153. }
  154. return mb_convert_encoding($string, $toEncoding, $fromEncoding);
  155. }
  156. /******************************************************************************************
  157. * MIME DECODE *
  158. *******************************************************************************************
  159. | Genel Kullanım: MIME başlık alanındaki dizgeyi dönüştürür. |
  160. @param string $string
  161. @return string
  162. | |
  163. ******************************************************************************************/
  164. public function mimeDecode($string = '')
  165. {
  166. if( ! is_string($string) )
  167. {
  168. return \Errors::set('Error', 'stringParameter', '1.(string)');
  169. }
  170. return mb_decode_mimeheader($string);
  171. }
  172. /******************************************************************************************
  173. * MIME ENCODE *
  174. *******************************************************************************************
  175. | Genel Kullanım: Dizgeyi MIME başlığı için kodlar. |
  176. @param string $string
  177. @return string
  178. | |
  179. ******************************************************************************************/
  180. public function mimeEncode($string = '', $encoding = 'UTF-8', $transferEncoding = 'B', $crlf = "\r\n", $indent = 0)
  181. {
  182. if( ! is_string($string) )
  183. {
  184. return \Errors::set('Error', 'stringParameter', '1.(string)');
  185. }
  186. if( ! isCharset($encoding) )
  187. {
  188. return \Errors::set('Error', 'charsetParameter', '2.(encoding)');
  189. }
  190. return mb_encode_mimeheader($string, $encoding, $transferEncoding, $crlf, $indent);
  191. }
  192. /******************************************************************************************
  193. * TO ENTITY *
  194. *******************************************************************************************
  195. | Genel Kullanım: HTML sayısal karakter gösterimini karaktere dönüştürür. |
  196. @param string $string
  197. @param array $convertMap
  198. @param string $encoding UTF-8
  199. @return string
  200. | |
  201. ******************************************************************************************/
  202. public function toEntity($string = '', $convertMap = [], $encoding = 'UTF-8')
  203. {
  204. if( ! is_string($string) )
  205. {
  206. return \Errors::set('Error', 'stringParameter', '1.(string)');
  207. }
  208. if( ! is_array($convertMap) )
  209. {
  210. return \Errors::set('Error', 'arrayParameter', '2.(convertMap)');
  211. }
  212. if( ! isCharset($encoding) )
  213. {
  214. return \Errors::set('Error', 'charsetParameter', '3.(encoding)');
  215. }
  216. return mb_decode_numericentity($string, $convertMap, $encoding);
  217. }
  218. /******************************************************************************************
  219. * TO NUMERIC *
  220. *******************************************************************************************
  221. | Genel Kullanım: Karakter kodlarını HTML sayısal karakter gösterimlerine dönüştürür. |
  222. @param string $string
  223. @param array $convertMap
  224. @param string $encoding UTF-8
  225. @return string
  226. | |
  227. ******************************************************************************************/
  228. public function toNumeric($string = '', $convertMap = [], $encoding = 'UTF-8')
  229. {
  230. if( ! is_string($string) )
  231. {
  232. return \Errors::set('Error', 'stringParameter', '1.(string)');
  233. }
  234. if( ! is_array($convertMap) )
  235. {
  236. return \Errors::set('Error', 'arrayParameter', '2.(convertMap)');
  237. }
  238. if( ! isCharset($encoding) )
  239. {
  240. return \Errors::set('Error', 'charsetParameter', '3.(encoding)');
  241. }
  242. return mb_encode_numericentity($string, $convertMap, $encoding);
  243. }
  244. /******************************************************************************************
  245. * DETECT *
  246. *******************************************************************************************
  247. | Genel Kullanım: Karakter kodlamasını algılar. |
  248. @param string $string
  249. @param array $encodingList ASCII, UTF-8
  250. @param bool $strict false
  251. @return string
  252. | |
  253. ******************************************************************************************/
  254. public function detect($string = '', $encodingList = ['ASCII', 'UTF-8'], $strict = false)
  255. {
  256. if( ! is_string($string) )
  257. {
  258. return \Errors::set('Error', 'stringParameter', '1.(string)');
  259. }
  260. if( ! is_array($encodingList) )
  261. {
  262. return \Errors::set('Error', 'arrayParameter', '2.(encodingList)');
  263. }
  264. if( ! is_bool($strict) )
  265. {
  266. return \Errors::set('Error', 'booleanParameter', '3.(strict)');
  267. }
  268. return mb_detect_encoding($string, $encodingList, $strict);
  269. }
  270. /******************************************************************************************
  271. * DETECT ORDER *
  272. *******************************************************************************************
  273. | Genel Kullanım: Karakter kodlaması algılama sırasını tanımlar. |
  274. @param mixed $encodingList ASCII, UTF-8
  275. @return mixed
  276. | |
  277. ******************************************************************************************/
  278. public function detectOrder($encodingList = ['ASCII', 'UTF-8'])
  279. {
  280. return mb_detect_order($encodingList);
  281. }
  282. /******************************************************************************************
  283. * ALIASES *
  284. *******************************************************************************************
  285. | Genel Kullanım: Karakter setinin takma adını döndürür. |
  286. @param string $string
  287. @return array
  288. | |
  289. ******************************************************************************************/
  290. public function aliases($string = '')
  291. {
  292. if( ! isCharset($string) )
  293. {
  294. return \Errors::set('Error', 'charsetParameter', '1.(string)');
  295. }
  296. return mb_encoding_aliases($string);
  297. }
  298. /******************************************************************************************
  299. * INFO *
  300. *******************************************************************************************
  301. | Genel Kullanım: Mbstring değiştirgelerinin dahili ayarlarını döndürür. |
  302. @param string $string all
  303. @return array
  304. | |
  305. ******************************************************************************************/
  306. public function info($string = 'all')
  307. {
  308. if( ! is_string($string) )
  309. {
  310. return \Errors::set('Error', 'stringParameter', '1.(string)');
  311. }
  312. return mb_get_info($string);
  313. }
  314. /******************************************************************************************
  315. * HTTP INPUT *
  316. *******************************************************************************************
  317. | Genel Kullanım: HTTP girdi karakter kodlamasını algılar. |
  318. @param type $type GET için "G",
  319. POST için "P",
  320. COOKIE için "C", D
  321. Sting için "S",
  322. Liste için "L",
  323. Tam liste için "I"
  324. @return mixed
  325. | |
  326. ******************************************************************************************/
  327. public function httpInput($type = 'I')
  328. {
  329. if( ! is_string($type) )
  330. {
  331. return \Errors::set('Error', 'stringParameter', '1.(type)');
  332. }
  333. return mb_http_input($type);
  334. }
  335. /******************************************************************************************
  336. * HTTP INPUT *
  337. *******************************************************************************************
  338. | Genel Kullanım: HTTP çıktı karakter kodlamasını tanımlar. |
  339. @param type $encoding UTF-8
  340. @return mixed
  341. | |
  342. ******************************************************************************************/
  343. public function httpOutput($encoding = 'UTF-8')
  344. {
  345. if( ! isCharset($encoding) )
  346. {
  347. return \Errors::set('Error', 'charsetParameter', '1.(encoding)');
  348. }
  349. return mb_http_output($encoding);
  350. }
  351. /******************************************************************************************
  352. * LANG *
  353. *******************************************************************************************
  354. | Genel Kullanım: Geçerli dili tanımlar. |
  355. @param lang $lang neutral
  356. @return mixed
  357. | |
  358. ******************************************************************************************/
  359. public function lang($lang = 'neutral')
  360. {
  361. if( ! is_string($lang) )
  362. {
  363. return \Errors::set('Error', 'stringParameter', '1.(lang)');
  364. }
  365. return mb_language($lang);
  366. }
  367. /******************************************************************************************
  368. * ENCODINGS *
  369. *******************************************************************************************
  370. | Genel Kullanım: Desteklenen kodlamaların tamamını bir dizi olarak döndürür. |
  371. @param void
  372. @return array
  373. | |
  374. ******************************************************************************************/
  375. public function encodings()
  376. {
  377. return mb_list_encodings();
  378. }
  379. /******************************************************************************************
  380. * OUTPUT HANDLER *
  381. *******************************************************************************************
  382. | Genel Kullanım: Çıktı tamporundaki karakter kodlamasını dönüştüren geriçağırım işlevi. |
  383. @param string $contents
  384. @param numeric $status
  385. @return string
  386. | |
  387. ******************************************************************************************/
  388. public function outputHandler($contents = '', $status = 0)
  389. {
  390. if( ! is_string($contents) )
  391. {
  392. return \Errors::set('Error', 'stringParameter', '1.(contents)');
  393. }
  394. if( ! is_numeric($status) )
  395. {
  396. return \Errors::set('Error', 'numericParameter', '2.(status)');
  397. }
  398. return mb_output_handler($contents, $status);
  399. }
  400. /******************************************************************************************
  401. * MIME NAME *
  402. *******************************************************************************************
  403. | Genel Kullanım: MIME karakter kümesi dizgesini döndürür. |
  404. @param string $encoding
  405. @return string
  406. | |
  407. ******************************************************************************************/
  408. public function mimeName($encoding = 'UTF-8')
  409. {
  410. if( ! isCharset($encoding) )
  411. {
  412. return \Errors::set('Error', 'stringParameter', '1.(encoding)');
  413. }
  414. return mb_preferred_mime_name($encoding);
  415. }
  416. }