Skip to content

Commit

Permalink
Removing older Laravel string and array helpers
Browse files Browse the repository at this point in the history
Also utilized `withCount()` when appropriate.
  • Loading branch information
ChrisThompsonTLDR committed Jun 23, 2020
1 parent 7aa9d38 commit 9f184ab
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 74 deletions.
4 changes: 2 additions & 2 deletions resources/views/filters/checkbox.blade.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="form-group filter">
<div id="filter{{ title_case($filter->field) }}" class="checkbox">
<div id="filter{{ Str::of($filter->field)->title() }}" class="checkbox">
<label>
{!! Form::checkbox('filter_' . $filter->field, isset($filter->value) ? $filter->value : true, isset($params['filter_' . $filter->field]) ? true : false) !!} {{ $filter->display }}
</label>
</div>
</div>
</div>
4 changes: 2 additions & 2 deletions resources/views/filters/datetime-range.blade.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<div class="form-group filter">
<div id="filter{{ title_case($filter->field) }}">
<div id="filter{{ Str::of($filter->field)->title() }}">
@if(!isset($filter->label) || $filter->label === true)<div><strong>{!! $filter->display !!}</strong></div>@endif
{!! Form::text('filter_' . $filter->field . '_start', isset($params['filter_' . $filter->field . '_start']) ? $params['filter_' . $filter->field . '_start'] : null, ['class' => 'form-control']) !!}
to
{!! Form::text('filter_' . $filter->field . '_end', isset($params['filter_' . $filter->field . '_end']) ? $params['filter_' . $filter->field . '_end'] : null, ['class' => 'form-control']) !!}
</div>
</div>
</div>
4 changes: 2 additions & 2 deletions resources/views/filters/input.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="form-group filter">
<div id="filter{{ title_case($filter->field) }}">
<div id="filter{{ Str::of($filter->field)->title() }}">
@if(!isset($filter->label) || $filter->label === true)<div><strong>{!! $filter->display !!}</strong></div>@endif
{{ Form::text('filter[' . $filter->field . ']', isset($params['filter'][$filter->field]) ? $params['filter'][$filter->field] : null, ['class' => 'form-control']) }}
</div>
</div>
</div>
4 changes: 2 additions & 2 deletions resources/views/filters/range.blade.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<div class="form-group filter">
<div id="filter{{ title_case($filter->field) }}">
<div id="filter{{ Str::of($filter->field)->title() }}">
@if(!isset($filter->label) || $filter->label === true)<div><strong>{!! $filter->display !!}</strong></div>@endif
{!! Form::text('filter[' . $filter->field . '-start]', isset($params['filter'][$filter->field . '-start']) ? $params['filter'][$filter->field . '-start'] : null, ['class' => 'form-control']) !!}
to
{!! Form::text('filter[' . $filter->field . '-end]', isset($params['filter'][$filter->field . '-end']) ? $params['filter'][$filter->field . '-end'] : null, ['class' => 'form-control']) !!}
</div>
</div>
</div>
4 changes: 2 additions & 2 deletions resources/views/filters/select.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="form-group filter">
<div id="filter{{ title_case($filter->field) }}">
<div id="filter{{ Str::of($filter->field)->title() }}">
@if(!isset($filter->label) || $filter->label === true)<div><strong>{!! $filter->display !!}</strong></div>@endif
{!! Form::select('filter[' . $filter->field . ']', $filter->values, isset($params['filter'][$filter->field]) ? $params['filter'][$filter->field] : null, ['class' => 'form-control', 'placeholder' => isset($filter->placeholder) ? $filter->placeholder : '']) !!}
</div>
</div>
</div>
5 changes: 3 additions & 2 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Route;
use Illuminate\Routing\Router;
use Illuminate\Support\Str;

class ServiceProvider extends \Illuminate\Support\ServiceProvider
{
Expand Down Expand Up @@ -43,7 +44,7 @@ public static function resource($path, $controller = '', array $options = [])
{
// auto build controller name
if (empty($controller)) {
$controller = str_replace(' ', '', \Str::title(str_replace('-', ' ', \Str::singular($path)))) . 'Controller';
$controller = str_replace(' ', '', Str::title(str_replace('-', ' ', Str::singular($path)))) . 'Controller';
}

Route::post($path . '/filter', ['as' => config('laraman.route.prefixDot') . $path . '.filter', 'uses' => $controller . '@filter']);
Expand All @@ -63,4 +64,4 @@ public static function resource($path, $controller = '', array $options = [])

Route::resource($path, $controller, $options);
}
}
}
103 changes: 55 additions & 48 deletions src/Traits/LaramanController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use DB;
use Illuminate\Support\Facades\View;
use Carbon\Carbon;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Schema;

trait LaramanController
Expand Down Expand Up @@ -55,7 +57,7 @@ private function startup()
$this->__configure();

if (empty($this->model)) {
$this->model = config('laraman.model_path') . str_singular(str_replace('Controller', '', class_basename($this)));
$this->model = config('laraman.model_path') . Str::of(class_basename($this))->replace('Controller', '')->singular();
}

