Skip to content

Commit

Permalink
Add DefaultNormalizerTypeEnum
Browse files Browse the repository at this point in the history
  • Loading branch information
ioigoume committed Jun 19, 2022
1 parent a169805 commit 668b0dc
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 21 deletions.
10 changes: 5 additions & 5 deletions app/Model/Behavior/NormalizationBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
4 changes: 2 additions & 2 deletions app/Model/CoPipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')));
}

Expand Down Expand Up @@ -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',
Expand Down
Empty file.
41 changes: 41 additions & 0 deletions app/Plugin/DefaultNormalizer/Lib/enum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* COmanage Registry Default Normalizer Enumerations
*
* Portions licensed to the University Corporation for Advanced Internet
* Development, Inc. ("UCAID") under one or more contributor license agreements.
* See the NOTICE file distributed with this work for additional information
* regarding copyright ownership.
*
* UCAID licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @link http://www.internet2.edu/comanage COmanage Project
* @package registry-plugin DefaultNormalizer
* @since COmanage Registry v4.0.1
* @license Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
*/


class DefaultNormalizerTypeEnum
{
const TrimWhitespace = 'TW';
const MixCase = 'MC';
const PunctuationToSpace = 'PS';

// Each type maps to a function having the first letter lowercase
public static $type = array(
DefaultNormalizerTypeEnum::TrimWhitespace => 'TrimWhitespace',
DefaultNormalizerTypeEnum::MixCase => 'MixCase',
DefaultNormalizerTypeEnum::PunctuationToSpace => 'PunctuationToSpace'
);
}
29 changes: 15 additions & 14 deletions app/Plugin/DefaultNormalizer/Model/DefaultNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
)
);

Expand Down Expand Up @@ -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;
}
}
}
Expand All @@ -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);
}
}
}
Expand Down

0 comments on commit 668b0dc

Please sign in to comment.