TableProperty.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin\Plugins\Export\Helpers;
  4. use PhpMyAdmin\Plugins\Export\ExportCodegen;
  5. use const ENT_COMPAT;
  6. use function htmlspecialchars;
  7. use function mb_strpos;
  8. use function mb_substr;
  9. use function str_replace;
  10. use function strlen;
  11. use function trim;
  12. /**
  13. * PhpMyAdmin\Plugins\Export\Helpers\TableProperty class
  14. */
  15. class TableProperty
  16. {
  17. /**
  18. * Name
  19. *
  20. * @var string
  21. */
  22. public $name;
  23. /**
  24. * Type
  25. *
  26. * @var string
  27. */
  28. public $type;
  29. /**
  30. * Whether the key is nullable or not
  31. *
  32. * @var string
  33. */
  34. public $nullable;
  35. /**
  36. * The key
  37. *
  38. * @var string
  39. */
  40. public $key;
  41. /**
  42. * Default value
  43. *
  44. * @var mixed
  45. */
  46. public $defaultValue;
  47. /**
  48. * Extension
  49. *
  50. * @var string
  51. */
  52. public $ext;
  53. /**
  54. * @param array $row table row
  55. */
  56. public function __construct(array $row)
  57. {
  58. $this->name = trim((string) $row[0]);
  59. $this->type = trim((string) $row[1]);
  60. $this->nullable = trim((string) $row[2]);
  61. $this->key = trim((string) $row[3]);
  62. $this->defaultValue = trim((string) $row[4]);
  63. $this->ext = trim((string) $row[5]);
  64. }
  65. /**
  66. * Gets the pure type
  67. *
  68. * @return string type
  69. */
  70. public function getPureType()
  71. {
  72. $pos = (int) mb_strpos($this->type, '(');
  73. if ($pos > 0) {
  74. return mb_substr($this->type, 0, $pos);
  75. }
  76. return $this->type;
  77. }
  78. /**
  79. * Tells whether the key is null or not
  80. *
  81. * @return string true if the key is not null, false otherwise
  82. */
  83. public function isNotNull()
  84. {
  85. return $this->nullable === 'NO' ? 'true' : 'false';
  86. }
  87. /**
  88. * Tells whether the key is unique or not
  89. *
  90. * @return string "true" if the key is unique, "false" otherwise
  91. */
  92. public function isUnique(): string
  93. {
  94. return $this->key === 'PRI' || $this->key === 'UNI' ? 'true' : 'false';
  95. }
  96. /**
  97. * Gets the .NET primitive type
  98. *
  99. * @return string type
  100. */
  101. public function getDotNetPrimitiveType()
  102. {
  103. if (mb_strpos($this->type, 'int') === 0) {
  104. return 'int';
  105. }
  106. if (mb_strpos($this->type, 'longtext') === 0) {
  107. return 'string';
  108. }
  109. if (mb_strpos($this->type, 'long') === 0) {
  110. return 'long';
  111. }
  112. if (mb_strpos($this->type, 'char') === 0) {
  113. return 'string';
  114. }
  115. if (mb_strpos($this->type, 'varchar') === 0) {
  116. return 'string';
  117. }
  118. if (mb_strpos($this->type, 'text') === 0) {
  119. return 'string';
  120. }
  121. if (mb_strpos($this->type, 'tinyint') === 0) {
  122. return 'bool';
  123. }
  124. if (mb_strpos($this->type, 'datetime') === 0) {
  125. return 'DateTime';
  126. }
  127. return 'unknown';
  128. }
  129. /**
  130. * Gets the .NET object type
  131. *
  132. * @return string type
  133. */
  134. public function getDotNetObjectType()
  135. {
  136. if (mb_strpos($this->type, 'int') === 0) {
  137. return 'Int32';
  138. }
  139. if (mb_strpos($this->type, 'longtext') === 0) {
  140. return 'String';
  141. }
  142. if (mb_strpos($this->type, 'long') === 0) {
  143. return 'Long';
  144. }
  145. if (mb_strpos($this->type, 'char') === 0) {
  146. return 'String';
  147. }
  148. if (mb_strpos($this->type, 'varchar') === 0) {
  149. return 'String';
  150. }
  151. if (mb_strpos($this->type, 'text') === 0) {
  152. return 'String';
  153. }
  154. if (mb_strpos($this->type, 'tinyint') === 0) {
  155. return 'Boolean';
  156. }
  157. if (mb_strpos($this->type, 'datetime') === 0) {
  158. return 'DateTime';
  159. }
  160. return 'Unknown';
  161. }
  162. /**
  163. * Gets the index name
  164. *
  165. * @return string containing the name of the index
  166. */
  167. public function getIndexName()
  168. {
  169. if (strlen($this->key) > 0) {
  170. return 'index="'
  171. . htmlspecialchars($this->name, ENT_COMPAT, 'UTF-8')
  172. . '"';
  173. }
  174. return '';
  175. }
  176. /**
  177. * Tells whether the key is primary or not
  178. *
  179. * @return bool true if the key is primary, false otherwise
  180. */
  181. public function isPK(): bool
  182. {
  183. return $this->key === 'PRI';
  184. }
  185. /**
  186. * Formats a string for C#
  187. *
  188. * @param string $text string to be formatted
  189. *
  190. * @return string formatted text
  191. */
  192. public function formatCs($text)
  193. {
  194. $text = str_replace(
  195. '#name#',
  196. ExportCodegen::cgMakeIdentifier($this->name, false),
  197. $text
  198. );
  199. return $this->format($text);
  200. }
  201. /**
  202. * Formats a string for XML
  203. *
  204. * @param string $text string to be formatted
  205. *
  206. * @return string formatted text
  207. */
  208. public function formatXml($text)
  209. {
  210. $text = str_replace(
  211. [
  212. '#name#',
  213. '#indexName#',
  214. ],
  215. [
  216. htmlspecialchars($this->name, ENT_COMPAT, 'UTF-8'),
  217. $this->getIndexName(),
  218. ],
  219. $text
  220. );
  221. return $this->format($text);
  222. }
  223. /**
  224. * Formats a string
  225. *
  226. * @param string $text string to be formatted
  227. *
  228. * @return string formatted text
  229. */
  230. public function format($text)
  231. {
  232. $text = str_replace(
  233. [
  234. '#ucfirstName#',
  235. '#dotNetPrimitiveType#',
  236. '#dotNetObjectType#',
  237. '#type#',
  238. '#notNull#',
  239. '#unique#',
  240. ],
  241. [
  242. ExportCodegen::cgMakeIdentifier($this->name),
  243. $this->getDotNetPrimitiveType(),
  244. $this->getDotNetObjectType(),
  245. $this->getPureType(),
  246. $this->isNotNull(),
  247. $this->isUnique(),
  248. ],
  249. $text
  250. );
  251. return $text;
  252. }
  253. }