Files
luos/workflow/engine/src/ProcessMaker/BusinessModel/Validator.php

333 lines
9.7 KiB
PHP
Raw Normal View History

2014-03-05 16:28:21 -04:00
<?php
namespace ProcessMaker\BusinessModel;
2014-03-05 16:28:21 -04:00
/**
* Validator fields
*
* @author Brayan Pereyra (Cochalo) <brayan@colosa.com>
* @copyright Colosa - Bolivia
*
* @protected
*/
class Validator
{
2014-03-05 16:28:21 -04:00
/**
* Validate dep_uid
* @var string $dep_uid. Uid for Departament
* @var string $nameField. Name of field for message
*
* @access public
* @author Brayan Pereyra (Cochalo) <brayan@colosa.com>
* @copyright Colosa - Bolivia
*
* @return string
*/
static public function depUid($dep_uid, $nameField = 'dep_uid')
{
$dep_uid = trim($dep_uid);
if ($dep_uid == '') {
throw (new \Exception(\G::LoadTranslation("ID_DEPARTMENT_NOT_EXIST", array($nameField,''))));
2014-03-05 16:28:21 -04:00
}
$oDepartment = new \Department();
if (!($oDepartment->existsDepartment($dep_uid))) {
throw (new \Exception(\G::LoadTranslation("ID_DEPARTMENT_NOT_EXIST", array($nameField,$dep_uid))));
2014-03-05 16:28:21 -04:00
}
return $dep_uid;
}
/**
* Validate dep_status
* @var string $dep_uid. Uid for Departament
* @var string $nameField. Name of field for message
*
* @access public
* @author Brayan Pereyra (Cochalo) <brayan@colosa.com>
* @copyright Colosa - Bolivia
*
* @return string
*/
static public function depStatus($dep_status)
{
$dep_status = trim($dep_status);
$values = array('ACTIVE', 'INACTIVE');
if (!in_array($dep_status, $values)) {
throw (new \Exception(\G::LoadTranslation("ID_DEPARTMENT_NOT_EXIST", array('dep_status',$dep_status))));
2014-03-05 16:28:21 -04:00
}
return $dep_status;
}
/**
* Validate usr_uid
2014-03-18 12:31:36 -04:00
*
* @param string $usr_uid, Uid for user
* @param string $nameField . Name of field for message
2014-03-05 16:28:21 -04:00
*
* @access public
* @author Brayan Pereyra (Cochalo) <brayan@colosa.com>
* @copyright Colosa - Bolivia
*
* @return string
*/
static public function usrUid($usr_uid, $nameField = 'usr_uid')
{
$usr_uid = trim($usr_uid);
if ($usr_uid == '') {
throw (new \Exception(\G::LoadTranslation("ID_USER_NOT_EXIST", array($nameField,''))));
2014-03-05 16:28:21 -04:00
}
$oUsers = new \Users();
if (!($oUsers->userExists($usr_uid))) {
throw (new \Exception(\G::LoadTranslation("ID_USER_NOT_EXIST", array($nameField,$usr_uid))));
2014-03-05 16:28:21 -04:00
}
return $usr_uid;
}
2014-03-18 12:31:36 -04:00
/**
* Validate app_uid
*
* @param string $app_uid, Uid for application
* @param string $nameField . Name of field for message
*
* @access public
* @author Brayan Pereyra (Cochalo) <brayan@colosa.com>
* @copyright Colosa - Bolivia
*
* @return string
*/
static public function appUid($app_uid, $nameField = 'app_uid')
{
$app_uid = trim($app_uid);
if ($app_uid == '') {
throw (new \Exception(\G::LoadTranslation("ID_APPLICATION_NOT_EXIST", array($nameField,''))));
2014-03-18 12:31:36 -04:00
}
$oApplication = new \Application();
if (!($oApplication->exists($app_uid))) {
throw (new \Exception(\G::LoadTranslation("ID_APPLICATION_NOT_EXIST", array($nameField,$app_uid))));
2014-03-18 12:31:36 -04:00
}
return $app_uid;
}
/**
* Validate app_uid
*
* @param string $tri_uid, Uid for trigger
* @param string $nameField . Name of field for message
*
* @access public
* @author Brayan Pereyra (Cochalo) <brayan@colosa.com>
* @copyright Colosa - Bolivia
*
* @return string
*/
static public function triUid($tri_uid, $nameField = 'tri_uid')
{
$tri_uid = trim($tri_uid);
if ($tri_uid == '') {
throw (new \Exception(\G::LoadTranslation("ID_TRIGGER_NOT_EXIST", array($nameField,''))));
2014-03-18 12:31:36 -04:00
}
$oTriggers = new \Triggers();
if (!($oTriggers->TriggerExists($tri_uid))) {
throw (new \Exception(\G::LoadTranslation("ID_TRIGGER_NOT_EXIST", array($nameField,$tri_uid))));
2014-03-18 12:31:36 -04:00
}
return $tri_uid;
}
2014-03-24 16:40:01 -04:00
/**
* Validate pro_uid
*
* @param string $pro_uid, Uid for process
* @param string $nameField . Name of field for message
*
* @access public
* @author Brayan Pereyra (Cochalo) <brayan@colosa.com>
* @copyright Colosa - Bolivia
*
* @return string
*/
static public function proUid($pro_uid, $nameField = 'pro_uid')
{
$pro_uid = trim($pro_uid);
if ($pro_uid == '') {
throw (new \Exception(\G::LoadTranslation("ID_PROCESS_NOT_EXIST", array($nameField,''))));
2014-03-24 16:40:01 -04:00
}
$oProcess = new \Process();
if (!($oProcess->exists($pro_uid))) {
throw (new \Exception(\G::LoadTranslation("ID_PROCESS_NOT_EXIST", array($nameField,$pro_uid))));
2014-03-24 16:40:01 -04:00
}
return $pro_uid;
}
/**
* Validate cat_uid
*
* @param string $cat_uid, Uid for category
* @param string $nameField . Name of field for message
*
* @access public
* @author Brayan Pereyra (Cochalo) <brayan@colosa.com>
* @copyright Colosa - Bolivia
*
* @return string
*/
static public function catUid($cat_uid, $nameField = 'cat_uid')
{
$cat_uid = trim($cat_uid);
if ($cat_uid == '') {
throw (new \Exception(\G::LoadTranslation("ID_CATEGORY_NOT_EXIST", array($nameField,''))));
2014-03-24 16:40:01 -04:00
}
$oCategory = new \ProcessCategory();
if (!($oCategory->exists($cat_uid))) {
throw (new \Exception(\G::LoadTranslation("ID_CATEGORY_NOT_EXIST", array($nameField,$cat_uid))));
2014-03-24 16:40:01 -04:00
}
return $cat_uid;
}
2014-03-18 12:31:36 -04:00
/**
* Validate date
*
* @param string $date, Date for validate
* @param string $nameField . Name of field for message
*
* @access public
* @author Brayan Pereyra (Cochalo) <brayan@colosa.com>
* @copyright Colosa - Bolivia
*
* @return string
*/
static public function isDate($date, $format = 'Y-m-d H:i:s', $nameField = 'app_uid')
{
$date = trim($date);
if ($date == '') {
throw (new \Exception(\G::LoadTranslation("ID_DATE_NOT_VALID", array('',$format))));
2014-03-18 12:31:36 -04:00
}
$d = \DateTime::createFromFormat($format, $date);
if (!($d && $d->format($format) == $date)) {
throw (new \Exception(\G::LoadTranslation("ID_DATE_NOT_VALID", array($date,$format))));
2014-03-18 12:31:36 -04:00
}
return $date;
}
/**
* Validate is array
* @var array $field. Field type array
*
* @access public
* @author Brayan Pereyra (Cochalo) <brayan@colosa.com>
* @copyright Colosa - Bolivia
*
* @return void
*/
static public function isArray($field, $nameField)
{
if (!is_array($field)) {
throw (new \Exception(\G::LoadTranslation("ID_INVALID_VALUE_ARRAY", array($nameField))));
}
}
2014-03-25 16:22:28 -04:00
/**
* Validate is string
* @var array $field. Field type string
*
* @access public
* @author Brayan Pereyra (Cochalo) <brayan@colosa.com>
* @copyright Colosa - Bolivia
*
* @return void
*/
static public function isString($field, $nameField)
{
if (!is_string($field)) {
throw (new \Exception(\G::LoadTranslation("ID_INVALID_VALUE_STRING", array($nameField))));
2014-03-25 16:22:28 -04:00
}
}
/**
* Validate is integer
* @var array $field. Field type integer
*
* @access public
* @author Brayan Pereyra (Cochalo) <brayan@colosa.com>
* @copyright Colosa - Bolivia
*
* @return void
*/
static public function isInteger($field, $nameField)
{
if (!is_integer($field)) {
throw (new \Exception(\G::LoadTranslation("ID_INVALID_VALUE_INTEGER", array($nameField))));
2014-03-25 16:22:28 -04:00
}
}
/**
* Validate is boolean
* @var boolean $field. Field type boolean
*
* @access public
* @author Brayan Pereyra (Cochalo) <brayan@colosa.com>
* @copyright Colosa - Bolivia
*
* @return void
*/
static public function isBoolean($field, $nameField)
{
if (!is_bool($field)) {
throw (new \Exception(\G::LoadTranslation("ID_INVALID_VALUE_BOOLEAN", array($nameField))));
}
}
/**
* Validate is boolean
* @var boolean $field. Field type boolean
*
* @access public
* @author Brayan Pereyra (Cochalo) <brayan@colosa.com>
* @copyright Colosa - Bolivia
*
* @return void
*/
static public function isNotEmpty($field, $nameField)
{
if (empty($field)) {
throw (new \Exception(\G::LoadTranslation("ID_INVALID_VALUE_IS_EMPTY", array($nameField))));
}
}
2014-03-05 16:28:21 -04:00
/**
* Verify if data is array
*
* @param string $data Data
* @param string $dataNameForException Data name for the exception
*
* return void Throw exception if data is not array
*/
public function throwExceptionIfDataIsNotArray($data, $dataNameForException)
{
try {
if (!is_array($data)) {
throw new \Exception(\G::LoadTranslation("ID_INVALID_VALUE_THIS_MUST_BE_ARRAY", array($dataNameForException)));
}
} catch (\Exception $e) {
throw $e;
}
}
/**
* Verify if data is empty
*
* @param string $data Data
* @param string $dataNameForException Data name for the exception
*
* return void Throw exception if data is empty
*/
public function throwExceptionIfDataIsEmpty($data, $dataNameForException)
{
try {
if (empty($data)) {
throw new \Exception(\G::LoadTranslation("ID_INVALID_VALUE_CAN_NOT_BE_EMPTY", array($dataNameForException)));
}
} catch (\Exception $e) {
throw $e;
}
}
}
2014-03-05 16:28:21 -04:00