Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractMapper.php
Go to the documentation of this file.
1 <?php
7 
12 use Psr\Log\LoggerInterface as Logger;
13 
19 abstract class AbstractMapper implements MapperInterface
20 {
26  protected $resourceModel;
27 
33  protected $resource;
34 
40  protected $joinedTables = [];
41 
47  protected $connection;
48 
54  protected $select;
55 
59  protected $logger;
60 
64  protected $fetchStrategy;
65 
69  protected $objectFactory;
70 
74  protected $mapperFactory;
75 
81  protected $map = [];
82 
90  public function __construct(
91  Logger $logger,
95  Select $select = null
96  ) {
97  $this->logger = $logger;
98  $this->fetchStrategy = $fetchStrategy;
99  $this->objectFactory = $objectFactory;
100  $this->mapperFactory = $mapperFactory;
101  $this->select = $select;
102  $this->init();
103  }
104 
110  abstract protected function init();
111 
118  public function map(CriteriaInterface $criteria)
119  {
120  $criteriaParts = $criteria->toArray();
121  foreach ($criteriaParts as $key => $value) {
123  $mapperMethod = 'map' . $camelCaseKey;
124  if (method_exists($this, $mapperMethod)) {
125  if (!is_array($value)) {
126  throw new \InvalidArgumentException('Wrong type of argument, expecting array for '. $mapperMethod);
127  }
128  call_user_func_array([$this, $mapperMethod], $value);
129  }
130  }
131  return $this->select;
132  }
133 
145  public function addExpressionFieldToSelect($alias, $expression, $fields)
146  {
147  // validate alias
148  if (!is_array($fields)) {
149  $fields = [$fields => $fields];
150  }
151  $fullExpression = $expression;
152  foreach ($fields as $fieldKey => $fieldItem) {
153  $fullExpression = str_replace('{{' . $fieldKey . '}}', $fieldItem, $fullExpression);
154  }
155  $this->getSelect()->columns([$alias => $fullExpression]);
156  }
157 
161  public function addFieldToFilter($field, $condition = null)
162  {
163  if (is_array($field)) {
164  $conditions = [];
165  foreach ($field as $key => $value) {
166  $conditions[] = $this->translateCondition($value, isset($condition[$key]) ? $condition[$key] : null);
167  }
168 
169  $resultCondition = '(' . implode(') ' . \Magento\Framework\DB\Select::SQL_OR . ' (', $conditions) . ')';
170  } else {
171  $resultCondition = $this->translateCondition($field, $condition);
172  }
173  $this->select->where($resultCondition, null, Select::TYPE_CONDITION);
174  }
175 
179  public function reset()
180  {
181  $this->getSelect()->reset();
182  }
183 
190  protected function setResourceModelName($model)
191  {
192  $this->resourceModel = $model;
193  }
194 
200  protected function getResourceModelName()
201  {
202  return $this->resourceModel;
203  }
204 
210  public function getResource()
211  {
212  if (empty($this->resource)) {
213  $this->resource = \Magento\Framework\App\ObjectManager::getInstance()->create(
214  $this->getResourceModelName()
215  );
216  }
217  return $this->resource;
218  }
219 
226  protected function initResource($resourceInterface)
227  {
228  $this->setResourceModelName($resourceInterface);
229  $this->setConnection($this->getResource()->getConnection());
230  if (!$this->select) {
231  $this->select = $this->getConnection()->select();
232  $this->initSelect();
233  }
234  }
235 
241  protected function initSelect()
242  {
243  $this->getSelect()->from(['main_table' => $this->getResource()->getMainTable()]);
244  }
245 
254  protected function join($table, $condition, $cols = '*')
255  {
256  if (is_array($table)) {
257  foreach ($table as $k => $v) {
258  $alias = $k;
259  $table = $v;
260  break;
261  }
262  } else {
263  $alias = $table;
264  }
265  if (!isset($this->joinedTables[$table])) {
266  $this->getSelect()->join([$alias => $this->getTable($table)], $condition, $cols);
267  $this->joinedTables[$alias] = true;
268  }
269  }
270 
276  protected function getConnection()
277  {
278  return $this->connection;
279  }
280 
288  protected function setConnection($connection)
289  {
290  if (!$connection instanceof \Magento\Framework\DB\Adapter\AdapterInterface) {
291  throw new \InvalidArgumentException(
292  (string)new \Magento\Framework\Phrase(
293  'dbModel read resource does not implement \Magento\Framework\DB\Adapter\AdapterInterface'
294  )
295  );
296  }
297  $this->connection = $connection;
298  }
299 
307  protected function translateCondition($field, $condition)
308  {
309  $field = $this->getMappedField($field);
310  return $this->getConditionSql($this->getConnection()->quoteIdentifier($field), $condition);
311  }
312 
319  protected function getMappedField($field)
320  {
321  $mapper = $this->getMapper();
322 
323  if (isset($mapper['fields'][$field])) {
324  $mappedField = $mapper['fields'][$field];
325  } else {
326  $mappedField = $field;
327  }
328 
329  return $mappedField;
330  }
331 
337  protected function getMapper()
338  {
339  if (isset($this->map)) {
340  return $this->map;
341  } else {
342  return false;
343  }
344  }
345 
377  protected function getConditionSql($fieldName, $condition)
378  {
379  return $this->getConnection()->prepareSqlCondition($fieldName, $condition);
380  }
381 
388  protected function getConditionFieldName($fieldName)
389  {
390  return $fieldName;
391  }
392 
397  protected function renderFiltersBefore()
398  {
399  }
400 
407  protected function getTable($table)
408  {
409  return $this->getResource()->getTable($table);
410  }
411 
417  protected function getSelect()
418  {
419  return $this->select;
420  }
421 }
map(CriteriaInterface $criteria)
getConditionSql($fieldName, $condition)
addFieldToFilter($field, $condition=null)
$fields
Definition: details.phtml:14
__construct(Logger $logger, FetchStrategyInterface $fetchStrategy, ObjectFactory $objectFactory, MapperFactory $mapperFactory, Select $select=null)
$value
Definition: gender.phtml:16
addExpressionFieldToSelect($alias, $expression, $fields)
join($table, $condition, $cols=' *')
if(!trim($html)) $alias
Definition: details.phtml:20
$table
Definition: trigger.php:14
const SQL_OR
Definition: Select.php:79