# 글로뷰 공용 헬퍼 함수 활용 및 데이터 포맷팅 가이드

글로뷰 플랫폼에서 전화번호 및 사업자등록번호 등 포맷팅이 필요한 데이터를 컨트롤러 단계에서 깔끔하게 정제하여 뷰로 전달하기 위한 공용 헬퍼 함수 활용 명세입니다.

---

## 1. 개요 및 설계 원칙

* 컨트롤러나 뷰마다 중복된 정규식 코드를 작성하는 것을 방지하기 위해 프로젝트 전역 헬퍼 파일에 공용 함수(format_phone, format_business_number)를 등록하여 일관성을 유지합니다.
* 데이터베이스에 하이픈 없이 숫자 형태로 저장된 원본 값을 가져와서, 뷰로 넘기기 전 컨트롤러 단에서 미리 알맞은 형태(010-1234-5678 등)로 변환하여 렌더링 부하를 줄이고 무결성을 확보합니다.

---

## 2. 공용 헬퍼 함수 정의 및 구현 명세

프로젝트 내에 등록된 공용 헬퍼 파일의 소스 코드 구조입니다.

    <?php

    if (!function_exists('format_phone')) {
        function format_phone($number) {
            if (!$number) return '-';
            $clean = preg_replace('/[^0-9]/', '', $number);
            
            // 10자리 (02 서울 및 일반 지역번호 / 대표번호 등)
            if (strlen($clean) === 10) {
                if (substr($clean, 0, 2) === '02') {
                    return preg_replace('/^([0-9]{2})([0-9]{4})([0-9]{4})$/', '$1-$2-$3', $clean);
                }
                return preg_replace('/^([0-9]{3})([0-9]{3})([0-9]{4})$/', '$1-$2-$3', $clean);
            } 
            // 11자리 휴대전화 (010 등)
            elseif (strlen($clean) === 11) {
                return preg_replace('/^([0-9]{3})([0-9]{4})([0-9]{4})$/', '$1-$2-$3', $clean);
            }
            // 전국대표번호 (1588, 1544 등 8자리)
            elseif (strlen($clean) === 8) {
                return preg_replace('/^([0-9]{4})([0-9]{4})$/', '$1-$2', $clean);
            }
            
            return $number;
        }
    }

    if (!function_exists('format_business_number')) {
        function format_business_number($number) {
            if (!$number) return '-';
            $clean = preg_replace('/[^0-9]/', '', $number);
            if (strlen($clean) === 10) {
                return preg_replace('/^([0-9]{3})([0-9]{2})([0-9]{5})$/', '$1-$2-$3', $clean);
            }
            return $number;
        }
    }

---

## 3. 컨트롤러 적용 및 데이터 흐름 파이프라인

뷰티샵 입점 신청 폼(ShopController@create)을 비롯한 주요 진입점에서 위 헬퍼 함수를 호출하여 데이터 포맷을 가공하는 예시입니다.

    public function create()
    {
        $user = Auth::user();
        
        $hasVerifiedPhone = false;
        $formattedPhone = old('phone');

        if ($user && $user->is_phone_verified && $user->phone) {
            $hasVerifiedPhone = true;
            // 헬퍼 함수를 통해 컨트롤러에서 미리 하이픈 포맷 적용
            $formattedPhone = format_phone($user->phone);
        }

        return view('shops.create', compact('hasVerifiedPhone', 'formattedPhone'));
    }

---

## 4. 뷰(Blade) 템플릿 렌더링 연동

컨트롤러에서 정제되어 넘어온 $formattedPhone 변수를 폼 인풋 태그의 value 속성에 그대로 바인딩하고, 인증 상태에 따른 readonly 잠금을 제어합니다.

    <input type="text" 
           class="form-control input-phone @if($hasVerifiedPhone) bg-light @endif" 
           id="phone" 
           name="phone" 
           value="{{ $formattedPhone }}" 
           data-sms-role="phone" 
           placeholder="01012345678" 
           maxlength="13" 
           @if($hasVerifiedPhone) readonly @endif>