ObjectResolver::getServiceId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Di
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Di\Resolver;
16
17
use Phossa2\Di\Container;
18
use Phossa2\Shared\Base\ObjectAbstract;
19
use Phossa2\Di\Traits\ContainerAwareTrait;
20
use Phossa2\Config\Interfaces\ConfigInterface;
21
use Phossa2\Di\Interfaces\ContainerAwareInterface;
22
use Phossa2\Shared\Delegator\DelegatorAwareInterface;
23
24
/**
25
 * ObjectResolver
26
 *
27
 * A config wrapper of container for service instance lookup
28
 *
29
 * @package Phossa2\Di
30
 * @author  Hong Zhang <[email protected]>
31
 * @see     ConfigInterface
32
 * @see     ContainerAwareInterface
33
 * @version 2.0.0
34
 * @since   2.0.0 added
35
 */
36
class ObjectResolver extends ObjectAbstract implements ConfigInterface, ContainerAwareInterface
37
{
38
    use ContainerAwareTrait;
39
40
    /**
41
     * @param  Container $container
42
     * @access public
43
     */
44
    public function __construct(Container $container)
45
    {
46
        $this->setContainer($container);
47
    }
48
49
    /**
50
     * Get '#service_id' from the container
51
     *
52
     * {@inheritDoc}
53
     */
54
    public function get(/*# string */ $id, $default = null)
55
    {
56
        if ($this->has($id)) {
57
            return $this->getRealContainer()->get(static::getRawId($id));
58
        } else {
59
            return $default;
60
        }
61
    }
62
63
    /**
64
     * Has '#service_id' in the container ?
65
     *
66
     * {@inheritDoc}
67
     */
68
    public function has(/*# string */ $id)/*# : bool */
69
    {
70
        if (static::isServiceId($id)) {
71
            return $this->getRealContainer()->has(static::getRawId($id));
72
        }
73
        return false;
74
    }
75
76
    /**
77
     * Get container or its delegator
78
     *
79
     * @return ContainerInterface
80
     * @access protected
81
     */
82
    protected function getRealContainer()/*# : ContainerInterface */
83
    {
84
        $cont = $this->getContainer();
85
86
        // get delegator recursively
87
        if ($cont instanceof DelegatorAwareInterface &&
88
            $cont->hasDelegator()
89
        ) {
90
            $cont = $cont->getDelegator(true);
91
        }
92
93
        return $cont;
94
    }
95
96
    /**
97
     * Convert '#service_id' to 'service_id'
98
     *
99
     * @param  string $serviceId
100
     * @return string
101
     * @access protected
102
     */
103
    public static function getRawId(/*# string */ $serviceId)/*# : string */
104
    {
105
        return substr($serviceId, 1);
106
    }
107
108
    /**
109
     * Convert 'service_id' to '#service_id'
110
     *
111
     * @param  string $rawId
112
     * @return string
113
     * @access public
114
     * @static
115
     */
116
    public static function getServiceId(/*# string */ $rawId)/*# : string */
117
    {
118
        return '#' . $rawId;
119
    }
120
121
    /**
122
     * Is $serviceId something like '#service_id' ?
123
     *
124
     * @param  mixed $serviceId
125
     * @return bool
126
     * @access public
127
     * @static
128
     */
129
    public static function isServiceId($serviceId)/*# : bool */
130
    {
131
        if (is_string($serviceId) &&
132
            isset($serviceId[0]) &&
133
            '#' == $serviceId[0]
134
        ) {
135
            return true;
136
        }
137
        return false;
138
    }
139
}
140