Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaneGlee committed Mar 20, 2019
0 parents commit bca6084
Show file tree
Hide file tree
Showing 13 changed files with 623 additions and 0 deletions.
19 changes: 19 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "focalstrategy/core",
"description": "Core Library for Focal Strategy's components",
"type": "library",
"authors": [
{
"name": "Shane Glee",
"email": "[email protected]"
}
],
"require": {
"myclabs/php-enum": "^1.5"
},
"autoload": {
"psr-4": {
"FocalStrategy\\Core\\": "src"
}
}
}
10 changes: 10 additions & 0 deletions src/BaseBgType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace FocalStrategy\Core;

use MyCLabs\Enum\Enum;

class BaseBgType extends Enum
{
const NONE = 'none';
}
10 changes: 10 additions & 0 deletions src/BaseBtnType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace FocalStrategy\Core;

use MyCLabs\Enum\Enum;

class BaseBtnType extends Enum
{
const NONE = 'btn-default';
}
131 changes: 131 additions & 0 deletions src/Buttons/Button.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

namespace FocalStrategy\Core\Buttons;

use FocalStrategy\Core\ReceivesData;
use FocalStrategy\Core\BaseBtnType;
use FocalStrategy\Core\Renderable;
use View;

class Button implements Renderable, ReceivesData
{
protected $text;
protected $action_type;
protected $url_template;
protected $data_attributes = [];
protected $attributes = [
'class' => 'btn '
];

public function __construct($text, $url, BaseBtnType $action_type)
{
$this->text = $text;

if ($url != null) {
$this->attributes['href'] = $url;
}

$this->attributes['class'] .= $action_type;
}

public function addDataAttributes(array $data_attributes) : Button
{
$this->data_attributes = $data_attributes;
return $this;
}

public function mergeAttributes(array $attributes) : Button
{
foreach ($attributes as $key => $a) {
if (isset($this->attributes[$key])) {
$this->attributes[$key] .= ' '.$a;
} else {
$this->attributes[$key] = $a;
}
}

return $this;
}

public function getText() : string
{
return $this->text;
}

public function isButton() : bool
{
return !isset($this->attributes['href']);
}

public function getRoute() : string
{
return $this->attributes['href'] ?? '';
}

public function attributes() : string
{
$result = '';
foreach ($this->attributes as $key => $attr) {
$result .= ' '.$key.'="'.$attr.'"';
}

return $result;
}

public function getDataAttributes() : string
{
$result = '';
foreach ($this->data_attributes as $key => $attr) {
$result .= ' '.$key.'="'.$attr.'"';
}

return $result;
}

public function setTarget($target) : Button
{
$this->attributes['target'] = $target;

return $this;
}

public function setUrlTemplate(string $template) : Button
{
$this->url_template = $template;

return $this;
}

public function addData(array $data)
{
if ($this->url_template) {
$url = $this->url_template;
foreach ($data as $key => $value) {
$url = str_replace('+'.$key.'+', $value, $url);
$url = str_replace('%2B'.$key.'%2B', $value, $url);
}
$this->attributes['href'] = $url;
}

if ($this->text) {
$text = $this->text;
foreach ($data as $key => $value) {
if (!is_array($value)) {
$text = str_replace('+'.$key.'+', $value, $text);
$text = str_replace('%2B'.$key.'%2B', $value, $text);
}
}
$this->text = $text;
}
}

public function hasAttribute($key)
{
return isset($this->attributes[$key]);
}

public function render()
{
return view('_components.button')->with('button', $this);
}
}
73 changes: 73 additions & 0 deletions src/Buttons/ButtonGroupDropdown.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace FocalStrategy\Core\Buttons;

use FocalStrategy\Core\BaseBtnType;
use View;

class ButtonGroupDropdown implements Renderable
{
protected $text;
protected $action_type;
protected $attributes = [
'class' => 'btn '
];

public function __construct($text, $url, BaseBtnType $action_type, $buttons)
{
$this->text = $text;

if ($url != null) {
$this->attributes['href'] = $url;
}

$this->attributes['class'] .= $action_type;
$this->buttons = $buttons;
}

public function mergeAttributes(array $attributes)
{
foreach ($attributes as $key => $a) {
if (isset($this->attributes[$key])) {
$this->attributes[$key] .= ' '.$a;
} else {
$this->attributes[$key] = $a;
}
}
}

public function getText()
{
return $this->text;
}

public function isButton()
{
return !isset($this->attributes['href']);
}

public function getRoute()
{
return $this->attributes['href'] ?? '';
}

public function attributes()
{
$result = '';
foreach ($this->attributes as $key => $attr) {
$result .= ' '.$key.'="'.$attr.'"';
}

return $result;
}

public function setTarget($target)
{
$this->attributes['target'] = $target;
}

public function render()
{
return view('_components.button_group_dropdown')->with('button', $this);
}
}
26 changes: 26 additions & 0 deletions src/Buttons/ImageButton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace FocalStrategy\Core\Buttons;

use FocalStrategy\Core\BaseBtnType;

class ImageButton extends Button
{
protected $img_url;

public function __construct($img_url, $route)
{
parent::__construct('', $route, BaseBtnType::NONE());
$this->img_url = $img_url;
}

public function render()
{
return view('_components.image_button')->with('button', $this);
}

public function getImage()
{
return $this->img_url;
}
}
26 changes: 26 additions & 0 deletions src/Buttons/PostButton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace FocalStrategy\Core\Buttons;

use FocalStrategy\Core\BaseBtnType;

class PostButton extends Button
{
protected $data;

public function __construct($text, $action, $data, BaseBtnType $btn_type)
{
parent::__construct($text, $action, $btn_type);
$this->data = $data;
}

public function render()
{
return view('_components.post_button')->with('button', $this);
}

public function data()
{
return $this->data;
}
}
30 changes: 30 additions & 0 deletions src/CoreServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace FocalStrategy\Core;

use Illuminate\Support\ServiceProvider;

class CoreServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
}

/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
$this->loadViewsFrom(__DIR__.'/views', 'core');
$this->publishes([
__DIR__.'/views' => resource_path('views/vendor/core'),
]);
}
}
Loading

0 comments on commit bca6084

Please sign in to comment.