diff --git a/CHANGELOG.md b/CHANGELOG.md index d688ce0..9957158 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,62 @@ All notable changes to `utilities` will be documented in this file. +## v1.0.11 - 2024-07-02 + +### What's Changed + +* Add disk option on spatie downloader by @mariusdatakrag in https://github.com/PropaySystems/utilities/pull/6 + +### New Contributors + +* @mariusdatakrag made their first contribution in https://github.com/PropaySystems/utilities/pull/6 + +**Full Changelog**: https://github.com/PropaySystems/utilities/compare/1.0.10...v1.0.11 + +## v1.0.10 - 2024-01-16 + +**Full Changelog**: https://github.com/PropaySystems/utilities/compare/1.0.9...1.0.10 + +## 1.0.9 - 2023-12-11 + +### What's Changed + +* Gender code configurable by @ettiennelouw in https://github.com/PropaySystems/utilities/pull/2 + +### New Contributors + +* @ettiennelouw made their first contribution in https://github.com/PropaySystems/utilities/pull/2 + +**Full Changelog**: https://github.com/PropaySystems/utilities/compare/v1.0.8...1.0.9 + +## v1.0.8 - 2023-11-27 + +Added proper FK to DropDownSchema + +## v1.0.7 - 2023-11-27 + +Added SaveRelation Trait helper + +## v1.0.6 - 2023-11-27 + +Fixed DropDownSchema added default value for status and fixed space in translaltion helper + +## v1.0.5 - 2023-11-23 + +Added function to number helper to combine country prefix and cell number if number starts with a 0 + +## v1.0.4 - 2023-11-20 + +### What's Changed + +- Simplified unit tests + +### New Contributors + +@MickProPay made their first contribution in https://github.com/PropaySystems/utilities/pull/1 + +**Full Changelog**: https://github.com/PropaySystems/utilities/compare/v1.0.3...v1.0.4 + ## v1.0.3 - 2023-11-19 Support multiple laravel versions diff --git a/README.md b/README.md index 42a67a9..b0fed99 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,10 @@ This will format the number accordingly 100000 will become 100.00k ```php NumberHelper::numberFormat(int $number); ``` +This will combine a country prefix ex: 27 with the cell number ex: 0821231234 and return 27821231234 +```php +NumberHelper::combineCellPrefix($prefix, $number) +``` ### -- Route Helper -- Check of the string is in the current route name diff --git a/config/utilities.php b/config/utilities.php index 7e021dd..0d7ce10 100644 --- a/config/utilities.php +++ b/config/utilities.php @@ -3,10 +3,26 @@ // config for PropaySystems/Utilities return [ + /* + |-------------------------------------------------------------------------- + | Translation Helper + |-------------------------------------------------------------------------- + | + | Set the default path for the translation files + | + */ 'translation_helper' => [ 'path' => env('TRANSLATION_HELPER_PATH', 'db'), ], + /* + |-------------------------------------------------------------------------- + | Save To Upper + |-------------------------------------------------------------------------- + | + | Exclude fields when converting to uppercase + | + */ 'save_to_upper' => [ 'exclusions' => [ 'email', @@ -19,8 +35,30 @@ ], ], + /* + |-------------------------------------------------------------------------- + | Trigger + |-------------------------------------------------------------------------- + | + | Enable or disable database triggers + | + */ 'trigger' => [ 'enable_trigger' => env('ENABLE_TRIGGER', false), ], + /* + |-------------------------------------------------------------------------- + | Gender Code + |-------------------------------------------------------------------------- + | + | This gender code is used mostly in the id number helper. + | + */ + 'gender' => [ + 'male' => 1, + 'female' => 2, + 'unknown' => 3, + ], + ]; diff --git a/src/Helpers/IdNumberHelper.php b/src/Helpers/IdNumberHelper.php index e493a4d..cc7b0be 100644 --- a/src/Helpers/IdNumberHelper.php +++ b/src/Helpers/IdNumberHelper.php @@ -8,8 +8,8 @@ class IdNumberHelper { /** - * @param $dateOfBirth 19821208 - * @param int $male 0 or 1 + * @param $dateOfBirth 19821208 + * @param int $male 0 or 1 * * @throws \Exception */ @@ -46,7 +46,7 @@ public static function generateFakeId(): string $maxYear = 99; $year = rand($minYear, $maxYear); - $now = new Carbon(); + $now = new Carbon; $minGender = 0000; $maxGender = 9999; @@ -86,7 +86,7 @@ public static function generateFakeId(): string $totalMod = ($total % 10); $lastDigit = 10 - $totalMod; - return Str::limit($tempId.$lastDigit, 13); + return Str::limit($tempId.$lastDigit, 13, ''); } public static function generateDateOfBirth(): string @@ -99,9 +99,9 @@ public static function generateDateOfBirth(): string ); } - public static function getGenderCode($idNumber): string + public static function getGenderCode($idNumber): int { - return (substr($idNumber, 6, 4) < 5000) ? 'female' : 'male'; + return (substr($idNumber, 6, 4) < 5000) ? config('utilities.gender.female') : config('utilities.gender.male'); } public static function getBirthDate($idNumber): string @@ -111,8 +111,6 @@ public static function getBirthDate($idNumber): string $birthYear = substr($idNumber, 0, 2); if ($birthYear < date('y')) { $default_century = '20'; - } else { - $default_century = '19'; } $year = $default_century.substr($idNumber, 0, 2); @@ -217,4 +215,12 @@ private static function generateLuhnDigit($input): int return ($total * 9) % 10; } + + public static function getBirthdayFromIdNumber($idNumber): int + { + $birthday = self::getBirthDate($idNumber); + $currentDate = Carbon::now(); + + return $currentDate->diffInYears($birthday); + } } diff --git a/src/Helpers/NumberHelper.php b/src/Helpers/NumberHelper.php index c35919e..978ba1d 100644 --- a/src/Helpers/NumberHelper.php +++ b/src/Helpers/NumberHelper.php @@ -7,13 +7,9 @@ class NumberHelper /** * @throws \Exception */ - public static function randomInt(int $min = 1, int $max = 100000, bool $appendTime = false): int + public static function randomInt(int $min = 1, int $max = 100000): int { - $number = random_int($min, $max).time(); - - if ($appendTime) { - $number.time(); - } + $number = random_int($min, $max); return (int) $number; } @@ -65,4 +61,18 @@ public static function numberFormat(int $number, int $precision = 2, $divisors = return number_format($number / $divisor, $precision).$shorthand; } + + /** + * @return false|string + */ + public static function combineCellPrefix($prefix, $number): bool|string + { + $firstChar = mb_substr($number, 0, 1, 'utf-8'); + + if ($firstChar == 0) { + return $prefix.substr($number, 1); + } else { + return false; + } + } } diff --git a/src/Helpers/SpatieMediaHelper.php b/src/Helpers/SpatieMediaHelper.php index 5e23ed3..5f92301 100644 --- a/src/Helpers/SpatieMediaHelper.php +++ b/src/Helpers/SpatieMediaHelper.php @@ -2,6 +2,7 @@ namespace PropaySystems\Utilities\Helpers; +use Illuminate\Support\Facades\Storage; use Spatie\MediaLibrary\MediaCollections\Models\Media; use Symfony\Component\HttpFoundation\StreamedResponse; @@ -35,10 +36,13 @@ public static function createFromString($model, string $string, string $name, st ->toMediaCollection($collection, $disk); } - public static function download(Media $media, bool $asFilename = false): StreamedResponse|Media + public static function download(Media $media, bool $asFilename = false, $disk = null): StreamedResponse|Media { - return $media; - //return Storage::download($media->getPath(), ($asFilename) ? $media->file_name : $media->name); + if (! is_null($disk)) { + return Storage::disk($disk)->download($media->getPath(), ($asFilename) ? $media->name : $media->file_name); + } + + return Storage::download($media->getPath(), ($asFilename) ? $media->name : $media->file_name); } public static function delete(Media $media): ?bool diff --git a/src/Helpers/TranslationHelper.php b/src/Helpers/TranslationHelper.php index f067a89..c2adf7a 100644 --- a/src/Helpers/TranslationHelper.php +++ b/src/Helpers/TranslationHelper.php @@ -19,6 +19,6 @@ class TranslationHelper */ public static function translate(string $value, string $fileName, string $character = '_') { - return Lang::has(config('utilities.translation_helper.path').DIRECTORY_SEPARATOR.$fileName.' . '.Str::slug($value, $character)) ? __(config('utilities.translation_helper.path').DIRECTORY_SEPARATOR.$fileName.'.'.Str::slug($value, $character)) : $value; + return Lang::has(config('utilities.translation_helper.path').DIRECTORY_SEPARATOR.$fileName.'.'.Str::slug($value, $character)) ? __(config('utilities.translation_helper.path').DIRECTORY_SEPARATOR.$fileName.'.'.Str::slug($value, $character)) : $value; } } diff --git a/src/Traits/DropdownSchema.php b/src/Traits/DropdownSchema.php index be55c86..3d3a270 100644 --- a/src/Traits/DropdownSchema.php +++ b/src/Traits/DropdownSchema.php @@ -17,11 +17,9 @@ public function up() Schema::create($this->table, function (Blueprint $table) { $table->id(); $table->string('name')->nullable(false)->index()->unique(); - $table->unsignedInteger('status_id')->nullable(false); + $table->foreignId('status_id')->default($this->status_default ?? 1)->nullable(false)->constrained(); $table->timestamps(); $table->softDeletes(); - - $table->foreign('status_id')->references('id')->on('statuses'); }); } diff --git a/src/Traits/HasCompositePrimaryKey.php b/src/Traits/HasCompositePrimaryKey.php index b891a01..738aec2 100644 --- a/src/Traits/HasCompositePrimaryKey.php +++ b/src/Traits/HasCompositePrimaryKey.php @@ -13,7 +13,7 @@ public function getIncrementing() public static function find($ids, $columns = ['*']) { - $me = new self(); + $me = new self; $query = $me->newQuery(); foreach ($me->getKeyName() as $key) { diff --git a/src/Traits/PasswordStrength.php b/src/Traits/PasswordStrength.php index d6e21e5..3221c7c 100644 --- a/src/Traits/PasswordStrength.php +++ b/src/Traits/PasswordStrength.php @@ -10,7 +10,7 @@ trait PasswordStrength public function updateProgress(): void { - $zxcvbn = new Zxcvbn(); + $zxcvbn = new Zxcvbn; $score = $zxcvbn->passwordStrength($this->password ?? ''); $this->passwordStrength = (($score['score'] / 4) * 100); } diff --git a/src/Traits/SaveRelations.php b/src/Traits/SaveRelations.php new file mode 100644 index 0000000..af54238 --- /dev/null +++ b/src/Traits/SaveRelations.php @@ -0,0 +1,71 @@ + $value) { + $method = str_replace('_id', '', $relation); + if (method_exists($model, $method)) { + if (empty($value)) { + $value = null; + } + + $model->$method()->associate($value)->save(); + } + } + } + } + + /** + * Save Many To Many relations using SYNC + * + * @param instance $model + * @param array $relations + * @return null + */ + public function sync($model, $relations = []) + { + //check if not empty + if (! empty($relations)) { + foreach ($relations as $relation => $value) { + if (! empty($value)) { + $method = str_replace('_id', '', $relation); + if (method_exists($model, $method)) { + if (empty($value)) { + $value = []; + } + + $model->$method()->sync($value); + } + } + } + } + } + + /** + * Save MorphTo relations + * + * @param instance $model + * @param array $relations + * @return null + */ + public function SaveMorphTo($model, $morphable, $morph) + { + $model->$morphable()->save($morph); + } +} diff --git a/src/Traits/TriggerHelper.php b/src/Traits/TriggerHelper.php index c35d810..c61dc48 100644 --- a/src/Traits/TriggerHelper.php +++ b/src/Traits/TriggerHelper.php @@ -7,7 +7,7 @@ trait TriggerHelper { /** - * @param $state + * @param $state */ public static function switchDatabaseTrigger($enable = true, $table = null, $trigger = null, string $connection = 'sqlsrv'): void { diff --git a/src/Utilities.php b/src/Utilities.php index da223e1..0695954 100755 --- a/src/Utilities.php +++ b/src/Utilities.php @@ -2,6 +2,4 @@ namespace PropaySystems\Utilities; -class Utilities -{ -} +class Utilities {} diff --git a/tests/Unit/NumberHelperTest.php b/tests/Unit/NumberHelperTest.php index 4f0ffce..477e7d2 100644 --- a/tests/Unit/NumberHelperTest.php +++ b/tests/Unit/NumberHelperTest.php @@ -32,3 +32,9 @@ expect($value)->toBe('100K'); }); + +it('can add cell number prefix to a number', function () { + $value = NumberHelper::combineCellPrefix('27', '0821231234'); + + expect($value)->toBe('27821231234'); +});