mirror of
https://github.com/Oreolek/ifhub.club.git
synced 2024-06-29 04:55:02 +03:00
130 lines
3.5 KiB
PHP
130 lines
3.5 KiB
PHP
<?php
|
||
/*-------------------------------------------------------
|
||
*
|
||
* 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 {
|
||
protected $_aData=array();
|
||
protected $sPrimaryKey = null;
|
||
|
||
/**
|
||
* Если передать в конструктор ассоциативный массив свойств и их значений, то они автоматом загрузятся в сущность
|
||
*
|
||
* @param array|null $aParam
|
||
*/
|
||
public function __construct($aParam = false) {
|
||
$this->_setData($aParam);
|
||
}
|
||
|
||
public function _setData($aData) {
|
||
if(is_array($aData)) {
|
||
foreach ($aData as $sKey => $val) {
|
||
$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) {
|
||
if(array_key_exists($key,$this->_aData)) {
|
||
$aReturn[$key] = $this->_aData[$key];
|
||
}
|
||
}
|
||
return $aReturn;
|
||
}
|
||
|
||
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));
|
||
if (!strpos($sName,'_') and in_array($sType,array('get','set'))) {
|
||
$sKey=func_underscore(substr($sName,3));
|
||
if ($sType=='get') {
|
||
if (isset($this->_aData[$sKey])) {
|
||
return $this->_aData[$sKey];
|
||
} else {
|
||
if (preg_match('/Entity([^_]+)/',get_class($this),$sModulePrefix)) {
|
||
$sModulePrefix=func_underscore($sModulePrefix[1]).'_';
|
||
if (isset($this->_aData[$sModulePrefix.$sKey])) {
|
||
return $this->_aData[$sModulePrefix.$sKey];
|
||
}
|
||
}
|
||
}
|
||
return null;
|
||
} 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());
|
||
}
|
||
}
|
||
?>
|