/
LocatorHelper.php
84 lines (70 loc) · 2.46 KB
/
LocatorHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
/**
* This file is part of the QA-Tools library.
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @copyright Alexander Obuhovich <aik.bold@gmail.com>
* @link https://github.com/qa-tools/qa-tools
*/
namespace QATools\QATools\BEM\ElementLocator;
use QATools\QATools\BEM\Exception\ElementException;
use QATools\QATools\PageObject\How;
class LocatorHelper
{
/**
* Returns block locator.
*
* @param string $block_name Block name.
* @param string|null $modificator_name Modificator name.
* @param string|null $modificator_value Modificator value.
*
* @return array
* @throws ElementException When block isn't specified.
*/
public function getBlockLocator($block_name, $modificator_name = null, $modificator_value = null)
{
if ( !$block_name ) {
throw new ElementException('BEM Block name cannot be empty', ElementException::TYPE_BLOCK_REQUIRED);
}
$class_name = $block_name . $this->getModificatorSelector($modificator_name, $modificator_value);
return array(How::CLASS_NAME => $class_name);
}
/**
* Returns element locator.
*
* @param string $element_name Element name.
* @param string $block_name Block name.
* @param string|null $modificator_name Modificator name.
* @param string|null $modificator_value Modificator value.
*
* @return array
* @throws ElementException When block/element isn't specified.
*/
public function getElementLocator($element_name, $block_name, $modificator_name = null, $modificator_value = null)
{
if ( !$element_name ) {
throw new ElementException('BEM element name cannot be empty', ElementException::TYPE_ELEMENT_REQUIRED);
}
if ( !$block_name ) {
throw new ElementException('BEM block name cannot be empty', ElementException::TYPE_BLOCK_REQUIRED);
}
$class_name = $block_name . '__' . $element_name . $this->getModificatorSelector($modificator_name, $modificator_value);
return array(How::CLASS_NAME => $class_name);
}
/**
* Returns block/element modificator selector.
*
* @param string|null $modificator_name Modificator name.
* @param string|null $modificator_value Modificator value.
*
* @return string
*/
protected function getModificatorSelector($modificator_name = null, $modificator_value = null)
{
if ( isset($modificator_name) && isset($modificator_value) ) {
return '_' . $modificator_name . '_' . $modificator_value;
}
return '';
}
}