Completed
Push — master ( 198927...2a15a4 )
by Cheren
02:34
created

Element::loadMeta()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
/**
3
 * CakeCMS Core
4
 *
5
 * This file is part of the of the simple cms based on CakePHP 3.
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @package   Core
10
 * @license   MIT
11
 * @copyright MIT License http://www.opensource.org/licenses/mit-license.php
12
 * @link      https://github.com/CakeCMS/Core".
13
 * @author    Sergey Kalistratov <[email protected]>
14
 */
15
16
namespace Core\Element;
17
18
use JBZoo\Utils\FS;
19
use JBZoo\Utils\Str;
20
use JBZoo\Data\Data;
21
use JBZoo\Data\JSON;
22
use JBZoo\Data\PHPArray;
23
use Cake\Core\Configure;
24
use Core\ORM\Entity\Element as EntityElement;
25
26
/**
27
 * Class Element
28
 *
29
 * @package Core\Element
30
 */
31
abstract class Element
32
{
33
34
    const MANIFEST_FILE = 'element.php';
35
36
    /**
37
     * @var Data
38
     */
39
    public $config;
40
41
    /**
42
     * @var string
43
     */
44
    public $id;
45
46
    /**
47
     * @var string
48
     */
49
    protected $_elementGroup;
50
51
    /**
52
     * @var string
53
     */
54
    protected $_elementType;
55
56
    /**
57
     * @var EntityElement
58
     */
59
    protected $_entity;
60
61
    /**
62
     * @var PHPArray
63
     */
64
    protected $_meta;
65
66
    /**
67
     * Element constructor.
68
     *
69
     * @param string $type
70
     * @param string $group
71
     */
72
    public function __construct($type, $group)
73
    {
74
        $this->_elementType  = Str::low($type);
75
        $this->_elementGroup = Str::low($group);
76
    }
77
78
    /**
79
     * Get entity object.
80
     *
81
     * @return EntityElement
82
     */
83
    public function getEntity()
84
    {
85
        return $this->_entity;
86
    }
87
88
    /**
89
     * Get meta data by key.
90
     *
91
     * @param string|int $key
92
     * @param mixed $default
93
     * @param mixed $filter
94
     * @return mixed
95
     */
96
    public function getMetaData($key, $default = null, $filter = null)
97
    {
98
        return $this->loadMeta()->find('meta.' . $key, $default, $filter);
99
    }
100
101
    /**
102
     * Get element name.
103
     *
104
     * @return null|string
105
     */
106
    public function getName()
107
    {
108
        return $this->getMetaData('name');
109
    }
110
111
    /**
112
     * Get element path.
113
     *
114
     * @return bool|string
115
     */
116
    public function getPath()
117
    {
118
        $typeDir  = ucfirst($this->_elementType);
119
        $groupDir = ucfirst($this->_elementGroup);
120
121
        foreach ($this->_getPaths() as $path) {
122
            $fullPath = FS::clean($path . '/' . $groupDir . '/' . $typeDir, '/');
123
            if (FS::isDir($fullPath)) {
124
                return $fullPath;
125
            }
126
        }
127
128
        return false;
129
    }
130
131
    /**
132
     * On initialize element.
133
     *
134
     * @return void
135
     */
136
    public function initialize()
137
    {
138
    }
139
140
    /**
141
     * Check is core element.
142
     *
143
     * @return bool
144
     */
145
    public function isCore()
146
    {
147
        return $this->getMetaData('core', false, 'bool');
148
    }
149
150
    /**
151
     * Load element meta data.
152
     *
153
     * @return PHPArray
154
     */
155
    public function loadMeta()
156
    {
157
        if (!$this->_meta) {
158
            $this->_meta = new PHPArray($this->getPath() . '/' . self::MANIFEST_FILE);
159
        }
160
161
        return $this->_meta;
162
    }
163
164
    /**
165
     * Setup element config data.
166
     *
167
     * @param array $config
168
     */
169
    public function setConfig(array $config)
170
    {
171
        $this->config = new JSON($config);
172
    }
173
174
    /**
175
     * Setup entity object.
176
     *
177
     * @param EntityElement $entity
178
     * @return void
179
     */
180
    public function setEntity(EntityElement $entity)
181
    {
182
        $this->_entity = $entity;
183
    }
184
185
    /**
186
     * Get element paths.
187
     *
188
     * @return array
189
     */
190
    protected function _getPaths()
191
    {
192
        return (array) Configure::read('App.paths.elements');
193
    }
194
}
195