Skip to content

Commit

Permalink
Added Minify, User Helper. Update Test
Browse files Browse the repository at this point in the history
  • Loading branch information
nasrulhazim committed Sep 8, 2018
1 parent 204ad0d commit 5e0009d
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 7 deletions.
59 changes: 53 additions & 6 deletions src/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* generate sequence
* @return sequence based on length supplied
*/
if (!function_exists('generate_sequence')) {
if (! function_exists('generate_sequence')) {
function generate_sequence($input = 1)
{
return str_pad($input, config('helper.sequence_length'), '0', STR_PAD_LEFT);
Expand All @@ -16,7 +16,7 @@ function generate_sequence($input = 1)
/*
* Get Abbreviation fo the given text
*/
if (!function_exists('abbrv')) {
if (! function_exists('abbrv')) {
function abbrv($value, $unique_characters = true)
{
if (true === config('helper.abbrv.remove_non_alphanumeric')) {
Expand All @@ -36,7 +36,7 @@ function abbrv($value, $unique_characters = true)
$split = str_split($value);
$unique_characters = [];
foreach ($split as $character) {
if (!in_array($character, $unique_characters)) {
if (! in_array($character, $unique_characters)) {
$unique_characters[] = $character;
}
}
Expand All @@ -51,7 +51,7 @@ function abbrv($value, $unique_characters = true)
/*
* Get Fully Qualified Class Name (FQCN) for an Object
*/
if (!function_exists('fqcn')) {
if (! function_exists('fqcn')) {
function fqcn($object)
{
return get_class($object);
Expand All @@ -61,7 +61,7 @@ function fqcn($object)
/*
* Get Slug Name for Fully Qualified Class Name (FQCN)
*/
if (!function_exists('str_slug_fqcn')) {
if (! function_exists('str_slug_fqcn')) {
function str_slug_fqcn($object)
{
return Str::kebab(fqcn($object));
Expand All @@ -71,9 +71,56 @@ function str_slug_fqcn($object)
/*
* Send Notification To User
*/
if (!function_exists('notify')) {
if (! function_exists('notify')) {
function notify($identifier, $column = 'id')
{
return \CleaniqueCoders\LaravelHelper\Services\NotificationService::make($identifier, $column);
}
}

/*
* user() helper
*/
if (! function_exists('user')) {
function user()
{
foreach (config('auth.guards') as $key => $value) {
if (Auth::guard($key)->check()) {
return Auth::guard($key)->user();
}
}

return null;
}
}

/*
* Minify given HTML Content
*/
if (! function_exists('minify')) {
function minify($value)
{
$replace = [
'/<!--[^\[](.*?)[^\]]-->/s' => '',
"/<\?php/" => '<?php ',
"/\n([\S])/" => '$1',
"/\r/" => '',
"/\n/" => '',
"/\t/" => '',
'/ +/' => ' ',
];

if (false !== strpos($value, '<pre>')) {
$replace = [
'/<!--[^\[](.*?)[^\]]-->/s' => '',
"/<\?php/" => '<?php ',
"/\r/" => '',
"/>\n</" => '><',
"/>\s+\n</" => '><',
"/>\n\s+</" => '><',
];
}

return preg_replace(array_keys($replace), array_values($replace), $value);
}
}
2 changes: 1 addition & 1 deletion tests/HelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function it_has_helper_config_file()
/** @test */
public function it_has_all_helpers()
{
$helpers = ['abbrv', 'generate_sequence', 'fqcn', 'str_slug_fqcn'];
$helpers = ['abbrv', 'generate_sequence', 'fqcn', 'str_slug_fqcn', 'notify', 'user', 'minify'];

foreach ($helpers as $helper) {
$this->assertTrue(function_exists($helper));
Expand Down
28 changes: 28 additions & 0 deletions tests/MinifyHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace CleaniqueCoders\LaravelHelper\Tests;

class MinifyHelperTest extends TestCase
{
/** @test */
public function it_has_minify_helper()
{
$this->assertTrue(function_exists('minify'));
}

/** @test */
public function it_can_minify_html()
{
$content = '<html>
<head>
<title>Laravel Helper</title>
</head>
<body>
<h1>Laravel Helper</h1>
</body>
</html>';
$expected = '<html><head><title>Laravel Helper</title></head><body><h1>Laravel Helper</h1></body></html>';
$actual = minify($content);
$this->assertEquals($expected, $actual);
}
}
12 changes: 12 additions & 0 deletions tests/UserHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace CleaniqueCoders\LaravelHelper\Tests;

class UserHelperTest extends TestCase
{
/** @test */
public function it_has_user_helper()
{
$this->assertTrue(function_exists('user'));
}
}

0 comments on commit 5e0009d

Please sign in to comment.