HasMeta   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 12
c 3
b 1
f 0
dl 0
loc 58
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A meta() 0 11 3
A uuid() 0 3 1
A replaceMeta() 0 3 1
A addMeta() 0 6 2
1
<?php
2
3
4
namespace Riclep\Storyblok\Traits;
5
6
7
trait HasMeta
8
{
9
	/**
10
	 * @var array stores the meta items for this class
11
	 */
12
	protected array $_meta = [];
13
14
	/**
15
	 * Returns the meta items
16
	 *
17
	 * @param string|null $key the key to return
18
	 * @param string|null $default a default if the $key is missing
19
	 * @return mixed
20
	 */
21
	public function meta(string $key = null, string $default = null): mixed
22
	{
23
		if ($key) {
24
			if (array_key_exists($key, $this->_meta)) {
25
				return $this->_meta[$key];
26
			}
27
28
			return $default;
29
		}
30
31
		return $this->_meta;
32
	}
33
34
	/**
35
	 * Adds items to the meta content optionally replacing existing keys
36
	 *
37
	 * @param $fields
38
	 */
39
	public function addMeta($fields, $replace = false): void
40
	{
41
		if ($replace) {
42
			$this->_meta = array_merge($this->_meta, $fields);
43
		}
44
		$this->_meta = array_merge($fields, $this->_meta);
45
	}
46
47
	/**
48
	 * Replaces a meta item
49
	 *
50
	 * @param $key
51
	 * @param $value
52
	 */
53
	public function replaceMeta($key, $value): void
54
	{
55
		$this->_meta[$key] = $value;
56
	}
57
58
	/**
59
	 * Returns the UUID of the Block
60
	 * @return string
61
	 */
62
	public function uuid(): string
63
	{
64
		return $this->meta('_uid');
65
	}
66
}