Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractParser.php
Go to the documentation of this file.
1 <?php
7 
9 
13 abstract class AbstractParser implements I18n\ParserInterface
14 {
20  protected $_filesCollector = [];
21 
27  protected $_factory;
28 
34  protected $_adapters = [];
35 
41  protected $_phrases = [];
42 
49  public function __construct(I18n\FilesCollector $filesCollector, I18n\Factory $factory)
50  {
51  $this->_filesCollector = $filesCollector;
52  $this->_factory = $factory;
53  }
54 
63  {
64  $this->_adapters[$type] = $adapter;
65  }
66 
70  public function parse(array $parseOptions)
71  {
72  $this->_validateOptions($parseOptions);
73 
74  foreach ($parseOptions as $typeOptions) {
75  $this->_parseByTypeOptions($typeOptions);
76  }
77  return $this->_phrases;
78  }
79 
86  abstract protected function _parseByTypeOptions($options);
87 
95  protected function _validateOptions($parseOptions)
96  {
97  foreach ($parseOptions as $parserOptions) {
98  if (empty($parserOptions['type'])) {
99  throw new \InvalidArgumentException('Missed "type" in parser options.');
100  }
101  if (!isset($this->_adapters[$parserOptions['type']])) {
102  throw new \InvalidArgumentException(
103  sprintf('Adapter is not set for type "%s".', $parserOptions['type'])
104  );
105  }
106  if (!isset($parserOptions['paths']) || !is_array($parserOptions['paths'])) {
107  throw new \InvalidArgumentException('"paths" in parser options must be array.');
108  }
109  }
110  }
111 
118  protected function _getFiles($options)
119  {
120  $fileMask = isset($options['fileMask']) ? $options['fileMask'] : '';
121 
122  return $this->_filesCollector->getFiles($options['paths'], $fileMask);
123  }
124 
128  public function getPhrases()
129  {
130  return $this->_phrases;
131  }
132 }
$adapter
Definition: webapi_user.php:16
$type
Definition: item.phtml:13
addAdapter($type, AdapterInterface $adapter)
__construct(I18n\FilesCollector $filesCollector, I18n\Factory $factory)