-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
445 additions
and
53 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/**! | ||
* Sortable 1.14.0 | ||
* @author RubaXa <[email protected]> | ||
* @author owenm <[email protected]> | ||
* @license MIT | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
namespace SimonHamp\LaravelNovaCsvImport\Concerns; | ||
|
||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Str; | ||
use SimonHamp\LaravelNovaCsvImport\Contracts\Modifier; | ||
use SimonHamp\LaravelNovaCsvImport\Modifiers\Boolean; | ||
use SimonHamp\LaravelNovaCsvImport\Modifiers\ExcelDate; | ||
use SimonHamp\LaravelNovaCsvImport\Modifiers\Str as StrModifier; | ||
use SimonHamp\LaravelNovaCsvImport\Modifiers\Hash; | ||
|
||
trait HasModifiers | ||
{ | ||
protected $modifiers = []; | ||
|
||
protected static $registered_modifiers = []; | ||
|
||
protected function bootHasModifiers() | ||
{ | ||
// Register built-in modifiers | ||
static::registerModifiers( | ||
new Boolean, | ||
new ExcelDate, | ||
new StrModifier, | ||
new Hash, | ||
); | ||
} | ||
|
||
public static function registerModifiers(Modifier ...$modifiers) | ||
{ | ||
foreach ($modifiers as $modifier) { | ||
$name = Str::snake(class_basename($modifier)); | ||
|
||
static::$registered_modifiers[$name] = $modifier; | ||
} | ||
} | ||
|
||
public function getAvailableModifiers() | ||
{ | ||
return collect(static::$registered_modifiers) | ||
->map(function (Modifier $modifier, $key) { | ||
return [ | ||
'name' => $key, | ||
'title' => $modifier->title(), | ||
'description' => $modifier->description(), | ||
'settings' => $this->formatModifierSettings($modifier->settings()), | ||
]; | ||
}) | ||
->keyBy('name'); | ||
} | ||
|
||
public function getModifiers($key = null): array | ||
{ | ||
if ($key) { | ||
return $this->modifiers[$key] ?? []; | ||
} | ||
|
||
return $this->modifiers; | ||
} | ||
|
||
public function setModifiers(array $map): self | ||
{ | ||
$this->modifiers = $map; | ||
|
||
return $this; | ||
} | ||
|
||
protected function modifyValue($value = null, array $modifiers = []) | ||
{ | ||
foreach ($modifiers as $modifier) { | ||
$instance = static::$registered_modifiers[$modifier['name']]; | ||
|
||
$value = $instance->handle($value, $modifier['settings'] ?? []); | ||
} | ||
|
||
|
||
return $value; | ||
} | ||
|
||
protected function formatModifierSettings(array $settings = []) | ||
{ | ||
$normalised_settings = []; | ||
foreach ($settings as $name => $setting) { | ||
$normalised = [ | ||
'title' => $setting['title'] ?? Str::title($name), | ||
'type' => is_string($setting) ? 'string' : $setting['type'] ?? 'select', | ||
'default' => $setting['default'] ?? '', | ||
]; | ||
|
||
if ($normalised['type'] === 'select') { | ||
$options = $setting['options'] ?? $setting; | ||
|
||
$normalised['options'] = Arr::isAssoc($options) ? $options : array_combine($options, $options); | ||
} | ||
|
||
$normalised_settings[$name] = $normalised; | ||
} | ||
|
||
return $normalised_settings; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace SimonHamp\LaravelNovaCsvImport\Contracts; | ||
|
||
interface Modifier | ||
{ | ||
public function title(): string; | ||
|
||
public function description(): string; | ||
|
||
public function settings(): array; | ||
|
||
public function handle($value = null, array $settings = []); | ||
} |
Oops, something went wrong.