Created
April 15, 2016 09:04
-
-
Save nask0/a9dd64551d8c7f91282bc10df07dda2d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace CloseContacts\Lib\DataServices; | |
use \Phalcon\Mvc\Model\Manager as ModelsManager; | |
use \Phalcon\Mvc\Model\Transaction\Failed as TxFailed; | |
use \Phalcon\Mvc\Model\Transaction\Manager as TxManager; | |
use \CloseContacts\Lib\Application\Services\Validation; | |
use \CloseContacts\Lib\Utilities\Date; | |
abstract class DataServiceAbstract | |
{ | |
/* @var array */ | |
private $_errors; | |
/* @var \Phalcon\Mvc\Model\Transaction\Manager */ | |
private $_txManager; | |
/* @var \Phalcon\Mvc\Model\Manager */ | |
private $_modelsManager; | |
/* @var \Phalcon\Mvc\Model\Manager */ | |
private $_validatorService; | |
/* @var \CloseContacts\Lib\Application\Services\Validation */ | |
private $_validationService; | |
/* @var string */ | |
protected $_modelsNs = ''; | |
/** | |
* @param \Phalcon\Mvc\Model\Manager $modelsManager | |
* @param \Phalcon\Mvc\Model\Transaction\Manager $txManager | |
* @param \CloseContacts\Lib\Application\Services\Validation $validation | |
*/ | |
public function __construct( ModelsManager $modelsManager, TxManager $txManager, Validation $validation ) | |
{ | |
$this->_errors = []; | |
$this->_txManager = $txManager; | |
$this->_modelsNs = $modelsManager->getNamespaceAlias( 'models' ) . '\\'; | |
$this->_modelsManager = $modelsManager; | |
$this->_validationService = $validation; | |
} | |
/** | |
* @param string $model | |
* @return \Phalcon\Validation | |
*/ | |
public function getValidator( $model, array $data = [], $bindEntity = false ) | |
{ | |
return $this->_validationService->get( $model, $data, $bindEntity ); | |
} | |
public function getValidationErrors() | |
{ | |
return (array) $this->_errors; | |
} | |
/** | |
* Whether data service has any errors. | |
* | |
* @return bool | |
*/ | |
public function hasValidationErrors() | |
{ | |
return (bool) count( $this->_errors ); | |
} | |
/** | |
* Returns a new Transaction or an already created once. | |
* | |
* @return \Phalcon\Mvc\Model\Transaction | |
*/ | |
protected function _getTransaction() | |
{ | |
return $this->_txManager->get(true); | |
} | |
/** | |
* @return \Phalcon\Mvc\Model\Manager | |
*/ | |
protected function _getModelsManager() | |
{ | |
return $this->_modelsManager; | |
} | |
/** | |
* @return \Phalcon\Mvc\Model\Transaction\Manager | |
*/ | |
protected function _getTxManager() | |
{ | |
return $this->_txManager; | |
} | |
/** | |
* @return string - Models namespace | |
*/ | |
protected function _getModelsNs( $model = '' ) | |
{ | |
return !empty($model) ? $this->_modelsNs . (string) $model : $this->_modelsNs; | |
} | |
/** | |
* @param $from - model name to crete query builder from | |
* @return \Phalcon\Mvc\Model\Query\Builder | |
*/ | |
protected function _createBuilder( $from ) | |
{ | |
if( is_array($from) ) { | |
$models = []; | |
foreach( $from as $model ) { | |
$models[$model] = $this->_modelsNs . $model; | |
} | |
return $this->_modelsManager->createBuilder()->from( $models ); | |
} | |
return $this->_modelsManager->createBuilder()->from( [ $from => $this->_modelsNs . $from ] ); | |
} | |
/** | |
* Append error to Data Service internal | |
* errors log (format is "$issue : $error"). | |
* | |
* @param string $field | |
* @param string $error | |
*/ | |
protected function _setValidationError( $field, $error ) | |
{ | |
$this->_errors[ (string) $field ] = (string) $error; | |
} | |
protected function _setValidationErrors( $errors ) | |
{ | |
} | |
/** | |
* Clear's internal Data Service log. | |
* | |
* @param void | |
* @return void | |
*/ | |
protected function _clearValidationErrors() | |
{ | |
$this->_errors = []; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment