/**
  * Given an array of sanitizer util types, a value, as well as several other parameters, run through each
  * sanitizer type on the value and process any sanitization messages or errors into the ImportSanitizeResultsUtil
  * provided.
  * @param array $sanitizerUtilTypes
  * @param string $modelClassName
  * @param string $attributeName
  * @param mixed $value
  * @param array $columnMappingData
  * @param ImportSanitizeResultsUtil $importSanitizeResultsUtil
  * @return mixed value
  */
 public static function sanitizeValueBySanitizerTypes($sanitizerUtilTypes, $modelClassName, $attributeName, $value, $columnName, $columnMappingData, ImportSanitizeResultsUtil $importSanitizeResultsUtil)
 {
     assert('is_array($sanitizerUtilTypes)');
     assert('is_string($modelClassName)');
     assert('is_string($attributeName) || $attributeName == null');
     assert('is_string($columnName)');
     assert('is_array($columnMappingData)');
     foreach ($sanitizerUtilTypes as $sanitizerUtilType) {
         try {
             $sanitizer = ImportSanitizerUtilFactory::make($sanitizerUtilType, $modelClassName, $attributeName, $columnName, $columnMappingData);
             if ($sanitizer->shouldSanitizeValue()) {
                 $value = $sanitizer->sanitizeValue($value);
             }
         } catch (InvalidValueToSanitizeException $e) {
             if ($e->getMessage() != null) {
                 if ($attributeName != null) {
                     $label = LabelUtil::makeModelAndAttributeNameCombinationLabel($modelClassName, $attributeName);
                 } else {
                     $label = $modelClassName::getModelLabelByTypeAndLanguage('Singular') . ' -';
                 }
                 $importSanitizeResultsUtil->addMessage($label . ' ' . $e->getMessage());
             }
             $value = null;
             if ($sanitizer::shouldNotSaveModelOnSanitizingValueFailure()) {
                 $importSanitizeResultsUtil->setModelShouldNotBeSaved();
             }
             break;
         }
     }
     return $value;
 }
 /**
  * Make an array of message strings by RedBeanModel errors
  * @param array $errors RedBeanModel errors
  */
 public static function makeMessagesByModel(RedBeanModel $model)
 {
     $messages = array();
     foreach ($model->getErrors() as $attributeName => $errors) {
         foreach ($errors as $relationAttributeName => $errorOrRelatedError) {
             if (is_array($errorOrRelatedError)) {
                 $relationModelClassName = $model->getRelationModelClassName($attributeName);
                 foreach ($errorOrRelatedError as $relatedError) {
                     if ($relatedError != '') {
                         $messages[] = LabelUtil::makeModelAndAttributeNameCombinationLabel(get_class($model), $attributeName) . ' - ' . $relatedError;
                     }
                 }
             } elseif ($errorOrRelatedError != '') {
                 $messages[] = LabelUtil::makeModelAndAttributeNameCombinationLabel(get_class($model), $attributeName) . ' - ' . $errorOrRelatedError;
             }
         }
     }
     return $messages;
 }
 /**
  * Given an array of sanitizer util types, a value, as well as several other parameters, run through each
  * sanitizer type on the value and process any sanitization messages or errors into the ImportSanitizeResultsUtil
  * provided.
  * @param array $sanitizerUtilTypes
  * @param string $modelClassName
  * @param string $attributeName
  * @param mixed $value
  * @param array $columnMappingData
  * @param ImportSanitizeResultsUtil $importSanitizeResultsUtil
  */
 public static function sanitizeValueBySanitizerTypes($sanitizerUtilTypes, $modelClassName, $attributeName, $value, $columnMappingData, ImportSanitizeResultsUtil $importSanitizeResultsUtil)
 {
     assert('is_array($sanitizerUtilTypes)');
     assert('is_string($modelClassName)');
     assert('is_string($attributeName) || $attributeName == null');
     assert('is_array($columnMappingData)');
     foreach ($sanitizerUtilTypes as $sanitizerUtilType) {
         $sanitizerUtilClassName = $sanitizerUtilType . 'SanitizerUtil';
         //For extra columns, only process sanitization for 'required' since that will add the default values.
         //Other sanitization is not required since extra columns are not fed from external data.
         if ($columnMappingData["type"] == 'extraColumn' && !is_subclass_of($sanitizerUtilClassName, 'RequiredSanitizerUtil') && $sanitizerUtilClassName != 'RequiredSanitizerUtil') {
             continue;
         }
         $mappingRuleType = $sanitizerUtilClassName::getLinkedMappingRuleType();
         if ($mappingRuleType != null) {
             assert('$mappingRuleType != null');
             $mappingRuleFormClassName = $mappingRuleType . 'MappingRuleForm';
             if (!isset($columnMappingData['mappingRulesData'][$mappingRuleFormClassName])) {
                 assert('$columnMappingData["type"] = "extraColumn"');
                 $mappingRuleData = null;
             } else {
                 $mappingRuleData = $columnMappingData['mappingRulesData'][$mappingRuleFormClassName];
             }
         } else {
             $mappingRuleData = null;
         }
         try {
             if ($sanitizerUtilClassName::supportsSanitizingWithInstructions()) {
                 if (!empty($columnMappingData['importInstructionsData'])) {
                     assert('isset($columnMappingData["importInstructionsData"])');
                     $importInstructionsData = $columnMappingData['importInstructionsData'];
                 } else {
                     $importInstructionsData = null;
                 }
                 $value = $sanitizerUtilClassName::sanitizeValueWithInstructions($modelClassName, $attributeName, $value, $mappingRuleData, $importInstructionsData);
             } else {
                 $value = $sanitizerUtilClassName::sanitizeValue($modelClassName, $attributeName, $value, $mappingRuleData);
             }
         } catch (InvalidValueToSanitizeException $e) {
             if ($e->getMessage() != null) {
                 if ($attributeName != null) {
                     $label = LabelUtil::makeModelAndAttributeNameCombinationLabel($modelClassName, $attributeName);
                 } else {
                     $label = $modelClassName::getModelLabelByTypeAndLanguage('Singular') . ' -';
                 }
                 $importSanitizeResultsUtil->addMessage($label . ' ' . $e->getMessage());
             }
             $value = null;
             if ($sanitizerUtilClassName::shouldNotSaveModelOnSanitizingValueFailure()) {
                 $importSanitizeResultsUtil->setModelShouldNotBeSaved();
             }
             break;
         }
     }
     return $value;
 }
Beispiel #4
0
 public function testMakeModelAndAttributeNameCombinationLabel()
 {
     $label = LabelUtil::makeModelAndAttributeNameCombinationLabel('A', 'a');
     $this->assertEquals('A - A', $label);
 }