Carton::data()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Session
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Session;
16
17
use Phossa2\Shared\Base\ObjectAbstract;
18
use Phossa2\Session\Traits\SessionAwareTrait;
19
use Phossa2\Session\Interfaces\CartonInterface;
20
use Phossa2\Session\Interfaces\SessionInterface;
21
22
/**
23
 * Carton
24
 *
25
 * @package Phossa2\Session
26
 * @author  Hong Zhang <[email protected]>
27
 * @version 2.1.0
28
 * @since   2.1.0 added
29
 */
30
class Carton extends ObjectAbstract implements CartonInterface
31
{
32
    use SessionAwareTrait;
33
34
    /**
35
     * carton name
36
     *
37
     * @var    string
38
     * @access protected
39
     */
40
    protected $name;
41
42
    /**
43
     * Constructor
44
     *
45
     * @param  string $name
46
     * @access public
47
     */
48
    public function __construct(
49
        /*# string */ $name = 'default',
50
        SessionInterface $session = null
51
    ) {
52
        // set box name
53
        $this->name = $name;
54
55
        // start session if not yet
56
        $this->setSession($session)->getSession()->start();
57
    }
58
59
    /**
60
     * {@inheritDoc}
61
     */
62
    public function __get(/*# string */ $name)
63
    {
64
        return $this->offsetGet($name);
65
    }
66
67
    /**
68
     * {@inheritDoc}
69
     */
70
    public function __set(/*# string */ $name, $value)
71
    {
72
        return $this->offsetSet($name, $value);
73
    }
74
75
    /**
76
     * {@inheritDoc}
77
     */
78
    public function __isset(/*# string */ $name)
79
    {
80
        return $this->offsetExists($name);
81
    }
82
83
    /**
84
     * {@inheritDoc}
85
     */
86
    public function __unset(/*# string */ $name)
87
    {
88
        return $this->offsetUnset($name);
89
    }
90
91
    /**
92
     * {@inheritDoc}
93
     */
94
    public function getName()/*# : string */
95
    {
96
        return $this->name;
97
    }
98
99
    /**
100
     * Convert to an array
101
     *
102
     * @return array
103
     * @access public
104
     * @api
105
     */
106
    public function toArray()/*# : array */
107
    {
108
        return $this->data()->getArrayCopy();
109
    }
110
111
    /**
112
     * {@inheritDoc}
113
     */
114
    public function fromArray(array $data)
115
    {
116
        $this->data()->exchangeArray($data);
117
        return $this;
118
    }
119
120
    public function offsetExists($offset)/*# : bool */
121
    {
122
        return isset($this->data()[$offset]);
123
    }
124
125
    public function offsetGet($offset)
126
    {
127
        if ($this->offsetExists($offset)) {
128
            return $this->data()[$offset];
129
        }
130
        return;
131
    }
132
133
    public function offsetSet($offset, $value)
134
    {
135
        $this->data()[$offset] = $value;
136
    }
137
138
    public function offsetUnset($offset)
139
    {
140
        unset($this->data()[$offset]);
141
    }
142
143
    public function count()
144
    {
145
        return count($this->data());
146
    }
147
148
    public function getIterator()
149
    {
150
        return new \ArrayIterator($this->data());
151
    }
152
153
    /**
154
     * Retrieve the data container
155
     *
156
     * @return ArrayObject
157
     * @access protected
158
     */
159
    protected function data()
160
    {
161
        return $this->getSession()->sessionData($this->name);
162
    }
163
}
164