Skip to content

Commit

Permalink
add config file
Browse files Browse the repository at this point in the history
add facade for use in blade templates
improve sort functionality
  • Loading branch information
lessmore92 committed Jul 9, 2020
1 parent 6dea1b9 commit 7cdd76e
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 14 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@
"laravel": {
"providers": [
"Lessmore92\\EloquentMarshall\\EloquentMarshallServiceProvider"
]
],
"aliases": {
"EloquentMarshall": "Lessmore92\\EloquentMarshall\\Facades\\EloquentMarshall"
}
}
}
}
155 changes: 155 additions & 0 deletions src/EloquentMarshall.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php
/**
* User: Lessmore92
* Date: 7/9/2020
* Time: 3:16 AM
*/

namespace Lessmore92\EloquentMarshall;


class EloquentMarshall
{
private $config;

public function __construct()
{
$this->config = /** @scrutinizer ignore-call */
config('eloquent_marshall');
}

/**
* @param string $key
* @return string
*/
public function getSortingUrl($key)
{
$sorts = $this->getSortsParameters();
$new_order = $this->config['default_sort_order'];
$new_sort = [];
foreach ($sorts as $sort)
{
list($order_by, $order) = array_pad(explode(':', $sort, 2), 2, null);
if (is_null($order))
{
$order = $this->config['default_sort_order'];
}
if ($order_by == $key)
{
$new_order = 'asc';
if (strtolower($order) == 'asc')
{
$new_order = 'desc';
}
$new_sort[$this->config['sort_param_name']][$order_by] = $order_by . ':' . $new_order;
}
else
{
$new_sort[$this->config['sort_param_name']][$order_by] = $order_by . ':' . $order;
}
}
if (!isset($new_sort[$this->config['sort_param_name']][$key]))
{
$new_sort[$this->config['sort_param_name']][$key] = $key . ':' . $new_order;
}

$new_sort[$this->config['sort_param_name']] = array_values($new_sort[$this->config['sort_param_name']]);

$clean_url = $this->getFullUrlWithoutSortParameter();

return $this->addSortParameterToUrl($clean_url, $new_sort);
}

/**
* @param string $key
* @param string $no_order_class
* @param string $asc_class
* @param string $desc_class
* @return string
*/
public function getSortingClass($key, $no_order_class, $asc_class, $desc_class)
{
$sorts = $this->getSortsParameters();
$out = $no_order_class;
foreach ($sorts as $sort)
{
list($order_by, $order) = array_pad(explode(':', $sort, 2), 2, null);

if (is_null($order))
{
$order = $this->config['default_sort_order'];
}
if ($order_by == $key)
{
if (strtolower($order) == 'asc')
{
$out = $asc_class;
}
else
{
$out = $desc_class;
}
break;
}
}
return $out;
}

/**
* @param string $key
* @return bool
*/
public function haveSorting($key = null)
{
$sorts = $this->getSortsParameters();
if ($key)
{
$found = false;
foreach ($sorts as $sort)
{
list($order_by) = array_pad(explode(':', $sort, 2), 2, null);

if ($order_by == $key)
{
$found = true;
}
}
return $found;
}
return count($sorts) > 0;
}

/**
* @return array
*/
private function getSortsParameters()
{
$sorts = request($this->config['sort_param_name'], []);
if (!is_array($sorts))
{
$sorts = [$sorts];
}
return $sorts;
}

/**
* @return string
*/
private function getFullUrlWithoutSortParameter()
{
$url = url()->current();
$query = request()->query();
unset($query[$this->config['sort_param_name']]);
return $query ? $url . '?' . http_build_query($query) : $url;
}

/**
* @param string $url
* @param array $sorts
* @return string
*/
private function addSortParameterToUrl($url, $sorts)
{
return strpos($url, '?') !== false ? $url . '&' . http_build_query($sorts) : $url . '?' . http_build_query($sorts);
}
}
17 changes: 17 additions & 0 deletions src/EloquentMarshallServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class EloquentMarshallServiceProvider extends ServiceProvider
{
public function register()
{
$this->mergeConfigFrom(__DIR__ . '/config/config.php', 'eloquent_marshall');

$this->app->bind(
SortParameterParserInterface::class,
SortParameterParser::class
Expand All @@ -22,6 +24,10 @@ public function register()
return new ParameterStore();
});

$this->app->bind('eloquent-marshall', function ($app) {
return new EloquentMarshall();
});

Builder::/** @scrutinizer ignore-call */ macro('searchable', function ($inputs = null) {
$searchable = new Searchable();
if ($inputs === null)
Expand Down Expand Up @@ -72,4 +78,15 @@ public function register()
return $store->get();
});
}

public function boot()
{
if ($this->app->runningInConsole())
{
$this->publishes([
__DIR__ . '/config/config.php' => config_path('eloquent_marshall.php'),
], 'config');

}
}
}
18 changes: 18 additions & 0 deletions src/Facades/EloquentMarshall.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* User: Lessmore92
* Date: 7/9/2020
* Time: 3:14 AM
*/

namespace Lessmore92\EloquentMarshall\Facades;

use Illuminate\Support\Facades\Facade;

class EloquentMarshall extends Facade
{
protected static function getFacadeAccessor()
{
return 'eloquent-marshall';
}
}
22 changes: 9 additions & 13 deletions src/Foundation/SortParameterParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ public function parse($sort_parameter): array
{
$_sorts = [];
$_default_order = /** @scrutinizer ignore-call */
config('eloquent-marshall.default_sort_order', 'desc');
if (!$this->sort_string_is_valid($sort_parameter))
config('eloquent_marshall.default_sort_order', 'desc');

if (!$this->sort_is_parsable($sort_parameter))
{
return $_sorts;
}

if ($this->sort_string_is_explodable($sort_parameter))
if ($this->sort_is_array($sort_parameter))
{
$sorts = explode(',', $sort_parameter);
$sorts = $sort_parameter;
}
else
{
Expand Down Expand Up @@ -53,23 +54,18 @@ public function parse($sort_parameter): array
* @param string $sort
* @return bool
*/
private function sort_string_is_valid($sort)
private function sort_is_parsable($sort)
{
if (strlen(trim($sort)) <= 0)
{
return false;
}

return true;
return is_array($sort) || (is_string($sort) && strlen(trim($sort))) > 0;
}

/**
* @param string $sort
* @return bool
*/
private function sort_string_is_explodable($sort)
private function sort_is_array($sort)
{
return strpos($sort, ',') !== false;
return is_array($sort);
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/config/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

return [
'sort_param_name' => 'sort',
'default_sort_order' => 'desc',
];

0 comments on commit 7cdd76e

Please sign in to comment.