Transformator::locator2Xpath()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Webino (http://webino.sk)
4
 *
5
 * @link        https://github.com/webino/WebinoDraw for the canonical source repository
6
 * @copyright   Copyright (c) 2012-2017 Webino, s. r. o. (http://webino.sk)
7
 * @author      Peter Bačinský <[email protected]>
8
 * @license     BSD-3-Clause
9
 */
10
11
namespace WebinoDraw\Dom\Locator;
12
13
use ArrayObject;
14
use WebinoDraw\Dom\Factory\LocatorStrategyFactory;
15
use WebinoDraw\Dom\Locator\TransformatorInterface;
16
use WebinoDraw\Exception;
17
18
/**
19
 * 
20
 */
21
class Transformator extends ArrayObject implements
22
    TransformatorInterface
23
{
24
    /**
25
     * @var LocatorStrategyFactory
26
     */
27
    protected $strategyFactory;
28
29
    /**
30
     * @param array $array
31
     */
32
    public function __construct(array $array = [])
33
    {
34
        parent::__construct($array);
35
    }
36
37
    /**
38
     * @return LocatorStrategyFactory
39
     */
40
    public function getStrategyFactory()
41
    {
42
        if (null === $this->strategyFactory) {
43
            $this->setStrategyFactory(new LocatorStrategyFactory);
44
        }
45
46
        return $this->strategyFactory;
47
    }
48
49
    /**
50
     * @param LocatorStrategyFactory $factory
51
     * @return Transformator
52
     */
53
    public function setStrategyFactory(LocatorStrategyFactory $factory)
54
    {
55
        $this->strategyFactory = $factory;
56
        return $this;
57
    }
58
59
    /**
60
     * @param string $index Strategy type
61
     * @return mixed
62
     */
63
    public function offsetGet($index)
64
    {
65
        if (!$this->offsetExists($index)) {
66
            $this->offsetSet(
67
                $index,
68
                $this->getStrategyFactory()->createStrategy($index)
69
            );
70
        }
71
72
        return parent::offsetGet($index);
73
    }
74
75
    /**
76
     * Add strategy
77
     *
78
     * @param string $index Strategy type
79
     * @param TransformatorInterface $strategy
80
     * @throws Exception\UnexpectedValueException
81
     */
82
    public function offsetSet($index, $strategy)
83
    {
84
        if (!($strategy instanceof TransformatorInterface)) {
85
            throw new Exception\UnexpectedValueException(
86
                'Expected newval as TransformatorInterface, but provided ' . gettype($strategy)
87
            );
88
        }
89
90
        return parent::offsetSet($index, $strategy);
91
    }
92
93
    /**
94
     * Select strategy and transform locator to XPath
95
     *
96
     * @param string $locator
97
     * @return string
98
     */
99
    public function locator2Xpath($locator)
100
    {
101
        $match = [];
102
        preg_match('~^(([a-z]+)\=)?(.+)~', $locator, $match);
103
        return $this->offsetGet($match[2])->locator2Xpath($match[3]);
104
    }
105
}
106