Completed
Push — master ( bd1769...dc246b )
by Niels
03:23
created

BaseObject::getName()   A

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 0
1
<?php
2
3
/*
4
 * DirectAdmin API Client
5
 * (c) Omines Internetbureau B.V. - https://omines.nl/
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Omines\DirectAdmin\Objects;
12
13
use Omines\DirectAdmin\Context\UserContext;
14
use Omines\DirectAdmin\DirectAdminException;
15
16
/**
17
 * Basic wrapper around a DirectAdmin object as observed within a specific context.
18
 *
19
 * @author Niels Keurentjes <[email protected]>
20
 */
21
abstract class BaseObject
22
{
23
    /** @var string */
24
    private $name;
25
26
    /** @var UserContext */
27
    private $context;
28
29
    /** @var array */
30
    private $cache = [];
31
32
    /**
33
     * @param string $name Canonical name for the object
34
     * @param UserContext $context Context within which the object is valid
35
     */
36
    protected function __construct($name, UserContext $context)
37
    {
38
        $this->name = $name;
39
        $this->context = $context;
40
    }
41
42
    /**
43
     * Clear the object's internal cache.
44
     */
45
    public function clearCache()
46
    {
47
        $this->cache = [];
48
    }
49
50
    /**
51
     * Retrieves an item from the internal cache.
52
     *
53
     * @param string $key Key to retrieve
54
     * @param callable|mixed $default Either a callback or an explicit default value
55
     * @return mixed Cached value
56
     */
57
    protected function getCache($key, $default)
58
    {
59
        if (!isset($this->cache[$key])) {
60
            $this->cache[$key] = is_callable($default) ? $default() : $default;
61
        }
62
        return $this->cache[$key];
63
    }
64
65
    /**
66
     * Retrieves a keyed item from inside a cache item.
67
     *
68
     * @param string $key
69
     * @param string $item
70
     * @param callable|mixed $defaultKey
71
     * @param mixed|null $defaultItem
72
     * @return mixed Cached value
73
     */
74
    protected function getCacheItem($key, $item, $defaultKey, $defaultItem = null)
75
    {
76
        if (empty($cache = $this->getCache($key, $defaultKey))) {
77
            return $defaultItem;
78
        }
79
        if (!is_array($cache)) {
80
            throw new DirectAdminException("Cache item $key is not an array");
81
        }
82
        return isset($cache[$item]) ? $cache[$item] : $defaultItem;
83
    }
84
85
    /**
86
     * Sets a specific cache item, for when a cacheable value was a by-product.
87
     *
88
     * @param string $key
89
     * @param mixed $value
90
     */
91
    protected function setCache($key, $value)
92
    {
93
        $this->cache[$key] = $value;
94
    }
95
96
    /**
97
     * @return UserContext
98
     */
99
    public function getContext()
100
    {
101
        return $this->context;
102
    }
103
104
    /**
105
     * Protected as a derived class may want to offer the name under a different name.
106
     *
107
     * @return string
108
     */
109
    protected function getName()
110
    {
111
        return $this->name;
112
    }
113
114
    /**
115
     * Converts an array of string items to an associative array of objects of the specified type.
116
     *
117
     * @param array $items
118
     * @param string $class
119
     * @param UserContext $context
120
     * @return array
121
     */
122
    public static function toObjectArray(array $items, $class, UserContext $context)
123
    {
124
        return array_combine($items, array_map(function ($item) use ($class, $context) {
125
            return new $class($item, $context);
126
        }, $items));
127
    }
128
129
    /**
130
     * Converts an associative array of descriptors to objects of the specified type.
131
     *
132
     * @param array $items
133
     * @param string $class
134
     * @param UserContext $context
135
     * @return array
136
     */
137
    public static function toRichObjectArray(array $items, $class, UserContext $context)
138
    {
139
        array_walk($items, function (&$value, $name) use ($class, $context) {
140
            $value = new $class($name, $context, $value);
141
        });
142
        return $items;
143
    }
144
}
145