// turn on/off searching
Expand All @@ -64,11 +66,11 @@ private function startup()
}

if (is_null($this->viewPath)) {
$this->viewPath = config('laraman.view.hintpath') . '::' . strtolower(str_plural(class_basename($this->model)));
$this->viewPath = config('laraman.view.hintpath') . '::' . strtolower(Str::plural(class_basename($this->model)));
}

if (is_null($this->routePath)) {
$this->routePath = config('laraman.route.prefix') . '.' . strtolower(str_plural(class_basename($this->model)));
$this->routePath = config('laraman.route.prefix') . '.' . strtolower(Str::plural(class_basename($this->model)));
// $this->routePath = str_replace('.index', '', Route::currentRouteName());
}

Expand Down Expand Up @@ -124,12 +126,12 @@ public function index(Request $request)

// get the related model fields
$related = $columns->filter(function ($column) {
return str_contains($column['field'], '.');
return Str::of($column['field'])->contains('.');
});

// remove related fields
$fields = $columns->filter(function ($column) {
return !str_contains($column['field'], '.');
return !Str::of($column['field'])->contains('.');
});

// count fields to eager load
Expand Down Expand Up @@ -164,7 +166,7 @@ public function index(Request $request)
// we need a join
$relation = false;

if (str_contains($key, '.')) {
if (Str::of($key)->contains('.')) {
$relation = true;

list($relatedModel, $rest) = explode('.', $key);
Expand All @@ -177,11 +179,11 @@ public function index(Request $request)
// this model
if (!$relation) {
// custom model filter
if (method_exists($builder->getModel(), 'filter' . studly_case($key))) {
$builder = $builder->getModel()->{'filter' . studly_case($key)}($builder, $val);
if (method_exists($builder->getModel(), 'filter' . Str::of($key)->studly())) {
$builder = $builder->getModel()->{'filter' . Str::of($key)->studly()}($builder, $val);
}
// ranges
elseif (str_contains($val, ':')) {
elseif (Str::of($val)->contains(':')) {
list($start, $end) = explode(':', $val);

$start = urldecode($start);
Expand All @@ -207,11 +209,11 @@ public function index(Request $request)
// related model
else {
$builder->whereHas($relatedModel, function ($query) use ($rest, $val, $filter, &$appliedFilters) {
if (method_exists($query->getModel(), 'filter' . studly_case($rest))) {
$query->{'filter' . studly_case($rest)}($query, $val);
if (method_exists($query->getModel(), 'filter' . Str::of($rest)->studly())) {
$query->{'filter' . Str::of($rest)->studly()}($query, $val);
}
// ranges
elseif (str_contains($val, ':')) {
elseif (Str::of($val)->contains(':')) {
list($start, $end) = explode(':', $val);

$start = urldecode($start);
Expand Down Expand Up @@ -245,31 +247,34 @@ public function index(Request $request)
// eager load
foreach ($counts as $count) {
if (method_exists($tmpModel, $count)) {
$builder->with($count);
$builder->withCount($count);

if ($sort === $count) {
$this->dbSortable = true;
}
}
}
$related->each(function ($row) use ($builder) {
if (!method_exists($builder->getModel(), 'filter' . studly_case($row['field']))) {
if (!method_exists($builder->getModel(), 'filter' . Str::of($row['field'])->studly())) {
$pieces = array_slice(explode('.', $row['field']), 0, -1);

$builder->with(implode('.', $pieces));
}
});

// related model, laraman has to do the heavy lifting
if (str_contains($sort, '.')) {
if (Str::of($sort)->contains('.')) {
list($relatedModel, $rest) = explode('.', $sort);

$builder->with($relatedModel);

}

// running a search
if (!empty($search) && $this->searchEnabled) {
$searchBuilder = $model::search($search);

if (is_array($searchBuilder)) {
$ids = array_pluck($searchBuilder['hits'], 'id');
$ids = Arr::pluck($searchBuilder['hits'], 'id');
} else {
$results = $searchBuilder->take($model::count())->get();

Expand All @@ -286,7 +291,12 @@ public function index(Request $request)
}
} else {
if ($this->dbSortable) {
$builder->orderBy($sort, $order);
$sortKey = $sort;
// count sort
if ($counts->contains($sort)) {
$sortKey = $sort . '_count';
}
$builder->orderBy($sortKey, $order);
}
}

Expand All @@ -304,26 +314,15 @@ public function index(Request $request)
$tmpRows = collect([]);

$builder->chunk($this->chunk, function ($rows) use (&$tmpRows, $key, $sort, $counts, $related) {
// sort is on a `count` formatter field
$countSort = false;
if ($counts->contains($sort)) {
$countSort = true;
}

$containsSort = false;
if (str_contains($sort, '.')) {
if (Str::of($sort)->contains('.')) {
$containsSort = true;
}

foreach ($rows as $row) {
$record = [];
$record[$key] = $row->{$key};

// sort is on a `count` formatter field
if ($countSort) {
$record[$sort . 'Count'] = $row->{$sort}->count();
}

$record['_laraman_sort'] = null;

if ($containsSort) {
Expand Down Expand Up @@ -370,25 +369,34 @@ public function index(Request $request)
$rows = $builder->get();
}

$rows = $rows->map(function($row) use ($model, $fields, $related, $location, $buttons) {
$rows = $rows->map(function($row) use ($model, $fields, $related, $location, $buttons, $counts) {
$new = [];

// run the formatters
$fields->each(function ($column) use (&$new, $row, $model) {
$fields->each(function ($column) use (&$new, $row, $model, $counts) {
if (empty($column['formatter'])) {
$new[$column['field']] = $row->{$column['field']};
}
// this will run the row through the accessors
$new[$column['field']] = $row->{$column['field']};
else {
$value = null;
if($counts->contains($column['field'])) {
$value = $row->{$column['field'] . '_count'};
}
elseif (isset($row->{$column['field']})) {
$value = $row->{$column['field']};
}

if (!empty($column['formatter'])) {
$params = [
'value' => $new[$column['field']],
'value' => $value,
'column' => $column,
'row' => $row,
'options' => isset($column['options']) ? $column['options'] : []
];

// formatter is a string
if (is_string($column['formatter'])) {
$new[$column['field']] = $model::{'formatter' . title_case($column['formatter'])}($params);
$new[$column['field']] = $model::{'formatter' . Str::of($column['formatter'])->title()}($params);
}
// formatter is function
else {
Expand All @@ -400,10 +408,10 @@ public function index(Request $request)
// related model fields
$related->each(function ($column) use (&$new, $row, $model) {
// this will run the row through the accessors
$new[$column['field']] = array_get($row, $column['field']);
$new[$column['field']] = Arr::get($row, $column['field']);

if (!empty($column['formatter'])) {
$new[$column['field']] = $model::{'formatter' . title_case($column['formatter'])}([
$new[$column['field']] = $model::{'formatter' . Str::of($column['formatter'])->title()}([
'value' => $new[$column['field']],
'column' => $column,
'row' => $row,
Expand All @@ -428,7 +436,7 @@ public function index(Request $request)
if ($column->field == 'id') {
$column->display = 'ID';
} else {
$column->display = title_case($column->display);
$column->display = Str::of($column->display)->title();
}
}

Expand All @@ -446,7 +454,7 @@ public function index(Request $request)
if ($filter->field == 'id') {
$filter->display = 'ID';
} else {
$filter->display = title_case($filter->field);
$filter->display = Str::of($filter->field)->title();
}
}

Expand All @@ -461,7 +469,6 @@ public function index(Request $request)
if (empty($this->view)) {
$this->view = config('laraman.view.hintpath') . '::index';
}

return view($this->view, [
'paginator' => $paginator,
'rows' => collect($rows),
Expand All @@ -481,7 +488,7 @@ public function index(Request $request)
}

/**
* Show for the recordr
* Show for the record
*
* @param integer $id
*/
Expand Down Expand Up @@ -524,7 +531,7 @@ public function show($id)
$column = ['field' => $column];
}

return str_contains($column['field'], '.');
return Str::of($column['field'])->contains('.');
});

// remove related fields
Expand All @@ -533,7 +540,7 @@ public function show($id)
$column = ['field' => $column];
}

return !str_contains($column['field'], '.');
return !Str::of($column['field'])->contains('.');
});

$buttons = collect(isset($set['buttons']) && is_array($set['buttons']) ? $set['buttons'] : []);
Expand Down Expand Up @@ -561,7 +568,7 @@ public function show($id)

// formatter is a string
if (is_string($column['formatter'])) {
$new[$column['field']] = $model::{'formatter' . title_case($column['formatter'])}($params);
$new[$column['field']] = $model::{'formatter' . Str::of($column['formatter'])->title()}($params);
}
// formatter is function
else {
Expand All @@ -573,10 +580,10 @@ public function show($id)
// related model fields
$relatedFields->each(function ($column) use (&$new, $relatedRow, $model) {
// this will run the row through the accessors
$new[$column['field']] = array_get($relatedRow, $column['field']);
$new[$column['field']] = Arr::get($relatedRow, $column['field']);

if (!empty($column['formatter'])) {
$new[$column['field']] = $model::{'formatter' . title_case($column['formatter'])}([
$new[$column['field']] = $model::{'formatter' . Str::of($column['formatter'])->title()}([
'value' => $new[$column['field']],
'column' => $column,
'row' => $relatedRow,
Expand Down Expand Up @@ -626,7 +633,7 @@ public function show($id)
if ($column->field == 'id') {
$column->display = 'ID';
} else {
$column->display = title_case($column->field);
$column->display = Str::of($column->field)->title();
}
}

Expand Down
Loading

0 comments on commit 9f184ab

Please sign in to comment.