From 8f88f30f55b95cd9aa3cd718662c068eea037d14 Mon Sep 17 00:00:00 2001 From: ettiennelouw Date: Mon, 20 Nov 2023 07:55:50 +0000 Subject: [PATCH 01/23] Update CHANGELOG --- CHANGELOG.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d688ce0..1b87ee0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,18 @@ All notable changes to `utilities` will be documented in this file. +## 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 From d45713c861529d14f2baa16c67307eef4611bb4c Mon Sep 17 00:00:00 2001 From: Ettienne Louw Date: Thu, 23 Nov 2023 08:28:58 +0200 Subject: [PATCH 02/23] Added function to combine cell number and country prefix if first char of number is 0 --- src/Helpers/NumberHelper.php | 32 ++++++++++++++++++++++++++++++++ tests/Unit/NumberHelperTest.php | 18 +++++++++++++----- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/src/Helpers/NumberHelper.php b/src/Helpers/NumberHelper.php index c35919e..e630ce1 100644 --- a/src/Helpers/NumberHelper.php +++ b/src/Helpers/NumberHelper.php @@ -5,6 +5,10 @@ class NumberHelper { /** + * @param int $min + * @param int $max + * @param bool $appendTime + * @return int * @throws \Exception */ public static function randomInt(int $min = 1, int $max = 100000, bool $appendTime = false): int @@ -18,6 +22,12 @@ public static function randomInt(int $min = 1, int $max = 100000, bool $appendTi return (int) $number; } + /** + * @param int $last + * @param int $current + * @param bool $round + * @return array + */ public static function getPercentageDifference(int $last, int $current, bool $round = false): array { $diff = $current - $last; @@ -31,6 +41,12 @@ public static function getPercentageDifference(int $last, int $current, bool $ro ]; } + /** + * @param int $number + * @param int $precision + * @param $divisors + * @return int|string + */ public static function numberFormat(int $number, int $precision = 2, $divisors = null): int|string { // Setup default $divisors if not provided @@ -65,4 +81,20 @@ public static function numberFormat(int $number, int $precision = 2, $divisors = return number_format($number / $divisor, $precision).$shorthand; } + + /** + * @param $prefix + * @param $number + * @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/tests/Unit/NumberHelperTest.php b/tests/Unit/NumberHelperTest.php index 3a494c7..0995784 100644 --- a/tests/Unit/NumberHelperTest.php +++ b/tests/Unit/NumberHelperTest.php @@ -1,32 +1,40 @@ toBeInt($value); }); it('can generate percentage difference', function () { - $value = \PropaySystems\Utilities\Helpers\NumberHelper::getPercentageDifference(200, 100); + $value = NumberHelper::getPercentageDifference(200, 100); expect($value['percentage'])->toBe((float) '50.0'); expect($value['direction'])->toBe('+'); }); it('can generate percentage difference rounded', function () { - $value = \PropaySystems\Utilities\Helpers\NumberHelper::getPercentageDifference(200, 100); + $value = NumberHelper::getPercentageDifference(200, 100); expect($value['percentage'])->toBe((float) '50'); }); it('can format a number to more human-readable', function () { - $value = \PropaySystems\Utilities\Helpers\NumberHelper::numberFormat(100000); + $value = NumberHelper::numberFormat(100000); expect($value)->toBe('100.00K'); }); it('can format a number to more human-readable without precision', function () { - $value = \PropaySystems\Utilities\Helpers\NumberHelper::numberFormat(100000, 0); + $value = NumberHelper::numberFormat(100000, 0); expect($value)->toBe('100K'); }); + +it('can add cell number prefix to a number', function () { + $value = NumberHelper::combineCellPrefix('27', '0821231234'); + + expect($value)->toBe('27821231234'); +}); From d76e10354290caeb42c20bcceda220f6806c1cc2 Mon Sep 17 00:00:00 2001 From: ettiennelouw Date: Thu, 23 Nov 2023 06:29:30 +0000 Subject: [PATCH 03/23] Fix styling --- src/Helpers/NumberHelper.php | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/src/Helpers/NumberHelper.php b/src/Helpers/NumberHelper.php index e630ce1..230b910 100644 --- a/src/Helpers/NumberHelper.php +++ b/src/Helpers/NumberHelper.php @@ -5,10 +5,6 @@ class NumberHelper { /** - * @param int $min - * @param int $max - * @param bool $appendTime - * @return int * @throws \Exception */ public static function randomInt(int $min = 1, int $max = 100000, bool $appendTime = false): int @@ -22,12 +18,6 @@ public static function randomInt(int $min = 1, int $max = 100000, bool $appendTi return (int) $number; } - /** - * @param int $last - * @param int $current - * @param bool $round - * @return array - */ public static function getPercentageDifference(int $last, int $current, bool $round = false): array { $diff = $current - $last; @@ -41,12 +31,6 @@ public static function getPercentageDifference(int $last, int $current, bool $ro ]; } - /** - * @param int $number - * @param int $precision - * @param $divisors - * @return int|string - */ public static function numberFormat(int $number, int $precision = 2, $divisors = null): int|string { // Setup default $divisors if not provided @@ -83,8 +67,6 @@ public static function numberFormat(int $number, int $precision = 2, $divisors = } /** - * @param $prefix - * @param $number * @return false|string */ public static function combineCellPrefix($prefix, $number): bool|string @@ -92,7 +74,7 @@ public static function combineCellPrefix($prefix, $number): bool|string $firstChar = mb_substr($number, 0, 1, 'utf-8'); if ($firstChar == 0) { - return $prefix . substr($number, 1); + return $prefix.substr($number, 1); } else { return false; } From 1ef4993b881879e627545a904dab4c7232868373 Mon Sep 17 00:00:00 2001 From: ettiennelouw Date: Thu, 23 Nov 2023 06:30:15 +0000 Subject: [PATCH 04/23] Update CHANGELOG --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b87ee0..1554495 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to `utilities` will be documented in this file. +## 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 From ac51c0d407b687dbe5f4850d7282547b2363683f Mon Sep 17 00:00:00 2001 From: Ettienne Louw Date: Mon, 27 Nov 2023 11:02:09 +0200 Subject: [PATCH 05/23] Fixed DropDownSchema added default value for status and fixed space in translaltion helper --- README.md | 4 ++++ src/Helpers/TranslationHelper.php | 2 +- src/Traits/DropdownSchema.php | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) 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/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..eaf1260 100644 --- a/src/Traits/DropdownSchema.php +++ b/src/Traits/DropdownSchema.php @@ -17,7 +17,7 @@ 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->unsignedInteger('status_id')->default($this->status_default ?? 1)->nullable(false); $table->timestamps(); $table->softDeletes(); From 33896cdad966b08b1283bbbb99d5e5bd87bc9ced Mon Sep 17 00:00:00 2001 From: ettiennelouw Date: Mon, 27 Nov 2023 09:03:11 +0000 Subject: [PATCH 06/23] Update CHANGELOG --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1554495..08b13f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to `utilities` will be documented in this file. +## 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 From d198926387447064abc13472450d040e33ed9c4b Mon Sep 17 00:00:00 2001 From: Ettienne Louw Date: Mon, 27 Nov 2023 18:10:11 +0200 Subject: [PATCH 07/23] Added save relation trait --- src/Traits/SaveRelations.php | 71 ++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/Traits/SaveRelations.php 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); + } +} From bc899cef699f3ada1767632b928e6f4513d346ec Mon Sep 17 00:00:00 2001 From: ettiennelouw Date: Mon, 27 Nov 2023 16:12:01 +0000 Subject: [PATCH 08/23] Update CHANGELOG --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 08b13f5..63e0aa8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to `utilities` will be documented in this file. +## 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 From 42c76bbb67e59889af94598b85e7504a97fa1f2e Mon Sep 17 00:00:00 2001 From: Ettienne Louw Date: Mon, 27 Nov 2023 21:17:29 +0200 Subject: [PATCH 09/23] Made dropdown schema proper foreign key --- src/Traits/DropdownSchema.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Traits/DropdownSchema.php b/src/Traits/DropdownSchema.php index eaf1260..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')->default($this->status_default ?? 1)->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'); }); } From 625f81707b1a923ac4ea5a1b59fe2a6eea2ee6d8 Mon Sep 17 00:00:00 2001 From: ettiennelouw Date: Mon, 27 Nov 2023 19:18:53 +0000 Subject: [PATCH 10/23] Update CHANGELOG --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 63e0aa8..b7076a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to `utilities` will be documented in this file. +## v1.0.8 - 2023-11-27 + +Added proper FK to DropDownSchema + ## v1.0.7 - 2023-11-27 Added SaveRelation Trait helper From 910f7fc8387a172620850c5c866f9e41256e7d93 Mon Sep 17 00:00:00 2001 From: Ettienne Louw Date: Mon, 11 Dec 2023 12:32:01 +0200 Subject: [PATCH 11/23] Added config value for get gender code from id number. --- config/utilities.php | 38 ++++++++++++++++++++++++++++++++++ src/Helpers/IdNumberHelper.php | 30 +++++++++++++++++++++++---- 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/config/utilities.php b/config/utilities.php index 7e021dd..de080eb 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..131dcad 100644 --- a/src/Helpers/IdNumberHelper.php +++ b/src/Helpers/IdNumberHelper.php @@ -40,6 +40,9 @@ public static function generateIdNumber($dateOfBirth, int $male = 0): string return $total; } + /** + * @return string + */ public static function generateFakeId(): string { $minYear = 20; @@ -89,6 +92,9 @@ public static function generateFakeId(): string return Str::limit($tempId.$lastDigit, 13); } + /** + * @return string + */ public static function generateDateOfBirth(): string { return str_pad((string) mt_rand(60, 99), 2, '0', STR_PAD_LEFT).str_pad(mt_rand(1, 12), 2, '0', STR_PAD_LEFT).str_pad( @@ -99,11 +105,19 @@ public static function generateDateOfBirth(): string ); } - public static function getGenderCode($idNumber): string + /** + * @param $idNumber + * @return int + */ + 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'); } + /** + * @param $idNumber + * @return string + */ public static function getBirthDate($idNumber): string { $default_century = 19; @@ -111,8 +125,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); @@ -122,6 +134,10 @@ public static function getBirthDate($idNumber): string return $year.'-'.$month.'-'.$day; } + /** + * @param $idNumber + * @return int + */ public static function getAgeFromIdNumber($idNumber): int { $birthDay = self::getBirthDate($idNumber); @@ -132,6 +148,12 @@ public static function getAgeFromIdNumber($idNumber): int return (int) $currentYear - (int) $birthYear; } + /** + * @param $attribute + * @param $value + * @param $parameters + * @return bool + */ public static function validateIdNumber($attribute, $value, $parameters): bool { /* From 749f9710e5b1c04fc8b4775607c3bda031e14f84 Mon Sep 17 00:00:00 2001 From: ettiennelouw Date: Mon, 11 Dec 2023 10:32:29 +0000 Subject: [PATCH 12/23] Fix styling --- config/utilities.php | 2 +- src/Helpers/IdNumberHelper.php | 24 ------------------------ 2 files changed, 1 insertion(+), 25 deletions(-) diff --git a/config/utilities.php b/config/utilities.php index de080eb..0d7ce10 100644 --- a/config/utilities.php +++ b/config/utilities.php @@ -59,6 +59,6 @@ 'male' => 1, 'female' => 2, 'unknown' => 3, - ] + ], ]; diff --git a/src/Helpers/IdNumberHelper.php b/src/Helpers/IdNumberHelper.php index 131dcad..e707bfa 100644 --- a/src/Helpers/IdNumberHelper.php +++ b/src/Helpers/IdNumberHelper.php @@ -40,9 +40,6 @@ public static function generateIdNumber($dateOfBirth, int $male = 0): string return $total; } - /** - * @return string - */ public static function generateFakeId(): string { $minYear = 20; @@ -92,9 +89,6 @@ public static function generateFakeId(): string return Str::limit($tempId.$lastDigit, 13); } - /** - * @return string - */ public static function generateDateOfBirth(): string { return str_pad((string) mt_rand(60, 99), 2, '0', STR_PAD_LEFT).str_pad(mt_rand(1, 12), 2, '0', STR_PAD_LEFT).str_pad( @@ -105,19 +99,11 @@ public static function generateDateOfBirth(): string ); } - /** - * @param $idNumber - * @return int - */ public static function getGenderCode($idNumber): int { return (substr($idNumber, 6, 4) < 5000) ? config('utilities.gender.female') : config('utilities.gender.male'); } - /** - * @param $idNumber - * @return string - */ public static function getBirthDate($idNumber): string { $default_century = 19; @@ -134,10 +120,6 @@ public static function getBirthDate($idNumber): string return $year.'-'.$month.'-'.$day; } - /** - * @param $idNumber - * @return int - */ public static function getAgeFromIdNumber($idNumber): int { $birthDay = self::getBirthDate($idNumber); @@ -148,12 +130,6 @@ public static function getAgeFromIdNumber($idNumber): int return (int) $currentYear - (int) $birthYear; } - /** - * @param $attribute - * @param $value - * @param $parameters - * @return bool - */ public static function validateIdNumber($attribute, $value, $parameters): bool { /* From 08da48616a17a05b1b3117cf8697e12425d5ad48 Mon Sep 17 00:00:00 2001 From: Ettienne Louw Date: Mon, 11 Dec 2023 13:03:59 +0200 Subject: [PATCH 13/23] Removed limit ending characters --- src/Helpers/IdNumberHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Helpers/IdNumberHelper.php b/src/Helpers/IdNumberHelper.php index 131dcad..9862ba7 100644 --- a/src/Helpers/IdNumberHelper.php +++ b/src/Helpers/IdNumberHelper.php @@ -89,7 +89,7 @@ public static function generateFakeId(): string $totalMod = ($total % 10); $lastDigit = 10 - $totalMod; - return Str::limit($tempId.$lastDigit, 13); + return Str::limit($tempId.$lastDigit, 13, ''); } /** From a9697dc07adf89faadd67cc85eefbb3dda90cabc Mon Sep 17 00:00:00 2001 From: ettiennelouw Date: Mon, 11 Dec 2023 11:09:04 +0000 Subject: [PATCH 14/23] Update CHANGELOG --- CHANGELOG.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b7076a9..feda291 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,18 @@ All notable changes to `utilities` will be documented in this file. +## 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 From 7a2a153571c4be1e3efc54803e7423c29434e55f Mon Sep 17 00:00:00 2001 From: Ettienne Louw Date: Tue, 16 Jan 2024 13:57:11 +0200 Subject: [PATCH 15/23] Removed time() append on random int as this does not do what the function says --- src/Helpers/NumberHelper.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/Helpers/NumberHelper.php b/src/Helpers/NumberHelper.php index 230b910..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; } From b6be757e36ceceb19abf959217a3133c82252398 Mon Sep 17 00:00:00 2001 From: ettiennelouw Date: Tue, 16 Jan 2024 11:58:36 +0000 Subject: [PATCH 16/23] Update CHANGELOG --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index feda291..065b197 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to `utilities` will be documented in this file. +## 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 From 3ad6fa4c32d4e764ab45aa534b1c8b7a375a0ca6 Mon Sep 17 00:00:00 2001 From: Marius Odendaal Date: Tue, 2 Jul 2024 12:29:02 +0200 Subject: [PATCH 17/23] Add disk option to download media. To be used to download file as original filename instead of generated filename --- src/Helpers/SpatieMediaHelper.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Helpers/SpatieMediaHelper.php b/src/Helpers/SpatieMediaHelper.php index 5e23ed3..e52584f 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,9 @@ 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); + return Storage::disk($disk ?? $media->disk)->download($media->getPath(), ($asFilename) ? $media->name : $media->file_name); } public static function delete(Media $media): ?bool From d2eca3dac56a6d874dd43fba4bae5e0672fe86df Mon Sep 17 00:00:00 2001 From: mariusdatakrag Date: Tue, 2 Jul 2024 10:29:41 +0000 Subject: [PATCH 18/23] Fix styling --- src/Helpers/IdNumberHelper.php | 4 ++-- src/Traits/TriggerHelper.php | 2 +- src/Utilities.php | 4 +--- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Helpers/IdNumberHelper.php b/src/Helpers/IdNumberHelper.php index a677bbd..487f72f 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 */ 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 {} From 34826a82e5a9bc2c48c347f0e6dedf2777b7b730 Mon Sep 17 00:00:00 2001 From: Marius Odendaal Date: Tue, 2 Jul 2024 16:18:46 +0200 Subject: [PATCH 19/23] Refactor to use default storage --- src/Helpers/SpatieMediaHelper.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Helpers/SpatieMediaHelper.php b/src/Helpers/SpatieMediaHelper.php index e52584f..96e7c8e 100644 --- a/src/Helpers/SpatieMediaHelper.php +++ b/src/Helpers/SpatieMediaHelper.php @@ -38,7 +38,11 @@ public static function createFromString($model, string $string, string $name, st public static function download(Media $media, bool $asFilename = false, $disk = null): StreamedResponse|Media { - return Storage::disk($disk ?? $media->disk)->download($media->getPath(), ($asFilename) ? $media->name : $media->file_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 From 4b151bce6fa7aa37f2cc9f345d9e33079425a8b9 Mon Sep 17 00:00:00 2001 From: mariusdatakrag Date: Tue, 2 Jul 2024 14:19:08 +0000 Subject: [PATCH 20/23] Fix styling --- src/Helpers/SpatieMediaHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Helpers/SpatieMediaHelper.php b/src/Helpers/SpatieMediaHelper.php index 96e7c8e..5f92301 100644 --- a/src/Helpers/SpatieMediaHelper.php +++ b/src/Helpers/SpatieMediaHelper.php @@ -38,7 +38,7 @@ public static function createFromString($model, string $string, string $name, st public static function download(Media $media, bool $asFilename = false, $disk = null): StreamedResponse|Media { - if(!is_null($disk)) { + if (! is_null($disk)) { return Storage::disk($disk)->download($media->getPath(), ($asFilename) ? $media->name : $media->file_name); } From 315c6d30622d4de390a2cf608b1262f5b9e77bb9 Mon Sep 17 00:00:00 2001 From: mariusdatakrag Date: Tue, 2 Jul 2024 14:22:53 +0000 Subject: [PATCH 21/23] Update CHANGELOG --- CHANGELOG.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 065b197..9957158 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,18 @@ 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 From 65c5f927fcf558ec1abf310a20a7f6328922a6d6 Mon Sep 17 00:00:00 2001 From: Jc Duvenage Date: Fri, 20 Sep 2024 14:31:14 +0200 Subject: [PATCH 22/23] Add method to calculate age from ID number Introduce a new method `getBirthdayFromIdNumber()` in `IdNumberHelper` to calculate the age based on the given ID number. This involves extracting the birth date from the ID and computing the age using the current full date. --- src/Helpers/IdNumberHelper.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Helpers/IdNumberHelper.php b/src/Helpers/IdNumberHelper.php index e493a4d..fbd8dab 100644 --- a/src/Helpers/IdNumberHelper.php +++ b/src/Helpers/IdNumberHelper.php @@ -217,4 +217,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); + } } From a989dcd5dc9de06fd99fbfc8254e9e38fe37d46a Mon Sep 17 00:00:00 2001 From: JCDuvenage Date: Fri, 20 Sep 2024 12:31:48 +0000 Subject: [PATCH 23/23] Fix styling --- src/Helpers/IdNumberHelper.php | 6 +++--- src/Traits/HasCompositePrimaryKey.php | 2 +- src/Traits/PasswordStrength.php | 2 +- src/Traits/TriggerHelper.php | 2 +- src/Utilities.php | 4 +--- 5 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/Helpers/IdNumberHelper.php b/src/Helpers/IdNumberHelper.php index fbd8dab..67469e7 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; 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/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 {}