AbstractHelper.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * Abstract DB helper class
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\DB\Helper;
  9. abstract class AbstractHelper
  10. {
  11. /**
  12. * Resource helper module prefix
  13. *
  14. * @var string
  15. */
  16. protected $_modulePrefix;
  17. /**
  18. * @var \Magento\Framework\App\ResourceConnection
  19. */
  20. protected $_resource;
  21. /**
  22. * Initialize resource helper instance
  23. *
  24. * @param \Magento\Framework\App\ResourceConnection $resource
  25. * @param string $modulePrefix
  26. */
  27. public function __construct(\Magento\Framework\App\ResourceConnection $resource, $modulePrefix)
  28. {
  29. $this->_resource = $resource;
  30. $this->_modulePrefix = (string)$modulePrefix;
  31. }
  32. /**
  33. * Retrieves connection to the resource
  34. *
  35. * @return \Magento\Framework\DB\Adapter\AdapterInterface
  36. */
  37. protected function getConnection()
  38. {
  39. return $this->_resource->getConnection($this->_modulePrefix);
  40. }
  41. /**
  42. * Escapes value, that participates in LIKE, with '\' symbol.
  43. * Note: this func cannot be used on its own, because different RDMBS may use different default escape symbols,
  44. * so you should either use addLikeEscape() to produce LIKE construction, or add escape symbol on your own.
  45. *
  46. * By default escapes '_', '%' and '\' symbols. If some masking symbols must not be escaped, then you can set
  47. * appropriate options in $options.
  48. *
  49. * $options can contain following flags:
  50. * - 'allow_symbol_mask' - the '_' symbol will not be escaped
  51. * - 'allow_string_mask' - the '%' symbol will not be escaped
  52. * - 'position' ('any', 'start', 'end') - expression will be formed so that $value will be found at position
  53. * within string, by default when nothing set - string must be fully matched with $value
  54. *
  55. * @param string $value
  56. * @param array $options
  57. * @return string
  58. */
  59. public function escapeLikeValue($value, $options = [])
  60. {
  61. $value = str_replace('\\', '\\\\', $value);
  62. $replaceFrom = [];
  63. $replaceTo = [];
  64. if (empty($options['allow_symbol_mask'])) {
  65. $replaceFrom[] = '_';
  66. $replaceTo[] = '\_';
  67. }
  68. if (empty($options['allow_string_mask'])) {
  69. $replaceFrom[] = '%';
  70. $replaceTo[] = '\%';
  71. }
  72. if ($replaceFrom) {
  73. $value = str_replace($replaceFrom, $replaceTo, $value);
  74. }
  75. if (isset($options['position'])) {
  76. switch ($options['position']) {
  77. case 'any':
  78. $value = '%' . $value . '%';
  79. break;
  80. case 'start':
  81. $value = $value . '%';
  82. break;
  83. case 'end':
  84. $value = '%' . $value;
  85. break;
  86. default:
  87. break;
  88. }
  89. }
  90. return $value;
  91. }
  92. /**
  93. * Escapes, quotes and adds escape symbol to LIKE expression.
  94. * For options and escaping see escapeLikeValue().
  95. *
  96. * @param string $value
  97. * @param array $options
  98. * @return \Zend_Db_Expr
  99. *
  100. * @see escapeLikeValue()
  101. */
  102. abstract public function addLikeEscape($value, $options = []);
  103. /**
  104. * Returns case insensitive LIKE construction.
  105. * For options and escaping see escapeLikeValue().
  106. *
  107. * @param string $field
  108. * @param string $value
  109. * @param array $options
  110. * @return \Zend_Db_Expr
  111. *
  112. * @see escapeLikeValue()
  113. */
  114. public function getCILike($field, $value, $options = [])
  115. {
  116. $quotedField = $this->getConnection()->quoteIdentifier($field);
  117. return new \Zend_Db_Expr($quotedField . ' LIKE ' . $this->addLikeEscape($value, $options));
  118. }
  119. }