Skip to content

Commit

Permalink
Three new functions + comments => 0.1.15
Browse files Browse the repository at this point in the history
  • Loading branch information
repat committed Mar 29, 2019
1 parent cd46b39 commit cf622bb
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 13 deletions.
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
vendor/
.DS_Store
composer.lock
doc/
docs/
html/
latex/
output/
vendor/
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,13 @@ str_icontains('foobar', 'test');
* `http_status_code()`
* `domain_slug()`
* `scrub_url()`
* `parse_signed_request()`

### object
* `object2array()`
* `morph_map()`
* `morph_map_key()`
* `cache_get_or_add()`

### optional packages
* `markdown2html()`
Expand All @@ -122,10 +124,11 @@ str_icontains('foobar', 'test');
* `str_bytes()`
* `str_replace_once()`
* `title_case_wo_underscore()`
* `hyphen2_`
* `hyphen2_()`
* `_2hypen()`
* `regex_list()`
* `to_ascii()`
* `base64_url_decode()`

## Contributors
* https://github.com/bertholf
Expand All @@ -134,7 +137,7 @@ str_icontains('foobar', 'test');
* MIT, see [LICENSE](https://github.com/repat/laravel-helper/blob/master/LICENSE)

## Version
* Version 0.1.14
* Version 0.1.15

## 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.14",
"version" : "0.1.15",
"authors": [
{"name": "repat", "email": "repat@repat.de"}
],
Expand Down
5 changes: 5 additions & 0 deletions src/date_helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ function timezone_list() : array
}

if (!function_exists('tomorrow')) {
/**
* Carbon instance of tomorrow, similar to `today()`
*
* @return \Carbon\Carbon
*/
function tomorrow() : \Carbon\Carbon
{
return \Carbon\Carbon::today()->addDay();
Expand Down
58 changes: 55 additions & 3 deletions src/defines.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,67 @@
<?php

// Time + Date
/*
|--------------------------------------------------------------------------
| Dates
|--------------------------------------------------------------------------
*/

/**
* Days in a normal year (not leap-year)
* @var int
*/
define('DAYS_PER_YEAR', 365);

// Regex
/*
|--------------------------------------------------------------------------
| Regex
|--------------------------------------------------------------------------
*/

/**
* Word boundry
* @var string
*/
define('REGEX_WORD_BOUNDARY', '\b');

/**
* String to parse a base64 encoded inline image
* @var string
*/
define('REGEX_IMG_BASE64_SRC', '/src=\"data:image\/([a-zA-Z]*);base64,([^\"]*)\"/');

/**
* String to replace a base64 encoded inline image
* @var string
*/
define('REGEX_IMG_BASE64_REPLACE', '/src=(\"data:image\/[a-zA-Z]*;base64,[^\"]*)\"/');

// Operating Systems
/*
|--------------------------------------------------------------------------
| Operating Systems
|--------------------------------------------------------------------------
*/

/**
* Operating System: MacOS(Darwin)
* @var string
*/
define('MACOS', 'macos');

/**
* Operating System: Windows
* @var string
*/
define('WINDOWS', 'windows');

/**
* Operating System: Linux
* @var string
*/
define('LINUX', 'linux');

/**
* Operating System: BSD
* @var string
*/
define('BSD', 'bsd');
29 changes: 29 additions & 0 deletions src/networking_helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,32 @@ function scrub_url(string $url) : string
return $url;
}
}

if (!function_exists('parse_signed_request')) {
/**
* Parses a sha256 signed request JSON into array
* Source: https://developers.facebook.com/docs/apps/delete-data
*
* @param string $signedRequest
* @param string $secret
* @param string $algo
* @return array
*/
function parse_signed_request(string $signedRequest, string $secret, string $algo = 'sha256') : array
{
list($encodedSig, $payload) = explode('.', $signedRequest, 2);

// decode the data
$sig = base64_url_decode($encodedSig);
$data = json_decode(base64_url_decode($payload), JSON_OBJECT_AS_ARRAY);

// confirm the signature
$expectedSig = hash_hmac($algo, $payload, $secret, $raw = true);
if ($sig !== $expectedSig) {
// Bad Signed JSON signature
return null;
}

return $data;
}
}
16 changes: 16 additions & 0 deletions src/object_helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,19 @@ function object2array($stdClassObject) : array
return $array;
}
}

if (!function_exists('cache_get_or_add')) {
/**
* Transforms an object into an array
*
* @param string $key
* @param callable $function
* @return mixed
*/
function cache_get_or_add(string $key, callable $function)
{
$result = Cache::get($key, $function);
Cache::add($result);
return $result;
}
}
43 changes: 37 additions & 6 deletions src/string_helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,35 +105,66 @@ function to_ascii(string $rawstring): string
}
}

/*
|--------------------------------------------------------------------------
| Need Comments
|--------------------------------------------------------------------------
*/

if (!function_exists('title_case_wo_underscore')) {
/**
* `title_case` but with every underscore replaced by a space
* @param string $string
* @return string
*/
function title_case_wo_underscore(string $string): string
{
return str_replace('_', ' ', title_case($string));
}
}

if (!function_exists('hyphen2_')) {
/**
* Replaces every hyphen("-") with underscore("_")
*
* @param string $string
* @return string
*/
function hyphen2_(string $string): string
{
return str_replace('-', '_', $string);
}
}

if (!function_exists('_2hyphen')) {
/**
* Replaces every underscore("_") with hyphen("-")
*
* @param string $string
* @return string
*/
function _2hyphen(string $string): string
{
return str_replace('_', '-', $string);
}
}
if (!function_exists('regex_list')) {
/**
* Regex string pipe separated for an array
*
* @param array $array
* @return string
*/
function regex_list(array $array): string
{
return REGEX_WORD_BOUNDARY . implode('|' . REGEX_WORD_BOUNDARY, $array);
}
}

if (!function_exists('base64_url_decode')) {
/**
* Decodes a base64 encoded url
* Source: https://developers.facebook.com/docs/apps/delete-data
*
* @param string $input
* @return string
*/
function base64_url_decode(string $input) : string
{
return base64_decode(strtr($input, '-_', '+/'));
}
}

0 comments on commit cf622bb

Please sign in to comment.