Skip to content

Commit

Permalink
New: auto_cast() + days_next_month() + operating_system()
Browse files Browse the repository at this point in the history
  • Loading branch information
repat committed Mar 17, 2019
1 parent 703bf53 commit c54e7c1
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 2 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Also have a look at
* https://laravel.com/docs/5.7/helpers
* http://calebporzio.com/11-awesome-laravel-helper-functions/
* https://packagist.org/packages/illuminated/helper-functions

Ideas what should go in here? Write a pull request or email!

Expand Down Expand Up @@ -77,6 +78,7 @@ str_icontains('foobar', 'test');
### date
* `days_this_month()`
* `days_this_year()`
* `days_next_month()`
* `days_left_in_month()`
* `days_left_in_year()`
* `timezone_list()`
Expand All @@ -88,11 +90,15 @@ str_icontains('foobar', 'test');
### html
* `linkify()`
* `embedded_video_url()`
* `extract_inline_img()`

### misc
* `human_filesize()`
* `generate_password()`
* `zenith()`
* `permutations()`
* `auto_cast()`
* `operating_system()`

### networking
* `http_status_code()`
Expand Down Expand Up @@ -124,7 +130,7 @@ str_icontains('foobar', 'test');
* MIT, see [LICENSE](https://github.com/repat/laravel-helper/blob/master/LICENSE)

## Version
* Version 0.1.9.2
* Version 0.1.11

## Contact
#### repat
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"keywords": ["laravel", "helper", "files", "array", "database", "strings", "date"],
"homepage": "http://repat.de",
"license": "MIT",
"version" : "0.1.10",
"version" : "0.1.11",
"authors": [
{"name": "repat", "email": "repat@repat.de"}
],
Expand Down
12 changes: 12 additions & 0 deletions src/date_helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ function days_this_month() : int
}
}

if (!function_exists('days_next_month')) {
/**
* How many days are in next month (28/29/30/31)
*
* @return int
*/
function days_next_month() : int
{
return cal_days_in_month(CAL_GREGORIAN, now()->addMonth()->month, now()->year);
}
}

if (!function_exists('days_this_year')) {
/**
* How many days are in current year, depending on leap year
Expand Down
6 changes: 6 additions & 0 deletions src/defines.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@
define('REGEX_WORD_BOUNDARY', '\b');
define('REGEX_IMG_BASE64_SRC', '/src=\"data:image\/([a-zA-Z]*);base64,([^\"]*)\"/');
define('REGEX_IMG_BASE64_REPLACE', '/src=(\"data:image\/[a-zA-Z]*;base64,[^\"]*)\"/');

// Operating Systems
define('MACOS', 'macos');
define('WINDOWS', 'windows');
define('LINUX', 'linux');
define('BSD', 'bsd');
47 changes: 47 additions & 0 deletions src/misc_helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,50 @@ function permutations(array $pool, ?array $r = null) : Generator
}
}
}

if (!function_exists('auto_cast')) {
/**
* Will automatically cast strings into float/int or bool values
*
* @param mixed $value
* @return mixed
*/
function auto_cast($value)
{
if (is_string($value)) {
if (is_numeric($value)) {
if (str_contains($value, '.')) {
// float, real or double
return floatval($value);
} else {
return intval($value);
}
} elseif (strtolower($value) === 'true' || strtolower($value) === 'false') {
return boolval($value);
}
}
return $value;
}
}

if (!function_exists('operating_system')) {
/**
* Returns Operating System, see constants in `defines.php`
*
* @return string|null
*/
function operating_system() : ?string
{
$uname = php_uname();
if (str_contains($uname, 'Darwin')) {
return MACOS;
} elseif (str_contains($uname, 'Linux')) {
return LINUX;
} elseif (str_icontains($uname, 'bsd')) {
return BSD;
} elseif (str_contains($uname, 'Windows')) {
return WINDOWS;
}
return null;
}
}

0 comments on commit c54e7c1

Please sign in to comment.