示例#1
0
 /**
  * Validates the attribute of the model.
  * If there is any error, the error message is added to the model.
  * @param RedBeanModel $model the model being validated
  * @param string $attribute the attribute being validated
  */
 protected function validateAttribute($object, $attribute)
 {
     $value = $object->{$attribute};
     if ($this->allowEmpty && $this->isEmpty($value)) {
         return;
     }
     switch ($this->type) {
         case 'blob':
         case 'longblob':
             return;
         case 'integer':
             $valid = preg_match('/^[-+]?[0-9]+$/', trim($value));
             // Not Coding Standard
             break;
         case 'float':
             $valid = preg_match('/^[-+]?([0-9]*\\.)?[0-9]+([eE][-+]?[0-9]+)?$/', trim($value));
             // Not Coding Standard
             break;
         case 'date':
             $valid = DateTimeUtil::isValidDbFormattedDate($value);
             break;
         case 'time':
             $valid = CDateTimeParser::parse($value, $this->timeFormat) !== false;
             break;
         case 'datetime':
             $valid = DateTimeUtil::isValidDbFormattedDateTime($value);
             break;
         case 'array':
             $valid = is_array($value);
             break;
         case 'string':
         default:
             return;
     }
     if (!$valid) {
         if ($this->message !== null) {
             $message = $this->message;
         } else {
             $message = Zurmo::t('Core', '{attribute} must be {type}.');
         }
         $this->addError($object, $attribute, $message, array('{type}' => $this->type));
     }
 }
示例#2
0
 public function testIsValidDbFormattedDateTime()
 {
     $this->assertTrue(DateTimeUtil::isValidDbFormattedDateTime('2011-09-23 23:23:23'));
     $this->assertTrue(DateTimeUtil::isValidDbFormattedDateTime('1756-01-01 00:59:59'));
     $this->assertFalse(DateTimeUtil::isValidDbFormattedDateTime('0011-09-23 23:23:23'));
     $this->assertFalse(DateTimeUtil::isValidDbFormattedDateTime('2011-13-32 23:23:23'));
     $this->assertFalse(DateTimeUtil::isValidDbFormattedDateTime('1011-09-23 24:23:23'));
     $this->assertFalse(DateTimeUtil::isValidDbFormattedDateTime('2011-12-32 23:23:23'));
     $this->assertFalse(DateTimeUtil::isValidDbFormattedDateTime('2011-12-32 23:60:23'));
     $this->assertFalse(DateTimeUtil::isValidDbFormattedDateTime('2011-12-32 23:23:60'));
     $this->assertFalse(DateTimeUtil::isValidDbFormattedDateTime('cascacasc'));
 }
 /**
  * Get array of modified items since beginning or since datetime in past
  * @param $params
  * @return ApiResult
  * @throws ApiException
  */
 public function processGetModifiedItems($params)
 {
     try {
         $modelClassName = $this->getModelName();
         $stateMetadataAdapterClassName = $this->resolveStateMetadataAdapterClassName();
         if (!isset($params['sinceDateTime'])) {
             $sinceTimestamp = 0;
         } else {
             if (DateTimeUtil::isValidDbFormattedDateTime($params['sinceDateTime'])) {
                 $sinceTimestamp = DateTimeUtil::convertDbFormatDateTimeToTimestamp($params['sinceDateTime']);
             } else {
                 $message = 'sinceDateTime format is not correct. sinceDateTime should be in "YYYY-MM-DD HH:MM:SS" format';
                 throw new ApiException($message);
             }
         }
         $pageSize = Yii::app()->pagination->getGlobalValueByType('apiListPageSize');
         if (isset($params['pagination']['pageSize'])) {
             $pageSize = (int) $params['pagination']['pageSize'];
         }
         // Get offset. Please note that API client provide page number, and we need to convert it into offset,
         // which is parameter of RedBeanModel::getSubset function
         if (isset($params['pagination']['page']) && (int) $params['pagination']['page'] > 0) {
             $currentPage = (int) $params['pagination']['page'];
         } else {
             $currentPage = 1;
         }
         $offset = $this->getOffsetFromCurrentPageAndPageSize($currentPage, $pageSize);
         $models = ModelStateChangesSubscriptionUtil::getUpdatedModels($modelClassName, $pageSize, $offset, $sinceTimestamp, $stateMetadataAdapterClassName, Yii::app()->user->userModel);
         $totalItems = ModelStateChangesSubscriptionUtil::getUpdatedModelsCount($modelClassName, $sinceTimestamp, $stateMetadataAdapterClassName, Yii::app()->user->userModel);
         $data = array('totalCount' => $totalItems, 'pageSize' => $pageSize, 'currentPage' => $currentPage);
         if (is_array($models) && !empty($models)) {
             foreach ($models as $model) {
                 $data['items'][] = static::getModelToApiDataUtilData($model);
             }
             $result = new ApiResult(ApiResponse::STATUS_SUCCESS, $data, null, null);
         } else {
             $result = new ApiResult(ApiResponse::STATUS_SUCCESS, $data, null, null);
         }
     } catch (Exception $e) {
         $message = $e->getMessage();
         throw new ApiException($message);
     }
     return $result;
 }