예제 #1
0
 /**
  * Validates the given data value using the given data type.
  *
  * @param DataValue $dataValue
  * @param string    $dataTypeId
  *
  * @return Result
  */
 public function validateDataValue(DataValue $dataValue, $dataTypeId)
 {
     $dataValueType = $this->dataTypeFactory->getType($dataTypeId)->getDataValueType();
     if ($dataValue instanceof UnDeserializableValue) {
         $result = Result::newError(array(Error::newError('Bad snak value: ' . $dataValue->getReason(), null, 'bad-value', array($dataValue->getReason()))));
     } elseif ($dataValueType != $dataValue->getType()) {
         $result = Result::newError(array(Error::newError('Bad value type: ' . $dataValue->getType() . ', expected ' . $dataValueType, null, 'bad-value-type', array($dataValue->getType(), $dataValueType))));
     } else {
         $result = Result::newSuccess();
     }
     //XXX: DataTypeValidatorFactory should expose only one validator, which would be a CompositeValidator
     foreach ($this->validatorFactory->getValidators($dataTypeId) as $validator) {
         $subResult = $validator->validate($dataValue);
         //XXX: Some validators should be fatal and cause us to abort the loop.
         //     Others shouldn't.
         if (!$subResult->isValid()) {
             //TODO: Don't bail out immediately. Accumulate errors from all validators.
             //      We need Result::merge() for this.
             return $subResult;
         }
     }
     return $result;
 }