This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContextAwareTrait.php
66 lines (60 loc) · 1.66 KB
/
ContextAwareTrait.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
<?php
namespace Dhii\Output;
use ArrayAccess;
use InvalidArgumentException;
use Psr\Container\ContainerInterface;
use stdClass;
/**
* Common functionality for objects that are aware of a context.
*
* @since 0.1
*/
trait ContextAwareTrait
{
/**
* The context.
*
* @since 0.1
*
* @var array|ArrayAccess|stdClass|ContainerInterface|null
*/
protected $context;
/**
* Retrieves the context associated with this instance.
*
* @since 0.1
*
* @return array|ArrayAccess|stdClass|ContainerInterface|null The context.
*/
protected function _getContext()
{
return $this->context;
}
/**
* Sets the context for this instance.
*
* @since 0.1
*
* @param array|ArrayAccess|stdClass|ContainerInterface|null $context The context instance, or null.
*/
protected function _setContext($context)
{
$this->context = ($context !== null)
? $this->_normalizeContainer($context)
: null;
}
/**
* Normalizes a container.
*
* @since [*next-version*]
*
* @param array|ArrayAccess|stdClass|ContainerInterface $container The container to normalize.
*
* @throws InvalidArgumentException If the container is invalid.
*
* @return array|ArrayAccess|stdClass|ContainerInterface Something that can be used with
* {@see ContainerGetCapableTrait#_containerGet()} or
* {@see ContainerHasCapableTrait#_containerHas()}.
*/
abstract protected function _normalizeContainer($container);
}