Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SectionObjectHandler.php
Go to the documentation of this file.
1 <?php
8 
15 
17 {
18  const SECTION = 'section';
19  const ELEMENT = 'element';
20  const TYPE = 'type';
21  const SELECTOR = 'selector';
22  const LOCATOR_FUNCTION = 'locatorFunction';
23  const TIMEOUT = 'timeout';
24  const PARAMETERIZED = 'parameterized';
25  const SECTION_NAME_ERROR_MSG = "Section names cannot contain non alphanumeric characters.\tSection='%s'";
26  const ELEMENT_NAME_ERROR_MSG = "Element names cannot contain non alphanumeric characters.\tElement='%s'";
27 
33  private static $INSTANCE;
34 
40  private $sectionObjects = [];
41 
48  private function __construct()
49  {
51  $parser = $objectManager->get(SectionParser::class);
52  $parserOutput = $parser->getData(self::SECTION);
53 
54  if (!$parserOutput) {
55  return;
56  }
57 
58  foreach ($parserOutput as $sectionName => $sectionData) {
59  $elements = [];
60 
61  if (preg_match('/[^a-zA-Z0-9_]/', $sectionName)) {
62  throw new XmlException(sprintf(self::SECTION_NAME_ERROR_MSG, $sectionName));
63  }
64 
65  try {
66  foreach ($sectionData[SectionObjectHandler::ELEMENT] as $elementName => $elementData) {
67  if (preg_match('/[^a-zA-Z0-9_]/', $elementName)) {
68  throw new XmlException(sprintf(self::ELEMENT_NAME_ERROR_MSG, $elementName, $sectionName));
69  }
70  $elementType = $elementData[SectionObjectHandler::TYPE];
71  $elementSelector = $elementData[SectionObjectHandler::SELECTOR] ?? null;
72  $elementLocatorFunc = $elementData[SectionObjectHandler::LOCATOR_FUNCTION] ?? null;
73  $elementTimeout = $elementData[SectionObjectHandler::TIMEOUT] ?? null;
74  $elementParameterized = $elementData[SectionObjectHandler::PARAMETERIZED] ?? false;
75 
76  $elements[$elementName] = new ElementObject(
78  $elementType,
79  $elementSelector,
80  $elementLocatorFunc,
81  $elementTimeout,
82  $elementParameterized
83  );
84  }
85  } catch (XmlException $exception) {
86  throw new XmlException($exception->getMessage() . " in Section '{$sectionName}'");
87  }
88 
89  $this->sectionObjects[$sectionName] = new SectionObject($sectionName, $elements);
90  }
91  }
92 
99  public static function getInstance()
100  {
101  if (!self::$INSTANCE) {
102  self::$INSTANCE = new SectionObjectHandler();
103  }
104 
105  return self::$INSTANCE;
106  }
107 
114  public function getObject($name)
115  {
116  if (array_key_exists($name, $this->getAllObjects())) {
117  return $this->getAllObjects()[$name];
118  }
119 
120  return null;
121  }
122 
128  public function getAllObjects()
129  {
130  return $this->sectionObjects;
131  }
132 }
$objectManager
Definition: bootstrap.php:17
$elementName
Definition: gallery.phtml:10
if(!isset($_GET['name'])) $name
Definition: log.php:14