* implemented the toArray method and test
[vsc.git] / _res / _libs / models / foo / fooentitya.class.php
blob476bd55d6933c03eed179bb1df1eb37c07d4f2bd
1 <?php
2 /**
3 * The abstract object entity - it represents an entry in the database.
4 * It can be composed from more Entity Objects using reflection
6 * @package ts_models
7 * @author Marius Orcsik <marius@habarnam.ro>
8 * @date 09.02.26
9 */
10 usingPackage ('models/foo/fields');
11 usingPackage ('models/foo/indexes');
13 abstract class fooEntityA {
14 protected $_name;
15 private $_alias;
16 private $_pk;
17 private $_indexes = array ();
19 public function __call ($sMethodName, $aParameters) {
20 // d ($sMethodName, $aParameters);
21 $i = preg_match ('/(set|get)(.*)/i', $sMethodName, $found );
22 if ($i) {
23 $sMethod = $found[1];
24 $sProperty = $found[2];
26 $sProperty[0] = strtolower ($sProperty[0]); // lowering the first letter
29 if ( $sMethod == 'set' ) {
30 // check for fields with $found[1] name
31 $this->$sProperty->setValue($aParameters[0]);
32 return true;
33 } else if ( $sMethod == 'get' ) {
34 return $this->$sProperty->getValue();
36 // throw new tsExceptionUnimplemented ('Method [' . get_class ($this) . '::' . $sMethodName . ']');
39 public function __get ($sPropertyName) {
40 throw new tsExceptionUnimplemented ('Property [' . get_class ($this) . '::' . $sPropertyName . '] doesn\'t exist');
43 public function __set ($sPropertyName, $mValue) {
44 throw new tsExceptionUnimplemented ('Property [' . get_class ($this) . '::' . $sPropertyName . '] doesn\'t exist');
47 /**
48 * @param string $sName
49 * @return void
51 protected function setName ($sName) {
52 $this->_name = $sName;
55 public function getName () {
56 return $this->_name;
59 /**
60 * @param fooFieldA $oIndex
61 * @return void
63 public function setPrimaryKey () {
64 $this->_pk = new fooKeyPrimary (func_get_args());
67 public function getPrimaryKey () {
68 return $this->_pk;
71 /**
72 * @return fooFieldA[]
74 public function getMembers () {
75 $aMembers = array();
76 $oReflector = new ReflectionClass (get_class($this));
77 $aReflectedProperties = $oReflector->getProperties ();
79 foreach ($aReflectedProperties as $oProperty) {
80 if ($oProperty->isPublic()) // this will need to be fixed asap (ie, php 5.3.0)
81 $aMembers[] = $oProperty->getValue ($this);
84 return $aMembers;
87 /**
88 * gets all the column names as an array
89 * @return string[]
91 public function getColumnNames () {
92 $aRet = array();
93 $oReflector = new ReflectionClass (get_class($this));
94 $aReflectedProperties = $oReflector->getProperties ();
96 foreach ($aReflectedProperties as $oProperty) {
97 if ($oProperty->isPublic()) {
98 $sName = $oProperty->getName();
99 $aRet[] = $sName;
103 return $aRet;
106 public function getIndexes ($bWithPrimaryKey = false) {
107 $aIndexes = array ();
108 if ($bWithPrimaryKey)
109 $aIndexes[] = $this->getPrimaryKey();
111 $aIndexes = array_merge($aIndexes, $this->_indexes);
113 return $aIndexes;
117 * returns an array of key=>value for all properties of the current object
118 * @return mixed[]
120 public function toArray () {
121 $aRet = array();
122 $oReflector = new ReflectionClass (get_class($this));
123 $aReflectedProperties = $oReflector->getProperties ();
125 foreach ($aReflectedProperties as $oProperty) {
126 if ($oProperty->isPublic()) {
127 $sName = $oProperty->getName();
128 $sGetter = 'get' . ucfirst($sName);
129 $aRet[$sName] = $this->$sGetter();
133 return $aRet;
137 * Receives an array of keys=> values and constructs an entity based on
138 * the existing ones.
139 * Returns:
140 * 1 if all array keys existed as properties of the object
141 * 0 if one of the keys didn't exist as a property of the object
142 * 2 if there were properties which didn't have a corresponding key=>value pair
143 * @param mixed[string] $aIncArray
144 * @return int
146 public function fromArray ($aIncArray) {
147 foreach ($aIncArray as $sFieldName => $mValue) {
148 $sSetter = 'set' . ucfirst($sFieldName);
149 try {
150 $this->$sSetter ($mValue);
151 } catch (Exception $e) {
152 // dunno what might be thrown here
153 d ($e);
154 return 0;
157 return 1;
162 * @param fooEntityA $oChild
163 * @return bool
165 public function loadChild (fooEntityA $oChild) {}