1
0
Fork 0
mirror of https://github.com/Oreolek/ifhub.club.git synced 2024-07-01 14:05:07 +03:00
ifhub.club/engine/classes/Entity.class.php

130 lines
3.5 KiB
PHP
Raw Normal View History

<?php
2008-09-21 09:36:57 +03:00
/*-------------------------------------------------------
*
* LiveStreet Engine Social Networking
* Copyright © 2008 Mzhelskiy Maxim
*
*--------------------------------------------------------
*
* Official site: www.livestreet.ru
* Contact e-mail: rus.engine@gmail.com
*
* GNU General Public License, version 2:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*
---------------------------------------------------------
*/
/**
* Абстрактный класс сущности
*
*/
abstract class Entity extends Object {
2008-09-21 09:36:57 +03:00
protected $_aData=array();
protected $sPrimaryKey = null;
2008-09-21 09:36:57 +03:00
/**
* Если передать в конструктор ассоциативный массив свойств и их значений, то они автоматом загрузятся в сущность
*
* @param array|null $aParam
2008-09-21 09:36:57 +03:00
*/
public function __construct($aParam = false) {
$this->_setData($aParam);
}
public function _setData($aData) {
if(is_array($aData)) {
foreach ($aData as $sKey => $val) {
2008-09-21 09:36:57 +03:00
$this->_aData[$sKey] = $val;
}
}
}
public function _getData($aKeys=array()) {
if(!is_array($aKeys) or !count($aKeys)) return $this->_aData;
$aReturn=array();
foreach ($aKeys as $key) {
2010-08-13 09:12:19 +03:00
if(array_key_exists($key,$this->_aData)) {
$aReturn[$key] = $this->_aData[$key];
}
}
return $aReturn;
}
2010-08-13 09:12:19 +03:00
public function _getDataOne($sKey) {
if(array_key_exists($sKey,$this->_aData)) {
return $this->_aData[$sKey];
}
return null;
}
/**
* Рекурсивное преобразование объекта и вложенных объектов в массив
*/
public function _getDataArray()
{
$aResult = array();
foreach ($this->_aData as $sKey => $sValue) {
if (is_object($sValue) && $sValue instanceOf Entity) {
$aResult[$sKey] = $sValue->_getDataArray();
} else {
$aResult[$sKey] = $sValue;
}
}
return $aResult;
}
/**
* Ставим хук на вызов неизвестного метода и считаем что хотели вызвать метод какого либо модуля
*
* @param string $sName
* @param array $aArgs
* @return unknown
*/
public function __call($sName,$aArgs) {
$sType=strtolower(substr($sName,0,3));
2010-08-03 23:13:44 +03:00
if (!strpos($sName,'_') and in_array($sType,array('get','set'))) {
2010-08-13 18:40:21 +03:00
$sKey=func_underscore(substr($sName,3));
if ($sType=='get') {
2010-08-03 23:13:44 +03:00
if (isset($this->_aData[$sKey])) {
return $this->_aData[$sKey];
2010-08-03 23:13:44 +03:00
} else {
if (preg_match('/Entity([^_]+)/',get_class($this),$sModulePrefix)) {
2010-08-13 18:40:21 +03:00
$sModulePrefix=func_underscore($sModulePrefix[1]).'_';
2010-08-03 23:13:44 +03:00
if (isset($this->_aData[$sModulePrefix.$sKey])) {
return $this->_aData[$sModulePrefix.$sKey];
}
}
}
return null;
2010-08-13 09:12:19 +03:00
} elseif ($sType=='set' and array_key_exists(0,$aArgs)) {
$this->_aData[$sKey]=$aArgs[0];
}
} else {
return Engine::getInstance()->_CallModule($sName,$aArgs);
}
}
/**
* Получение первичного ключа сущности (ключ, а не значение!)
*/
public function _getPrimaryKey()
{
if (!$this->sPrimaryKey) {
if (isset($this->_aData['id'])) {
$this->sPrimaryKey = 'id';
} else {
// Получение primary_key из схемы бд (пока отсутствует)
$this->sPrimaryKey = 'id';
}
}
return $this->sPrimaryKey;
}
public function _getPrimaryKeyValue() {
return $this->_getDataOne($this->_getPrimaryKey());
}
2008-09-21 09:36:57 +03:00
}
?>