From 668b0dc343c43db4ca0a3b05745ec3ea5f0892e8 Mon Sep 17 00:00:00 2001 From: Ioannis Igoumenos Date: Sun, 14 Nov 2021 08:39:46 +0200 Subject: [PATCH] Add DefaultNormalizerTypeEnum --- app/Model/Behavior/NormalizationBehavior.php | 10 ++--- app/Model/CoPipeline.php | 4 +- app/Plugin/DefaultNormalizer/Lib/empty | 0 app/Plugin/DefaultNormalizer/Lib/enum.php | 41 +++++++++++++++++++ .../Model/DefaultNormalizer.php | 29 ++++++------- 5 files changed, 63 insertions(+), 21 deletions(-) delete mode 100644 app/Plugin/DefaultNormalizer/Lib/empty create mode 100644 app/Plugin/DefaultNormalizer/Lib/enum.php diff --git a/app/Model/Behavior/NormalizationBehavior.php b/app/Model/Behavior/NormalizationBehavior.php index fdd91b520..501b1278d 100644 --- a/app/Model/Behavior/NormalizationBehavior.php +++ b/app/Model/Behavior/NormalizationBehavior.php @@ -58,12 +58,12 @@ public function normalize(Model $model, $data, $coId = false, $options = array() // Are there any types of normalization that we need to skip $normalization_dis = array(); - if(isset($options['mixCase']) && !$options['mixCase']) { - $normalization_dis[] = 'mixCase'; - } - if(isset($options['trimWhitespace']) && !$options['trimWhitespace']) { - $normalization_dis[] = 'trimWhitespace'; + foreach (DefaultNormalizerTypeEnum::$type as $key => $value) { + if(isset($options[$key]) && !$options[$key]) { + $normalization_dis[] = $key; + } } + // If $coId is false, look for a CO ID. If we don't find one or if $coId is null, // we're dealing with org identity data, which normalizations don't currently support. diff --git a/app/Model/CoPipeline.php b/app/Model/CoPipeline.php index 703289b29..33225541b 100644 --- a/app/Model/CoPipeline.php +++ b/app/Model/CoPipeline.php @@ -1000,7 +1000,7 @@ protected function syncOrgIdentityToCoPerson($coPipeline, if(!$this->Co->CoPerson->CoPersonRole->save($newCoPersonRole, array("provision" => false, "safeties" => $safeties, - "mixCase" => false))) { + DefaultNormalizerTypeEnum::MixCase => false))) { throw new RuntimeException(_txt('er.db.save-a', array('CoPersonRole'))); } @@ -1229,7 +1229,7 @@ protected function syncOrgIdentityToCoPerson($coPipeline, if(!$model->save($nr, array("provision" => false, "safeties" => $safeties, "skipAvailability" => true, - "mixCase" => !in_array($model, array('CoPersonRole', 'EnrolleeCoPersonRole')), + DefaultNormalizerTypeEnum::MixCase => !in_array($model, array('CoPersonRole', 'EnrolleeCoPersonRole')), "trustVerified" => $trustVerified))) { throw new RuntimeException(_txt('er.db.save-a', diff --git a/app/Plugin/DefaultNormalizer/Lib/empty b/app/Plugin/DefaultNormalizer/Lib/empty deleted file mode 100644 index e69de29bb..000000000 diff --git a/app/Plugin/DefaultNormalizer/Lib/enum.php b/app/Plugin/DefaultNormalizer/Lib/enum.php new file mode 100644 index 000000000..544dd5d53 --- /dev/null +++ b/app/Plugin/DefaultNormalizer/Lib/enum.php @@ -0,0 +1,41 @@ + 'TrimWhitespace', + DefaultNormalizerTypeEnum::MixCase => 'MixCase', + DefaultNormalizerTypeEnum::PunctuationToSpace => 'PunctuationToSpace' + ); +} \ No newline at end of file diff --git a/app/Plugin/DefaultNormalizer/Model/DefaultNormalizer.php b/app/Plugin/DefaultNormalizer/Model/DefaultNormalizer.php index 519a98508..8f4140ead 100644 --- a/app/Plugin/DefaultNormalizer/Model/DefaultNormalizer.php +++ b/app/Plugin/DefaultNormalizer/Model/DefaultNormalizer.php @@ -61,43 +61,43 @@ public function normalize($data, $normalization_dis = array()) { $normalizations = array( 'Address' => array( - 'mixCase' => array('street', 'locality', 'state', 'country'), - 'trimWhitespace' => array('street', 'locality', 'state', 'postal_code', 'country') + DefaultNormalizerTypeEnum::MixCase => array('street', 'locality', 'state', 'country'), + DefaultNormalizerTypeEnum::TrimWhitespace => array('street', 'locality', 'state', 'postal_code', 'country') ), 'CoPersonRole' => array( - 'mixCase' => array('title', 'o', 'ou'), - 'trimWhitespace' => array('title', 'o', 'ou') + DefaultNormalizerTypeEnum::MixCase => array('title', 'o', 'ou'), + DefaultNormalizerTypeEnum::TrimWhitespace => array('title', 'o', 'ou') ), // We get passed the alias, not the model name during enrollment. // There's not an obvious generic way to figure the out, but for now this // only happens here, so we simply duplicate the rules. (CO-1550) 'EnrolleeCoPersonRole' => array( - 'mixCase' => array('title', 'o', 'ou'), - 'trimWhitespace' => array('title', 'o', 'ou') + DefaultNormalizerTypeEnum::MixCase => array('title', 'o', 'ou'), + DefaultNormalizerTypeEnum::TrimWhitespace => array('title', 'o', 'ou') ), 'EmailAddress' => array( // Note cake validation will likely prevent this from being called - 'trimWhitespace' => array('mail') + DefaultNormalizerTypeEnum::TrimWhitespace => array('mail') ), 'Identifier' => array( - 'trimWhiteSpace' => array('identifier') + DefaultNormalizerTypeEnum::TrimWhitespace => array('identifier') ), 'Name' => array( // For now, we don't mix case to avoid dealing with issues like people who // go by lowercase names, or McPherson-style capitalization - 'trimWhitespace' => array('honorific', 'given', 'middle', 'family', 'suffix') + DefaultNormalizerTypeEnum::TrimWhitespace => array('honorific', 'given', 'middle', 'family', 'suffix') ), 'TelephoneNumber' => array( // Following E.123 format, we only use spaces in telephone numbers // (the + and extension label get added by formatTelephone at rendering time) - 'punctuationToSpace' => array('country_code', 'area_code', 'number', 'extension'), - 'trimWhitespace' => array('country_code', 'area_code', 'number', 'extension') + DefaultNormalizerTypeEnum::PunctuationToSpace => array('country_code', 'area_code', 'number', 'extension'), + DefaultNormalizerTypeEnum::TrimWhitespace => array('country_code', 'area_code', 'number', 'extension') ), 'Url' => array( // We don't normalize an http:// prefix because cake validation will prevent // a URL from being submitted without a prefix (and we wouldn't know the // protocol anyway). - 'trimWhitespace' => array('url') + DefaultNormalizerTypeEnum::TrimWhitespace => array('url') ) ); @@ -135,7 +135,7 @@ public function normalize($data, $normalization_dis = array()) { // We only trim whitespace since we can't say too much about the contents // of the extended attribute. - $normalizations[$model]['trimWhitespace'][] = $name; + $normalizations[$model][DefaultNormalizerTypeEnum::TrimWhitespace][] = $name; } } } @@ -154,7 +154,8 @@ public function normalize($data, $normalization_dis = array()) { } foreach($normalizations[$model][$normalization] as $field) { if(!empty($ret[$model][$field])) { - $ret[$model][$field] = $this->$normalization($ret[$model][$field], $field); + $func = lcfirst( (DefaultNormalizerTypeEnum::$type)[$normalization] ); + $ret[$model][$field] = $this->$func($ret[$model][$field], $field); } } }