Skip to content

Commit

Permalink
Set layout
Browse files Browse the repository at this point in the history
  • Loading branch information
ngtfkx committed Aug 30, 2017
1 parent ac6ac21 commit 759acc2
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 17 deletions.
22 changes: 22 additions & 0 deletions src/Elements/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Ngtfkx\Laradeck\FormBuilder\Elements;


use Ngtfkx\Laradeck\FormBuilder\Layouts\AbstractLayout;

class Form extends AbstractElement
{
/**
Expand Down Expand Up @@ -35,6 +37,22 @@ class Form extends AbstractElement
*/
protected $enctype;

/**
* @var AbstractLayout
*/
protected $layout;

/**
* Form constructor.
* @param AbstractLayout $layout
*/
public function __construct(?AbstractLayout $layout = null)
{
parent::__construct();

$this->layout = $layout;
}

/**
* @param string|null $value
* @return Form
Expand Down Expand Up @@ -146,6 +164,10 @@ public function tag(): void

public function __toString(): string
{
if ($this->layout) {
$this->class($this->layout->getFormClasses());
}

$this->attrs([
'action' => $this->action,
'method' => $this->method,
Expand Down
27 changes: 10 additions & 17 deletions src/FormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@


use Ngtfkx\Laradeck\FormBuilder\Elements;
use Ngtfkx\Laradeck\FormBuilder\Layouts\AbstractLayout;

class FormBuilder
{
Expand All @@ -17,17 +18,7 @@ class FormBuilder
*/
protected $form;

/**
* Тип верстки. Допустимые значения: html, bootstrap3, bootstrap4
* @var string
*/
protected $cssFramework = 'html';

/**
* Тип верстки формы
* @var string
*/
protected $formLayout;
protected $layout;

/**
* FormBuilder constructor.
Expand All @@ -41,16 +32,18 @@ public function __construct()
* Сеттер для шаблона верстки (тип фремворка и тип расположения формы)
*
* @param string $cssFramework
* @param null|string $formLayout
* @param null|string $orientation
* @return FormBuilder
*/
public function layout(string $cssFramework, ?string $formLayout = null): FormBuilder
public function layout(string $cssFramework, ?string $orientation = null): FormBuilder
{
$this->cssFramework = $cssFramework;
$className = "\\Ngtfkx\\Laradeck\\FormBuilder\\Layouts\\" . studly_case($cssFramework);

$this->formLayout = $formLayout;
if(class_exists($className)) {
$this->layout = (new $className)->orientation($orientation);
}

// TODO: надо вешать стили на тег формы в зависимости от переданных данных
return $this;
}

/**
Expand All @@ -62,7 +55,7 @@ public function layout(string $cssFramework, ?string $formLayout = null): FormBu
*/
public function open(?string $action = '', ?string $method = 'get'): Elements\Form
{
$this->form = (new Elements\Form())->action($action)->method($method);
$this->form = (new Elements\Form($this->layout))->action($action)->method($method);

return $this->form;
}
Expand Down
50 changes: 50 additions & 0 deletions src/Layouts/AbstractLayout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Ngtfkx\Laradeck\FormBuilder\Layouts;


abstract class AbstractLayout
{
/**
* @var string $cssFramework Тип верстки. Допустимые значения: html, bootstrap3, bootstrap4
*/
protected $cssFramework;

/**
* @var string $orientation Тип верстки формы
*/
protected $orientation;

/**
* @var string $formClasses Классы, которые надо назначить на форму
*/
protected $formClasses = '';

abstract protected function setFormClasses();

/**
* AbstractLayout constructor.
* @param string $cssFramework
* @param string $orientation
*/
public function __construct(string $cssFramework = 'html', ?string $orientation = null)
{
$this->cssFramework = $cssFramework;
$this->orientation($orientation);
}

public function orientation(?string $orientation = null): self
{
$this->orientation = $orientation;

$this->setFormClasses();

return $this;
}

public function getFormClasses(): string
{
return $this->formClasses;
}

}
24 changes: 24 additions & 0 deletions src/Layouts/Bootstrap3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Ngtfkx\Laradeck\FormBuilder\Layouts;


class Bootstrap3 extends AbstractLayout
{
protected $cssFramework = 'bootstrap3';

protected function setFormClasses()
{
switch ($this->orientation) {
case 'inline':
$this->formClasses = 'form-inline';
break;
case 'horizontal':
$this->formClasses = 'form-horizontal';
break;
default:
break;

}
}
}
13 changes: 13 additions & 0 deletions src/Layouts/Html.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Ngtfkx\Laradeck\FormBuilder\Layouts;


class Html extends AbstractLayout
{
protected $cssFramework = 'html';

protected function setFormClasses()
{
}
}
15 changes: 15 additions & 0 deletions tests/Layouts/Bootstrap3Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Ngtfkx\Laradeck\FormBuilder\Tests;

use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class Bootstrap3Test extends TestCase
{
public function testFormClasses()
{
$this->assertContains(' class="form-inline"', '' . fb()->layout('bootstrap3', 'inline')->open());
}
}

0 comments on commit 759acc2

Please sign in to comment.