From 6b05c3ad7a1bfefe6a4a8ae142fbfbeeaa808c77 Mon Sep 17 00:00:00 2001 From: Haries Nur Ikhwan Date: Fri, 1 May 2020 14:58:35 +0800 Subject: [PATCH] Format mobile number to E164 format. --- app/Group.php | 6 +++ app/ShortenedUrl.php | 6 +++ ...6_format mobile numbers to E164 format.php | 54 +++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 database/migrations/2020_05_01_142326_format mobile numbers to E164 format.php diff --git a/app/Group.php b/app/Group.php index 3c49790..53108a0 100644 --- a/app/Group.php +++ b/app/Group.php @@ -3,12 +3,18 @@ namespace App; use Illuminate\Database\Eloquent\Model; +use libphonenumber\PhoneNumberFormat; class Group extends Model { protected $guarded = []; + public function setMobileNumberAttribute($value) + { + $this->attributes['mobile_number'] = phone($value, 'MY', PhoneNumberFormat::E164); + } + public function urls() { return $this->belongsTo(ShortenedUrl::class, 'shortened_urls_id'); diff --git a/app/ShortenedUrl.php b/app/ShortenedUrl.php index 5b9edf3..d4bb255 100644 --- a/app/ShortenedUrl.php +++ b/app/ShortenedUrl.php @@ -4,6 +4,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; +use libphonenumber\PhoneNumberFormat; use Vinkla\Hashids\Facades\Hashids; class ShortenedUrl extends Model @@ -26,6 +27,11 @@ public function getHashidAttribute($value) return Hashids::encode($this->id); } + public function setMobileNumberAttribute($value) + { + $this->attributes['mobile_number'] = phone($value, 'MY', PhoneNumberFormat::E164); + } + public function group() { return $this->hasMany(Group::class, 'shortened_urls_id'); diff --git a/database/migrations/2020_05_01_142326_format mobile numbers to E164 format.php b/database/migrations/2020_05_01_142326_format mobile numbers to E164 format.php new file mode 100644 index 0000000..473b25c --- /dev/null +++ b/database/migrations/2020_05_01_142326_format mobile numbers to E164 format.php @@ -0,0 +1,54 @@ +with('group') + ->withTrashed() + ->chunk(1000, function ($urls) { + $urls->each(function ($url) { + + if ($url->type === 'single') { + $url->timestamps = false; + + $url->update([ + 'mobile_number' => $url->mobile_number, + ]); + + } else { + $url->group->each(function ($item) { + $item->timestamps = false; + + $item->update([ + 'mobile_number' => $item->mobile_number, + ]); + }); + } + + }); + }); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + } +}