/*
* #%L
* BroadleafCommerce Profile Web
* %%
* Copyright (C) 2009 - 2013 Broadleaf Commerce
* %%
* Licensed 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.
* #L%
*/
package org.broadleafcommerce.profile.web.core.util;
import org.apache.commons.lang.StringUtils;
import org.broadleafcommerce.profile.core.domain.Phone;
import org.springframework.stereotype.Service;
@Service("blPhoneFormatter")
public class PhoneFormatterImpl implements PhoneFormatter {
public void formatPhoneNumber(Phone phone) {
if(phone != null && !StringUtils.isEmpty(phone.getPhoneNumber())){
phone.setPhoneNumber(formatTelephoneNumber(phone.getPhoneNumber()));
}
}
private String formatTelephoneNumber(String pNumber) {
if (pNumber == null) {
return null;
}
String number = pNumber.replaceAll("\\D", "");
if (number.length() == 0) {
return null;
}
if (number.length() > 10) {
number = number.substring(0, 10);
}
StringBuffer newNumber = new StringBuffer(number);
if (newNumber.length() == 10) {
newNumber.insert(6, "-");
newNumber.insert(3, "-");
}
return newNumber.toString();
}
}