1: <?php
2:
3: namespace RedBeanPHP;
4:
5: use RedBeanPHP\OODBBean as OODBBean;
6:
7: /**
8: * SimpleModel
9: * Base Model For All RedBeanPHP Models using FUSE.
10: *
11: * RedBeanPHP FUSE is a mechanism to connect beans to posthoc
12: * models. Models are connected to beans by naming conventions.
13: * Actions on beans will result in actions on models.
14: *
15: * @file RedBeanPHP/SimpleModel.php
16: * @author Gabor de Mooij and the RedBeanPHP Team
17: * @license BSD/GPLv2
18: *
19: * @copyright
20: * copyright (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community
21: * This source file is subject to the BSD/GPLv2 License that is bundled
22: * with this source code in the file license.txt.
23: */
24: class SimpleModel
25: {
26: /**
27: * @var OODBBean
28: */
29: protected $bean;
30:
31: /**
32: * Used by FUSE: the ModelHelper class to connect a bean to a model.
33: * This method loads a bean in the model.
34: *
35: * @param OODBBean $bean bean to load
36: *
37: * @return void
38: */
39: public function loadBean( OODBBean $bean )
40: {
41: $this->bean = $bean;
42: }
43:
44: /**
45: * Magic Getter to make the bean properties available from
46: * the $this-scope.
47: *
48: * @note this method returns a value, not a reference!
49: * To obtain a reference unbox the bean first!
50: *
51: * @param string $prop property to get
52: *
53: * @return mixed
54: */
55: public function __get( $prop )
56: {
57: return $this->bean->$prop;
58: }
59:
60: /**
61: * Magic Setter.
62: * Sets the value directly as a bean property.
63: *
64: * @param string $prop property to set value of
65: * @param mixed $value value to set
66: *
67: * @return void
68: */
69: public function __set( $prop, $value )
70: {
71: $this->bean->$prop = $value;
72: }
73:
74: /**
75: * Isset implementation.
76: * Implements the isset function for array-like access.
77: *
78: * @param string $key key to check
79: *
80: * @return boolean
81: */
82: public function __isset( $key )
83: {
84: return isset( $this->bean->$key );
85: }
86:
87: /**
88: * Box the bean using the current model.
89: * This method wraps the current bean in this model.
90: * This method can be reached using FUSE through a simple
91: * OODBBean. The method returns a RedBeanPHP Simple Model.
92: * This is useful if you would like to rely on PHP type hinting.
93: * You can box your beans before passing them to functions or methods
94: * with typed parameters.
95: *
96: * @return SimpleModel
97: */
98: public function box()
99: {
100: return $this;
101: }
102:
103: /**
104: * Unbox the bean from the model.
105: * This method returns the bean inside the model.
106: *
107: * @return OODBBean
108: */
109: public function unbox()
110: {
111: return $this->bean;
112: }
113: }
114: