encoding.class.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * Set the system encoding to the plateform encoding.
  4. *
  5. * @todo:
  6. * Note: those lines are here for ease of use only. They should be move away:
  7. *
  8. * 1 first autodetection should be done inside the Encoding class
  9. * 2 this library should not call a chamilo specific function (this should
  10. * be the other way around, chamilo calling the encoding functions)
  11. */
  12. $plateform_encoding = api_get_system_encoding();
  13. Encoding::system($plateform_encoding);
  14. /**
  15. * Encoding class. Handles text encoding. Usage:
  16. *
  17. * $encoding = Encoding::get('name');
  18. * $decoder = $encoding->decoder();
  19. * $decoder->convert('text');
  20. *
  21. * The system encoding is the platform/system/default encoding. This defaults to
  22. * UTF8 but can be changed:
  23. *
  24. * Encoding::system('name');
  25. *
  26. * Note that Encoding returns to its name when converted to a string. As such it
  27. * can be used in places where a string is expected:
  28. *
  29. * $utf8 = Encoding::Utf8();
  30. * echo $utf8;
  31. *
  32. * @copyright (c) 2012 University of Geneva
  33. * @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html
  34. * @author Laurent Opprecht <laurent@opprecht.info>
  35. */
  36. class Encoding
  37. {
  38. private static $system = null;
  39. /**
  40. * Returns encoding for $name.
  41. *
  42. * @param string $name
  43. * @return Encoding
  44. */
  45. public static function get($name)
  46. {
  47. if (is_object($name)) {
  48. return $name;
  49. } else if (Encoding::utf8()->is($name)) {
  50. return self::utf8();
  51. } else {
  52. return new self($name);
  53. }
  54. }
  55. /**
  56. * Returns the Utf8 encoding.
  57. *
  58. * @return Utf8
  59. */
  60. public static function utf8()
  61. {
  62. return Utf8::instance();
  63. }
  64. /**
  65. * Returns/set the system/default encoding.
  66. *
  67. * @return Encoding
  68. */
  69. public static function system($value = null)
  70. {
  71. if (is_object($value)) {
  72. self::$system = $value;
  73. } else if (is_string($value)) {
  74. self::$system = self::get($value);
  75. }
  76. return self::$system ? self::$system : self::utf8();
  77. }
  78. /**
  79. * Detect encoding from an abstract.
  80. *
  81. * @param string $abstract
  82. * @return Encoding
  83. */
  84. public static function detect_encoding($abstract)
  85. {
  86. $encoding_name = api_detect_encoding($abstract);
  87. return self::get($encoding_name);
  88. }
  89. protected $name = '';
  90. protected function __construct($name = '')
  91. {
  92. $this->name = $name;
  93. }
  94. /**
  95. * The name of the encoding
  96. *
  97. * @return string
  98. */
  99. function name()
  100. {
  101. return $this->name;
  102. }
  103. /**
  104. * The Byte Order Mark.
  105. *
  106. * @see http://en.wikipedia.org/wiki/Byte_order_mark
  107. * @return string
  108. */
  109. function bom()
  110. {
  111. return '';
  112. }
  113. /**
  114. * Returns a decoder that convert encoding to another encoding.
  115. *
  116. * @param string|Encoder $to Encoding to convert to, defaults to system encoding
  117. * @return Converter
  118. */
  119. public function decoder($to = null)
  120. {
  121. $from = $this;
  122. $to = $to ? $to : Encoding::system();
  123. return EncodingConverter::create($from, $to);
  124. }
  125. /**
  126. * Returns an encoder that convert from another encoding to this encoding.
  127. *
  128. * @param string|Encoder $from Encoding to convert from, defaults to system encoding.
  129. * @return Converter
  130. */
  131. public function encoder($from = null)
  132. {
  133. $from = $from ? $from : Encoding::system();
  134. $to = $this;
  135. return EncodingConverter::create($from, $to);
  136. }
  137. function __toString()
  138. {
  139. return $this->name();
  140. }
  141. }