Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
8f88f30
Update CHANGELOG
ettiennelouw Nov 20, 2023
d45713c
Added function to combine cell number and country prefix if first cha…
ettiennelouw Nov 23, 2023
d76e103
Fix styling
ettiennelouw Nov 23, 2023
1ef4993
Update CHANGELOG
ettiennelouw Nov 23, 2023
ac51c0d
Fixed DropDownSchema added default value for status and fixed space i…
ettiennelouw Nov 27, 2023
71fd30a
Merge branch 'main' of github.com:PropaySystems/utilities
ettiennelouw Nov 27, 2023
33896cd
Update CHANGELOG
ettiennelouw Nov 27, 2023
d198926
Added save relation trait
ettiennelouw Nov 27, 2023
9af72a8
Merge branch 'main' of github.com:PropaySystems/utilities
ettiennelouw Nov 27, 2023
bc899ce
Update CHANGELOG
ettiennelouw Nov 27, 2023
42c76bb
Made dropdown schema proper foreign key
ettiennelouw Nov 27, 2023
625f817
Update CHANGELOG
ettiennelouw Nov 27, 2023
910f7fc
Added config value for get gender code from id number.
ettiennelouw Dec 11, 2023
749f971
Fix styling
ettiennelouw Dec 11, 2023
d3375a2
Merge branch 'main' into Fixes-to-id-number-helper
ettiennelouw Dec 11, 2023
08da486
Removed limit ending characters
ettiennelouw Dec 11, 2023
556c7c3
Merge branch 'Fixes-to-id-number-helper' of github.com:PropaySystems/…
ettiennelouw Dec 11, 2023
4adc017
Merge pull request #2 from PropaySystems/Fixes-to-id-number-helper
ettiennelouw Dec 11, 2023
a9697dc
Update CHANGELOG
ettiennelouw Dec 11, 2023
7a2a153
Removed time() append on random int as this does not do what the func…
ettiennelouw Jan 16, 2024
b6be757
Update CHANGELOG
ettiennelouw Jan 16, 2024
3ad6fa4
Add disk option to download media. To be used to download file as ori…
mariusdatakrag Jul 2, 2024
d2eca3d
Fix styling
mariusdatakrag Jul 2, 2024
34826a8
Refactor to use default storage
mariusdatakrag Jul 2, 2024
4b151bc
Fix styling
mariusdatakrag Jul 2, 2024
880b9da
Merge pull request #6 from PropaySystems/AddDiskOnSpatieDownload
mariusdatakrag Jul 2, 2024
315c6d3
Update CHANGELOG
mariusdatakrag Jul 2, 2024
65c5f92
Add method to calculate age from ID number
JCDuvenage Sep 20, 2024
a989dcd
Fix styling
JCDuvenage Sep 20, 2024
4d38897
Merge branch 'main' into FullDateBirthdayCalculation
JCDuvenage Sep 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```

### <u>-- Route Helper --</u>
Check of the string is in the current route name
Expand Down
38 changes: 38 additions & 0 deletions config/utilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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,
],

];
22 changes: 14 additions & 8 deletions src/Helpers/IdNumberHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
}
22 changes: 16 additions & 6 deletions src/Helpers/NumberHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
}
}
10 changes: 7 additions & 3 deletions src/Helpers/SpatieMediaHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PropaySystems\Utilities\Helpers;

use Illuminate\Support\Facades\Storage;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Symfony\Component\HttpFoundation\StreamedResponse;

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Helpers/TranslationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
4 changes: 1 addition & 3 deletions src/Traits/DropdownSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/Traits/HasCompositePrimaryKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/PasswordStrength.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
71 changes: 71 additions & 0 deletions src/Traits/SaveRelations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace PropaySystems\Utilities\Traits;

use App\Traits\instance;

trait SaveRelations
{
/**
* Save belongsTo relations
*
* @param instance $model
* @param array $relations
* @return null
*/
public function saveBelongsTo($model, $relations = [])
{

//check if not empty
if (! empty($relations)) {
foreach ($relations as $relation => $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);
}
}
2 changes: 1 addition & 1 deletion src/Traits/TriggerHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
trait TriggerHelper
{
/**
* @param $state
* @param $state
*/
public static function switchDatabaseTrigger($enable = true, $table = null, $trigger = null, string $connection = 'sqlsrv'): void
{
Expand Down
4 changes: 1 addition & 3 deletions src/Utilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@

namespace PropaySystems\Utilities;

class Utilities
{
}
class Utilities {}
6 changes: 6 additions & 0 deletions tests/Unit/NumberHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});