generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from mailcarrierapp/feat/multiple-cc-bcc
Allow multiple CCs and BCCs
- Loading branch information
Showing
14 changed files
with
411 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Support\Facades\DB; | ||
use MailCarrier\Models\Log; | ||
|
||
return new class extends Migration { | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
$tableName = (new Log())->getTable(); | ||
|
||
DB::table($tableName) | ||
->whereNotNull('cc') | ||
->orWhereNotNull('bcc') | ||
->orderBy('id') | ||
->each(function (object $log) use ($tableName) { | ||
$changes = []; | ||
|
||
if (!is_null($log->cc)) { | ||
$changes['cc'] = [json_decode($log->cc, true)]; | ||
} | ||
|
||
if (!is_null($log->bcc)) { | ||
$changes['bcc'] = [json_decode($log->bcc, true)]; | ||
} | ||
|
||
if (!empty($changes)) { | ||
DB::table($tableName) | ||
->where('id', $log->id) | ||
->update($changes); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
// | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace MailCarrier\Dto\Casters; | ||
|
||
use Illuminate\Support\Collection; | ||
use MailCarrier\Dto\ContactDto; | ||
use Spatie\DataTransferObject\Caster; | ||
|
||
class ContactArrayCaster implements Caster | ||
{ | ||
public function cast(mixed $value): ?array | ||
{ | ||
if (is_null($value)) { | ||
return null; | ||
} | ||
|
||
if ($value instanceof ContactDto) { | ||
return [$value]; | ||
} | ||
|
||
if (!is_array($value)) { | ||
$value = [ | ||
'email' => $value, | ||
]; | ||
} | ||
|
||
if (Collection::make($value)->every(fn (mixed $value) => $value instanceof ContactDto)) { | ||
return $value; | ||
} | ||
|
||
if (is_array($value) && array_is_list($value) && is_string($value[0])) { | ||
$value = array_map( | ||
fn (string $item) => [ | ||
'email' => $item, | ||
], | ||
$value | ||
); | ||
} | ||
|
||
if (!array_is_list($value)) { | ||
$value = [$value]; | ||
} | ||
|
||
return array_map( | ||
fn (array $item) => new ContactDto($item), | ||
$value | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace MailCarrier\Models\Casts; | ||
|
||
use Illuminate\Contracts\Database\Eloquent\CastsAttributes; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Support\Collection; | ||
use MailCarrier\Dto\ContactDto; | ||
|
||
class CollectionOfContacts implements CastsAttributes | ||
{ | ||
/** | ||
* Cast the given value. | ||
* | ||
* @param array<string, mixed> $attributes | ||
* @return \Illuminate\Support\Collection<\MailCarrier\Dto\ContactDto>|null | ||
*/ | ||
public function get(Model $model, string $key, mixed $value, array $attributes): ?Collection | ||
{ | ||
if (is_null($value)) { | ||
return null; | ||
} | ||
|
||
return Collection::make(json_decode($value, true)) | ||
->map(fn (array $value) => new ContactDto($value)); | ||
} | ||
|
||
/** | ||
* Prepare the given value for storage. | ||
* | ||
* @param array<string, mixed> $attributes | ||
*/ | ||
public function set(Model $model, string $key, mixed $value, array $attributes): string | ||
{ | ||
return json_encode($value); | ||
} | ||
} |
Oops, something went wrong.