Completed
Pull Request — master (#18)
by Randy
01:46
created

JSendDataTrait::getData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
rs 9.9666
cc 2
nc 2
nop 2
1
<?php
2
3
namespace Demv\JSend;
4
5
/**
6
 * Trait JSendDataTrait
7
 * @package Demv\JSend
8
 */
9
trait JSendDataTrait
10
{
11
    /**
12
     * @var array
13
     */
14
    private $data = [];
15
16
    /**
17
     * @param string|null $key
18
     *
19
     * @return bool
20
     */
21
    final public function hasData(string $key = null): bool
22
    {
23
        if ($key === null) {
24
            return !empty($this->data);
25
        }
26
27
        return array_key_exists($key, $this->data ?? []);
28
    }
29
30
    /**
31
     * @param string|null $key
32
     * @param mixed|null  $default
33
     *
34
     * @return array|mixed|null
35
     */
36
    final public function getData(string $key = null, $default = null)
37
    {
38
        $data = $this->data ?? [];
39
        if ($key === null) {
40
            return $data;
41
        }
42
43
        return $data[$key] ?? $default;
44
    }
45
}
46