root/trunk/lib/AkActiveRecord/AkActiveRecordMock.php

Revision 1399, 4.9 kB (checked in by bermi, 6 months ago)

When loading associations using load(), it will always return false is no associates are found.

Converting most Active record files to vanilla PHP5

Line 
1 <?php
2 class AkActiveRecordMockHandler
3 {
4     public $_parent, $_association_id;
5     public function __construct(&$parent, $association_id)
6     {
7         $this->_parent = $parent;
8         $this->_association_id = $association_id;
9     }
10     public function load()
11     {
12         if (!empty($this->_parent)) {
13             $assoc = $this->_association_id;
14             return $this->_parent->$assoc;
15         }
16         return false;
17     }
18     
19     public function __call($name, $args)
20     {
21         $handler = &$this->_parent->_getHandlerForAssociation($this->_association_id);
22         return call_user_func_array(array($handler, $name),$args);
23     }
24 }
25 class AkActiveRecordMock
26 {
27     public $_parent,$_class,$_pkValue,$_handler;
28     public $load_acts = false;
29     public $load_associations = true;
30     public $__associations=array();
31     public $__handlers = array();
32     public $_dummy_instance;
33     public function __construct($pk,$class, $handler, &$parent)
34     {
35         $this->_class = $class;
36         $this->_pkValue = $pk;
37         $this->_handler = $handler;
38         $this->_parent = &$parent;
39     }
40     
41     public function getId()
42     {
43         return $this->_pkValue;
44     }
45     public function isCallable($method)
46     {
47         return is_callable(array($this->_class,$method));
48     }
49     public function get($name)
50     {
51         return isset($this->$name)?$this->$name:null;
52     }
53     
54     public function getAttribute($name)
55     {
56         return $this->get($name);
57     }
58     public function &_getHandlerForAssociation($association_id)
59     {
60         $false = false;
61         if (isset($this->__handlers[$association_id])) {
62             $class = $this->_class;
63             $obj=&$this->_getObject();
64             $handler_name = $this->__handlers[$association_id];
65             $myobj  = new $class();
66             if (isset($myobj->$handler_name)) {
67                 $handler = $myobj->$handler_name;
68                 $handler->Owner = &$obj;
69                 $obj->$handler_name = &$handler;
70                 $obj->$handler_name->_loaded=true;
71                 return $obj->$handler_name;
72             }
73         } else {
74             $class = $this->_class;
75             $obj=&$this->_getObject();
76             $handler_name = $obj->getCollectionHandlerName($association_id);
77             if(!$handler_name) {
78                 $handler_name = $association_id;
79             }
80             $myobj  = new $class();
81             if (isset($myobj->$handler_name)) {
82                 $handler = &$myobj->$handler_name;
83                 $handler->Owner = &$obj;
84                 $obj->$handler_name = &$handler;
85                 return $obj->$handler_name;
86             }
87         }
88         return $false;
89     }
90     public function _getAssociationId($handler_name)
91     {
92         return isset($this->__associations[$handler_name])?$this->__associations[$handler_name]:false;
93     }
94     public function load()
95     {
96         if (!empty($this->_parent)) {
97             $assoc = $this->_parent->_getAssociationId($this->_handler);
98             return $this->_parent->$assoc;
99         }
100         return false;
101     }
102     public function _addAssociation($association_id, $handler_name)
103     {
104         //Ak::getLogger()->message('addAssociation on '.$this->_getClass().' with association_id:'.$association_id.' handler_name:'.$handler_name);
105         if ($association_id != $handler_name) {
106             $this->$handler_name = &new AkActiveRecordMockHandler($this,$association_id);
107         }
108         if(is_object($this->$handler_name)) {
109             $this->$handler_name->_loaded=true;
110             $this->__associations[$handler_name] = $association_id;
111             $this->__handlers[$association_id] = $handler_name;
112         }
113     }
114     public function &_getObject()
115     {
116         static $obj;
117         
118         if (!empty($obj)) return $obj;
119         $class = $this->_class;
120         $object_vars = get_object_vars($this);
121         $attributes = array();
122         $associations = array();
123         foreach($object_vars as $key => $value) {
124             if (!($is_association=in_array($key, $this->__associations)) && is_scalar($value)) {
125                 $attributes[$key]=$value;
126             } else if ($is_association) {
127                 $associations[]=$key;
128             }
129         }
130   
131         $obj =& new $class('attributes', $attributes);
132         
133         $obj->_newRecord = false;
134         foreach($associations as $assoc) {
135             
136             $handler_name = $this->__handlers[$assoc];
137             $obj->$handler_name = new AkActiveRecordMockHandler($this, $assoc);
138             $obj->$assoc = &$this->$assoc;
139         }
140         return $obj;
141      }
142
143     
144     public function _getClass()
145     {
146         return $this->_class;
147     }
148     
149     public function __call($name, $args = array())
150     {
151         $obj = &$this->_getObject();
152         if($obj) {
153             //Ak::getLogger()->message('calling '.$name.' on '.$this->_getClass());
154             if (method_exists(&$obj,$name)) {
155                 return call_user_func_array(array(&$obj,$name),$args);
156             }
157         }
158         
159         
160     }
161 }
162 ?>
Note: See TracBrowser for help on using the browser.