Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.

Commit 2a07be9

Browse files
committed
Models need certain functions (setData, setRawData, save and Array Access functions
1 parent be67664 commit 2a07be9

File tree

1 file changed

+80
-3
lines changed

1 file changed

+80
-3
lines changed

library/smCore/Model.php

Lines changed: 80 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,90 @@
1616
*
1717
* The Initial Developer of the Original Code is the smCore project.
1818
*
19-
* Portions created by the Initial Developer are Copyright (C) 2011
19+
* Portions created by the Initial Developer are Copyright (C) 2013
2020
* the Initial Developer. All Rights Reserved.
2121
*/
2222

2323
namespace smCore;
2424

25-
abstract class Model
25+
abstract class Model implements ArrayAccess
2626
{
27-
// @todo
27+
protected $_data = array();
28+
29+
/**
30+
* setData - Set the modules data.
31+
*
32+
* @return null
33+
*/
34+
abstract public function setData(array $data);
35+
36+
/**
37+
* save - Use a storage object to save the current models data
38+
*
39+
* @return null
40+
*/
41+
abstract public function save();
42+
43+
public function setRawData(array $data)
44+
{
45+
foreach ($data as $key => $value)
46+
{
47+
$this->_data[$key] = $value;
48+
}
49+
}
50+
51+
/**
52+
* ArrayAccess - implementation for empty/isset/array_key_exists/etc.
53+
*
54+
* @param mixed $offset
55+
*
56+
* @return boolean
57+
*/
58+
public function offsetExists($offset)
59+
{
60+
return isset($this->_data[$offset]);
61+
}
62+
63+
/**
64+
* ArrayAccess - implementation for getting data via array syntax
65+
*
66+
* @param mixed $offset Name of the key, usually a string
67+
*
68+
* @return boolean
69+
*/
70+
public function offsetGet($offset)
71+
{
72+
if (array_key_exists($offset, $this->_data))
73+
{
74+
return $this->_data[$offset];
75+
}
76+
77+
return false;
78+
}
79+
80+
/**
81+
* ArrayAccess - implementation for setting data via array syntax
82+
*
83+
* @param mixed $offset Name of the key, usually a string
84+
* @param mixed $value
85+
*/
86+
public function offsetSet($offset, $value)
87+
{
88+
if ('password' === $offset)
89+
{
90+
throw new Exception('User passwords cannot be set via array access.');
91+
}
92+
93+
$this->_data[$offset] = $value;
94+
}
95+
96+
/**
97+
* ArrayAccess - implementation for unsetting data via array syntax
98+
*
99+
* @param mixed $offset Name of the key, usually a string
100+
*/
101+
public function offsetUnset($offset)
102+
{
103+
unset($this->_data[$offset]);
104+
}
28105
}

0 commit comments

Comments
 (0)