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

JSendDataTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 0
dl 0
loc 37
c 0
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A hasData() 0 8 2
A getData() 0 9 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