Quote.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\DB\Platform;
  7. use Magento\Framework\DB\Select;
  8. /**
  9. * Class Quote
  10. */
  11. class Quote
  12. {
  13. /**
  14. * @param string $identifier
  15. * @return string
  16. */
  17. public function quoteIdentifier($identifier)
  18. {
  19. return $this->quoteIdentifierAs($identifier);
  20. }
  21. /**
  22. * @param string $identifier
  23. * @param string|null $alias
  24. * @return string
  25. */
  26. public function quoteColumnAs($identifier, $alias = null)
  27. {
  28. return $this->quoteIdentifierAs($identifier, $alias);
  29. }
  30. /**
  31. * @param string $identifier
  32. * @param string|null $alias
  33. * @return string
  34. */
  35. public function quoteTableAs($identifier, $alias = null)
  36. {
  37. return $this->quoteIdentifierAs($identifier, $alias);
  38. }
  39. /**
  40. * @param string $identifier
  41. * @param string|null $alias
  42. * @return string
  43. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  44. */
  45. protected function quoteIdentifierAs($identifier, $alias = null)
  46. {
  47. if ($identifier instanceof \Zend_Db_Expr) {
  48. $quoted = $identifier->__toString();
  49. } elseif ($identifier instanceof \Magento\Framework\DB\Select) {
  50. $quoted = '(' . $identifier->assemble() . ')';
  51. } else {
  52. if (is_string($identifier)) {
  53. $identifier = explode('.', $identifier);
  54. }
  55. if (is_array($identifier)) {
  56. $segments = [];
  57. foreach ($identifier as $segment) {
  58. if ($segment instanceof \Zend_Db_Expr) {
  59. $segments[] = $segment->__toString();
  60. } else {
  61. $segments[] = $this->replaceQuoteSymbol($segment);
  62. }
  63. }
  64. if ($alias !== null && end($identifier) == $alias) {
  65. $alias = null;
  66. }
  67. $quoted = implode('.', $segments);
  68. } else {
  69. $quoted = $this->replaceQuoteSymbol($identifier);
  70. }
  71. }
  72. if ($alias !== null) {
  73. $quoted .= ' ' . Select::SQL_AS . ' ' . $this->replaceQuoteSymbol($alias);
  74. }
  75. return $quoted;
  76. }
  77. /**
  78. * @param string $value
  79. * @return string
  80. */
  81. protected function replaceQuoteSymbol($value)
  82. {
  83. $symbol = $this->getQuoteIdentifierSymbol();
  84. return ($symbol . str_replace("$symbol", "$symbol$symbol", $value) . $symbol);
  85. }
  86. /**
  87. * @return string
  88. */
  89. protected function getQuoteIdentifierSymbol()
  90. {
  91. return '`';
  92. }
  93. }