Skip to content

Commit

Permalink
Format mobile number to E164 format.
Browse files Browse the repository at this point in the history
  • Loading branch information
hariesnurikhwan committed May 1, 2020
1 parent a778afb commit 6b05c3a
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
6 changes: 6 additions & 0 deletions app/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
6 changes: 6 additions & 0 deletions app/ShortenedUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

use App\ShortenedUrl;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

class FormatMobileNumbersToE164Format extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
DB::transaction(function () {
ShortenedUrl::query()
->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()
{
//
}
}

0 comments on commit 6b05c3a

Please sign in to comment.