當前位置: 首頁>>代碼示例>>PHP>>正文


PHP Doctrine_Validator::isValidType方法代碼示例

本文整理匯總了PHP中Doctrine_Validator::isValidType方法的典型用法代碼示例。如果您正苦於以下問題:PHP Doctrine_Validator::isValidType方法的具體用法?PHP Doctrine_Validator::isValidType怎麽用?PHP Doctrine_Validator::isValidType使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Doctrine_Validator的用法示例。


在下文中一共展示了Doctrine_Validator::isValidType方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: testIsValidType

 public function testIsValidType()
 {
     $var = '123';
     $this->assertTrue(Doctrine_Validator::isValidType($var, 'string'));
     $this->assertTrue(Doctrine_Validator::isValidType($var, 'integer'));
     $this->assertTrue(Doctrine_Validator::isValidType($var, 'float'));
     $this->assertFalse(Doctrine_Validator::isValidType($var, 'array'));
     $this->assertFalse(Doctrine_Validator::isValidType($var, 'object'));
     $var = 123;
     $this->assertTrue(Doctrine_Validator::isValidType($var, 'string'));
     $this->assertTrue(Doctrine_Validator::isValidType($var, 'integer'));
     $this->assertTrue(Doctrine_Validator::isValidType($var, 'float'));
     $this->assertFalse(Doctrine_Validator::isValidType($var, 'array'));
     $this->assertFalse(Doctrine_Validator::isValidType($var, 'object'));
     $var = 123.12;
     $this->assertTrue(Doctrine_Validator::isValidType($var, 'string'));
     $this->assertFalse(Doctrine_Validator::isValidType($var, 'integer'));
     $this->assertTrue(Doctrine_Validator::isValidType($var, 'float'));
     $this->assertFalse(Doctrine_Validator::isValidType($var, 'array'));
     $this->assertFalse(Doctrine_Validator::isValidType($var, 'object'));
     $var = '123.12';
     $this->assertTrue(Doctrine_Validator::isValidType($var, 'string'));
     $this->assertFalse(Doctrine_Validator::isValidType($var, 'integer'));
     $this->assertTrue(Doctrine_Validator::isValidType($var, 'float'));
     $this->assertFalse(Doctrine_Validator::isValidType($var, 'array'));
     $this->assertFalse(Doctrine_Validator::isValidType($var, 'object'));
     $var = '';
     $this->assertTrue(Doctrine_Validator::isValidType($var, 'string'));
     $this->assertFalse(Doctrine_Validator::isValidType($var, 'integer'));
     $this->assertFalse(Doctrine_Validator::isValidType($var, 'float'));
     $this->assertFalse(Doctrine_Validator::isValidType($var, 'array'));
     $this->assertFalse(Doctrine_Validator::isValidType($var, 'object'));
     $var = null;
     $this->assertTrue(Doctrine_Validator::isValidType($var, 'string'));
     $this->assertTrue(Doctrine_Validator::isValidType($var, 'integer'));
     $this->assertTrue(Doctrine_Validator::isValidType($var, 'float'));
     $this->assertTrue(Doctrine_Validator::isValidType($var, 'array'));
     $this->assertTrue(Doctrine_Validator::isValidType($var, 'object'));
     $var = 'str';
     $this->assertTrue(Doctrine_Validator::isValidType($var, 'string'));
     $this->assertFalse(Doctrine_Validator::isValidType($var, 'integer'));
     $this->assertFalse(Doctrine_Validator::isValidType($var, 'float'));
     $this->assertFalse(Doctrine_Validator::isValidType($var, 'array'));
     $this->assertFalse(Doctrine_Validator::isValidType($var, 'object'));
     $var = array();
     $this->assertFalse(Doctrine_Validator::isValidType($var, 'string'));
     $this->assertFalse(Doctrine_Validator::isValidType($var, 'integer'));
     $this->assertFalse(Doctrine_Validator::isValidType($var, 'float'));
     $this->assertTrue(Doctrine_Validator::isValidType($var, 'array'));
     $this->assertFalse(Doctrine_Validator::isValidType($var, 'object'));
     $var = new Exception();
     $this->assertFalse(Doctrine_Validator::isValidType($var, 'string'));
     $this->assertFalse(Doctrine_Validator::isValidType($var, 'integer'));
     $this->assertFalse(Doctrine_Validator::isValidType($var, 'float'));
     $this->assertFalse(Doctrine_Validator::isValidType($var, 'array'));
     $this->assertTrue(Doctrine_Validator::isValidType($var, 'object'));
 }
開發者ID:dracony,項目名稱:forked-php-orm-benchmark,代碼行數:57,代碼來源:ValidatorTestCase.php

示例2: validateField

 /**
  * validateField
  *
  * @param string $name
  * @param string $value
  * @param Doctrine_Record $record
  * @return Doctrine_Validator_ErrorStack $errorStack
  */
 public function validateField($fieldName, $value, Doctrine_Record $record = null)
 {
     if ($record instanceof Doctrine_Record) {
         $errorStack = $record->getErrorStack();
     } else {
         $record = $this->create();
         $errorStack = new Doctrine_Validator_ErrorStack($this->getOption('name'));
     }
     if ($value === self::$_null) {
         $value = null;
     } else {
         if ($value instanceof Doctrine_Record && $value->exists()) {
             $value = $value->getIncremented();
         } else {
             if ($value instanceof Doctrine_Record && !$value->exists()) {
                 foreach ($this->getRelations() as $relation) {
                     if ($fieldName == $relation->getLocalFieldName() && (get_class($value) == $relation->getClass() || is_subclass_of($value, $relation->getClass()))) {
                         return $errorStack;
                     }
                 }
             }
         }
     }
     $dataType = $this->getTypeOf($fieldName);
     // Validate field type, if type validation is enabled
     if ($this->getAttribute(Doctrine::ATTR_VALIDATE) & Doctrine::VALIDATE_TYPES) {
         if (!Doctrine_Validator::isValidType($value, $dataType)) {
             $errorStack->add($fieldName, 'type');
         }
         if ($dataType == 'enum') {
             $enumIndex = $this->enumIndex($fieldName, $value);
             if ($enumIndex === false && $value !== null) {
                 $errorStack->add($fieldName, 'enum');
             }
         }
     }
     // Validate field length, if length validation is enabled
     if ($this->getAttribute(Doctrine::ATTR_VALIDATE) & Doctrine::VALIDATE_LENGTHS) {
         if (!Doctrine_Validator::validateLength($value, $dataType, $this->getFieldLength($fieldName))) {
             $errorStack->add($fieldName, 'length');
         }
     }
     // Run all custom validators
     foreach ($this->getFieldValidators($fieldName) as $validatorName => $args) {
         if (!is_string($validatorName)) {
             $validatorName = $args;
             $args = array();
         }
         $validator = Doctrine_Validator::getValidator($validatorName);
         $validator->invoker = $record;
         $validator->field = $fieldName;
         $validator->args = $args;
         if (!$validator->validate($value)) {
             $errorStack->add($fieldName, $validator);
         }
     }
     return $errorStack;
 }
開發者ID:mediasadc,項目名稱:alba,代碼行數:66,代碼來源:Table.php

示例3: validateField

 /**
  * Validates a given field using table ATTR_VALIDATE rules.
  * @see Doctrine_Core::ATTR_VALIDATE
  *
  * @param string $fieldName
  * @param string $value
  * @param Doctrine_Record $record   record to consider; if it does not exists, it is created
  * @return Doctrine_Validator_ErrorStack $errorStack
  */
 public function validateField($fieldName, $value, Doctrine_Record $record = null)
 {
     if ($record instanceof Doctrine_Record) {
         $errorStack = $record->getErrorStack();
     } else {
         $record = $this->create();
         $errorStack = new Doctrine_Validator_ErrorStack($this->getOption('name'));
     }
     if ($value === self::$_null) {
         $value = null;
     } else {
         if ($value instanceof Doctrine_Record && $value->exists()) {
             $value = $value->getIncremented();
         } else {
             if ($value instanceof Doctrine_Record && !$value->exists()) {
                 foreach ($this->getRelations() as $relation) {
                     if ($fieldName == $relation->getLocalFieldName() && (get_class($value) == $relation->getClass() || is_subclass_of($value, $relation->getClass()))) {
                         return $errorStack;
                     }
                 }
             } elseif (array_key_exists($fieldName, $this->getRelationIdentifiers()) && $record !== null) {
                 $r = $this->_relationIdentifiers[$fieldName];
                 // Related record is not saved yet
                 if (!$record->hasReference($r) || !$record->{$r}->exists()) {
                     return $errorStack;
                 }
             }
         }
     }
     $dataType = $this->getTypeOf($fieldName);
     // Validate field type, if type validation is enabled
     if ($this->getAttribute(Doctrine_Core::ATTR_VALIDATE) & Doctrine_Core::VALIDATE_TYPES) {
         if (!Doctrine_Validator::isValidType($value, $dataType)) {
             $errorStack->add($fieldName, 'type');
         }
         if ($dataType == 'enum') {
             $enumIndex = $this->enumIndex($fieldName, $value);
             if ($enumIndex === false && $value !== null) {
                 $errorStack->add($fieldName, 'enum');
             }
         }
         if ($dataType == 'set') {
             $values = $this->_columns[$fieldName]['values'];
             // Convert string to array
             if (is_string($value)) {
                 $value = explode(',', $value);
                 foreach ($value as &$v) {
                     $v = trim($v);
                 }
                 $record->set($fieldName, $value);
             }
             // Make sure each set value is valid
             foreach ($value as $k => $v) {
                 if (!in_array($v, $values)) {
                     $errorStack->add($fieldName, 'set');
                 }
             }
         }
     }
     // Validate field length, if length validation is enabled
     if ($this->getAttribute(Doctrine_Core::ATTR_VALIDATE) & Doctrine_Core::VALIDATE_LENGTHS) {
         if (!Doctrine_Validator::validateLength($value, $dataType, $this->getFieldLength($fieldName))) {
             $errorStack->add($fieldName, 'length');
         }
     }
     // Run all custom validators
     foreach ($this->getFieldValidators($fieldName) as $validatorName => $args) {
         if (!is_string($validatorName)) {
             $validatorName = $args;
             $args = array();
         }
         $validator = Doctrine_Validator::getValidator($validatorName);
         $validator->invoker = $record;
         $validator->field = $fieldName;
         $validator->args = $args;
         if (!$validator->validate($value)) {
             $errorStack->add($fieldName, $validator);
         }
     }
     return $errorStack;
 }
開發者ID:sabaki-dev,項目名稱:doctrine1,代碼行數:90,代碼來源:Table.php


注:本文中的Doctrine_Validator::isValidType方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。