-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit 6196390
Showing
11 changed files
with
336 additions
and
0 deletions.
There are no files selected for viewing
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 @@ | ||
.DS_Store | ||
.idea | ||
/vendor | ||
/build | ||
.phpunit.result.cache | ||
composer.lock |
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,28 @@ | ||
# Options | ||
|
||
Class-based select options for Laravel | ||
|
||
## Installation | ||
|
||
```$xslt | ||
composer require binarycabin/options | ||
``` | ||
|
||
## Generate Permissions | ||
|
||
```$xslt | ||
php artisan make:option CLASSNAME | ||
``` | ||
|
||
ie: | ||
|
||
```$xslt | ||
php artisan make:option County | ||
``` | ||
|
||
## Display the option | ||
|
||
```$xslt | ||
$field->options(\App\Options\County::get('---')) !!} | ||
``` | ||
|
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,33 @@ | ||
{ | ||
"name": "binarycabin/options", | ||
"description": "Class-based select options for Laravel", | ||
"type": "library", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Jeff Kilroy", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": {}, | ||
"autoload":{ | ||
"psr-4": { | ||
"BinaryCabin\\Options\\": "src" | ||
} | ||
}, | ||
"autoload-dev":{ | ||
"psr-4": { | ||
"BinaryCabin\\Options\\Tests\\": "tests" | ||
} | ||
}, | ||
"extra": { | ||
"laravel": { | ||
"providers": [ | ||
"BinaryCabin\\Options\\Providers\\OptionServiceProvider" | ||
] | ||
} | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^8.0" | ||
} | ||
} |
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,25 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit backupGlobals="false" | ||
backupStaticAttributes="false" | ||
bootstrap="vendor/autoload.php" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false" | ||
> | ||
<testsuites> | ||
<testsuite name="Package Test Suite"> | ||
<directory suffix=".php">./tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<whitelist> | ||
<directory suffix=".php">src/</directory> | ||
</whitelist> | ||
</filter> | ||
<logging> | ||
<log type="coverage-text" target="build/coverage.txt" /> | ||
</logging> | ||
</phpunit> |
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,58 @@ | ||
<?php | ||
|
||
namespace BinaryCabin\Options; | ||
|
||
abstract class BaseOption{ | ||
|
||
protected $attributes=[]; | ||
|
||
public static function get($blank=false, $attributes=[]){ | ||
$optionClass = new static(); | ||
$optionClass->attributes = $attributes; | ||
$array = $optionClass->getArray(); | ||
$response = []; | ||
if(!empty($blank)){ | ||
$response[''] = $blank; | ||
} | ||
foreach($array as $key => $arrayItem){ | ||
$response[$key] = $arrayItem; | ||
} | ||
return $response; | ||
} | ||
|
||
public static function checkboxes($attributes=[]){ | ||
$optionClass = new static(); | ||
$optionClass->attributes = $attributes; | ||
$name = ''; | ||
if(!empty($attributes['name'])){ | ||
$name = $attributes['name']; | ||
} | ||
$checkedKeys = []; | ||
if(!empty($attributes['checked'])){ | ||
$checkedKeys = $attributes['checked']; | ||
} | ||
$array = $optionClass->getArray(); | ||
$response = []; | ||
foreach($array as $key => $label){ | ||
$checked = false; | ||
if(in_array($key,$checkedKeys)){ | ||
$checked = true; | ||
} | ||
$response[$label] = [ | ||
'name' => $name, | ||
'checked' => $checked, | ||
'value' => $key, | ||
]; | ||
} | ||
return $response; | ||
} | ||
|
||
public function keysAsValues($array = []){ | ||
return array_combine($array, $array); | ||
} | ||
|
||
public function getArray(){ | ||
return []; | ||
} | ||
|
||
} |
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,45 @@ | ||
<?php | ||
|
||
namespace BinaryCabin\Options\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
|
||
class MakeOption extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'make:option {className}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Make a new option class'; | ||
|
||
/** | ||
* Create a new command instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return mixed | ||
*/ | ||
public function handle() | ||
{ | ||
$creator = new \BinaryCabin\Options\Creator\OptionCreator(); | ||
$creator->setInterface($this); | ||
$creator->setClassName($this->argument('className')); | ||
$creator->create(); | ||
} | ||
} |
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,13 @@ | ||
<?php | ||
|
||
namespace BinaryCabin\Options\Configuration; | ||
|
||
class OptionConfig{ | ||
|
||
private $optionAppDirectory = '/Options'; | ||
|
||
public function getOptionAppDirectory(){ | ||
return $this->optionAppDirectory; | ||
} | ||
|
||
} |
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,49 @@ | ||
<?php | ||
|
||
namespace BinaryCabin\Options\Creator; | ||
|
||
class OptionCreator{ | ||
|
||
protected $interface; | ||
protected $className; | ||
protected $config; | ||
|
||
public function __construct(){ | ||
$this->config = new \BinaryCabin\Options\Configuration\OptionConfig(); | ||
} | ||
|
||
public function setInterface($interface){ | ||
$this->interface = $interface; | ||
} | ||
|
||
public function setClassName($className){ | ||
$this->className = $className; | ||
} | ||
|
||
public function createOptionsFile(){ | ||
$this->interface->info('Creating Options File'); | ||
$putFilePath = app_path().$this->config->getOptionAppDirectory().'/'.$this->className.'.php'; | ||
if(file_exists($putFilePath)){ | ||
$this->interface->error('File Already Exists: '.$putFilePath); | ||
}else{ | ||
$templateFilePath = __DIR__.'/Templates/OptionClass.php.txt'; | ||
$templateFile = file_get_contents($templateFilePath); | ||
$fileContents = str_replace('[CLASSNAME]',$this->className,$templateFile); | ||
$this->prepareOptionDirectory(); | ||
\File::put($putFilePath,$fileContents); | ||
} | ||
} | ||
|
||
public function prepareOptionDirectory(){ | ||
$directory = $this->config->getOptionAppDirectory(); | ||
if (!is_dir(app_path().$directory)) { | ||
mkdir(app_path().$directory,0777, true); | ||
} | ||
} | ||
|
||
public function create(){ | ||
$this->createOptionsFile(); | ||
$this->interface->info('Done!'); | ||
} | ||
|
||
} |
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,13 @@ | ||
<?php | ||
|
||
namespace App\Options; | ||
|
||
use BinaryCabin\Options\BaseOption; | ||
|
||
class [CLASSNAME] extends BaseOption { | ||
|
||
public function getArray(){ | ||
return []; | ||
} | ||
|
||
} |
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,35 @@ | ||
<?php | ||
namespace BinaryCabin\Options\Providers; | ||
|
||
use Illuminate\Support\ServiceProvider; | ||
|
||
class OptionServiceProvider extends ServiceProvider | ||
{ | ||
/** | ||
* Bootstrap the application services. | ||
* | ||
* @return void | ||
*/ | ||
public function boot() | ||
{ | ||
if ($this->app->runningInConsole()) { | ||
$this->commands([ | ||
\BinaryCabin\Options\Commands\MakeOption::class, | ||
]); | ||
} | ||
// | ||
$this->publishes([ | ||
__DIR__.'/../Configuration/Templates/options.php' => config_path('options.php') | ||
], 'config'); | ||
} | ||
/** | ||
* Register the application services. | ||
* | ||
* @return void | ||
*/ | ||
public function register() | ||
{ | ||
// | ||
} | ||
|
||
} |
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,31 @@ | ||
<?php | ||
|
||
namespace BinaryCabin\Tests; | ||
|
||
use BinaryCabin\Options\BaseOption; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class OptionTest extends TestCase{ | ||
|
||
public function test_it_returns_an_array_of_items(){ | ||
$items = ExampleItemOptions::get(); | ||
$this->assertEquals(2, count($items)); | ||
} | ||
|
||
public function test_it_returns_a_blank_entry(){ | ||
$items = ExampleItemOptions::get('---'); | ||
$this->assertEquals(3, count($items)); | ||
} | ||
|
||
} | ||
|
||
class ExampleItemOptions extends BaseOption{ | ||
|
||
public function getArray(){ | ||
return [ | ||
'item-1', | ||
'item-2', | ||
]; | ||
} | ||
|
||
} |