Completed
Pull Request — master (#399)
by Anton
04:06
created

Container::setFromArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
ccs 0
cts 7
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Bluz Framework Component
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link https://github.com/bluzphp/framework
7
 */
8
9
declare(strict_types=1);
10
11
namespace Bluz\Common\Container;
12
13
/**
14
 * Container of data for object
15
 *
16
 * @package  Bluz\Common
17
 * @author   Anton Shevchuk
18
 * @link     https://github.com/bluzphp/framework/wiki/Trait-Container
19
 */
20
trait Container
21
{
22
    /**
23
     * @var array Container of elements
24
     */
25
    protected $container = [];
26
27
    /**
28
     * Set key/value pair
29
     *
30
     * @param  string $key
31
     * @param  mixed  $value
32
     * @return void
33
     */
34
    protected function doSetContainer($key, $value)
35
    {
36
        $this->container[$key] = $value;
37
    }
38
39
    /**
40
     * Get value by key
41
     *
42
     * @param  string $key
43
     * @return mixed
44
     */
45
    protected function doGetContainer($key)
46
    {
47
        if ($this->doContainsContainer($key)) {
48
            return $this->container[$key];
49
        } else {
50
            return null;
51
        }
52
    }
53
54
    /**
55
     * Check contains key in container
56
     *
57
     * @param  string $key
58
     * @return bool
59
     */
60
    protected function doContainsContainer($key)
61
    {
62
        return array_key_exists($key, $this->container);
63
    }
64
65
    /**
66
     * Delete value by key
67
     *
68
     * @param  string $key
69
     * @return void
70
     */
71
    protected function doDeleteContainer($key)
72
    {
73
        unset($this->container[$key]);
74
    }
75
76
    /**
77
     * Sets all data in the row from an array
78
     *
79
     * @param  array $data
80
     * @return self
81
     */
82
    public function setFromArray(array $data)
83
    {
84
        foreach ($data as $key => $value) {
85
            $this->container[$key] = $value;
86
        }
87
        return $this;
88
    }
89
90
    /**
91
     * Returns the column/value data as an array
92
     *
93
     * @return array
94
     */
95
    public function toArray()
96
    {
97
        return $this->container;
98
    }
99
100
    /**
101
     * Reset container array
102
     *
103
     * @return self
104
     */
105
    public function resetArray()
106
    {
107
        foreach ($this->container as &$value) {
108
            $value = null;
109
        }
110
        return $this;
111
    }
112
}
113