Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
kilrizzy committed Mar 5, 2019
0 parents commit 6196390
Show file tree
Hide file tree
Showing 11 changed files with 336 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.DS_Store
.idea
/vendor
/build
.phpunit.result.cache
composer.lock
28 changes: 28 additions & 0 deletions README.md
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('---')) !!}
```

33 changes: 33 additions & 0 deletions composer.json
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"
}
}
25 changes: 25 additions & 0 deletions phpunit.xml
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>
58 changes: 58 additions & 0 deletions src/BaseOption.php
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 [];
}

}
45 changes: 45 additions & 0 deletions src/Commands/MakeOption.php
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();
}
}
13 changes: 13 additions & 0 deletions src/Configuration/OptionConfig.php
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;
}

}
49 changes: 49 additions & 0 deletions src/Creator/OptionCreator.php
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!');
}

}
13 changes: 13 additions & 0 deletions src/Creator/Templates/OptionClass.php.txt
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 [];
}

}
35 changes: 35 additions & 0 deletions src/Providers/OptionServiceProvider.php
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()
{
//
}

}
31 changes: 31 additions & 0 deletions tests/OptionTest.php
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',
];
}

}

0 comments on commit 6196390

Please sign in to comment.