AbstractResolverMap::buildContextKey()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 1
nc 1
1
<?php
2
/**
3
 * @link    https://github.com/nnx-framework/entry-name-resolver
4
 * @author  Malofeykin Andrey  <[email protected]>
5
 */
6
namespace Nnx\EntryNameResolver;
7
8
/**
9
 * Class AbstractResolverMap
10
 *
11
 * @package Nnx\EntryNameResolver
12
 */
13
abstract class AbstractResolverMap implements EntryNameResolverInterface
14
{
15
    /**
16
     * Карта используемая для определения имени сервиса в зависимости от контекста вызова
17
     *
18
     * 'имяСервиса' => [
19
     *      'contextKey' => 'имяСервисаДляЭтогоМодуля'
20
     * ]
21
     *
22
     * @var array
23
     */
24
    protected $contextMap = [];
25
26
    /**
27
     * @inheritdoc
28
     *
29
     * @param      $entryName
30
     * @param null $context
31
     *
32
     * @return null|string
33
     */
34
    public function resolveEntryNameByContext($entryName, $context = null)
35
    {
36
        if (null === $context) {
37
            return null;
38
        }
39
        $contextKey = $this->buildContextKey($context);
40
41
        $map = $this->getContextMap();
42
        $resolvedEntryName = null;
43
        if (array_key_exists($entryName, $map) && is_array($map[$entryName]) && array_key_exists($contextKey, $map[$entryName])) {
44
            $resolvedEntryName = $map[$entryName][$contextKey];
45
        }
46
47
        return $resolvedEntryName;
48
    }
49
50
    /**
51
     * Преобразует контекст в ключ
52
     *
53
     * @param $context
54
     *
55
     * @return mixed
56
     */
57
    abstract public function buildContextKey($context);
58
59
    /**
60
     * Возвращает карту используемую для определения имени сервиса в зависимости от контекста вызова
61
     *
62
     * @return array
63
     */
64
    public function getContextMap()
65
    {
66
        return $this->contextMap;
67
    }
68
69
    /**
70
     * Устанавливает карту используемую для определения имени сервиса в зависимости от контекста вызова
71
     *
72
     * @param array $contextMap
73
     *
74
     * @return $this
75
     */
76
    public function setContextMap($contextMap)
77
    {
78
        $this->contextMap = $contextMap;
79
80
        return $this;
81
    }
82
}
83