Skip to content
This repository was archived by the owner on Feb 26, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/AdamWathan/Form/Binding/BoundData.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,59 @@

namespace AdamWathan\Form\Binding;

/**
* Class BoundData
* @package AdamWathan\Form\Binding
*/
class BoundData
{
protected $data;

/**
* BoundData constructor.
* @param $data
*/
public function __construct($data)
{
$this->data = $data;
}

/**
* @param $name
* @param null $default
* @return mixed
*/
public function get($name, $default = null)
{
return $this->dotGet($this->transformKey($name), $default);
}

/**
* @return mixed
*/
public function data()
{
return $this->data;
}

/**
* @param $dotKey
* @param $default
* @return mixed
*/
protected function dotGet($dotKey, $default)
{
$keyParts = explode('.', $dotKey);

return $this->dataGet($this->data, $keyParts, $default);
}

/**
* @param $target
* @param $keyParts
* @param $default
* @return mixed
*/
protected function dataGet($target, $keyParts, $default)
{
if (count($keyParts) == 0) {
Expand All @@ -45,6 +72,12 @@ protected function dataGet($target, $keyParts, $default)
return $default;
}

/**
* @param $target
* @param $keyParts
* @param $default
* @return mixed
*/
protected function arrayGet($target, $keyParts, $default)
{
$key = array_shift($keyParts);
Expand All @@ -56,6 +89,12 @@ protected function arrayGet($target, $keyParts, $default)
return $this->dataGet($target[$key], $keyParts, $default);
}

/**
* @param $target
* @param $keyParts
* @param $default
* @return mixed
*/
protected function objectGet($target, $keyParts, $default)
{
$key = array_shift($keyParts);
Expand All @@ -67,6 +106,10 @@ protected function objectGet($target, $keyParts, $default)
return $this->dataGet($target->{$key}, $keyParts, $default);
}

/**
* @param $key
* @return mixed
*/
protected function transformKey($key)
{
return str_replace(['[]', '[', ']'], ['', '.', ''], $key);
Expand Down
17 changes: 16 additions & 1 deletion src/AdamWathan/Form/Elements/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace AdamWathan\Form\Elements;

/**
* Class Button
* @package AdamWathan\Form\Elements
*/
class Button extends FormControl
{
protected $attributes = [
Expand All @@ -10,18 +14,29 @@ class Button extends FormControl

protected $value;

public function __construct($value, $name = null)
/**
* Button constructor.
* @param string $value
* @param null $name
*/
public function __construct($value, $name = null)
{
parent::__construct($name);

$this->value($value);
}

/**
* @return string
*/
public function render()
{
return sprintf('<button%s>%s</button>', $this->renderAttributes(), $this->value);
}

/**
* @param $value
*/
public function value($value)
{
$this->value = $value;
Expand Down
37 changes: 37 additions & 0 deletions src/AdamWathan/Form/Elements/Checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace AdamWathan\Form\Elements;

/**
* Class Checkbox
* @package AdamWathan\Form\Elements
*/
class Checkbox extends Input
{
protected $attributes = [
Expand All @@ -12,13 +16,21 @@ class Checkbox extends Input

protected $oldValue;

/**
* Checkbox constructor.
* @param $name
* @param int $value
*/
public function __construct($name, $value = 1)
{
parent::__construct($name);

$this->setValue($value);
}

/**
* @param $oldValue
*/
public function setOldValue($oldValue)
{
$this->oldValue = $oldValue;
Expand All @@ -29,6 +41,9 @@ public function unsetOldValue()
$this->oldValue = null;
}

/**
* @return $this
*/
public function defaultToChecked()
{
if (! isset($this->checked) && is_null($this->oldValue)) {
Expand All @@ -38,6 +53,9 @@ public function defaultToChecked()
return $this;
}

/**
* @return $this
*/
public function defaultToUnchecked()
{
if (! isset($this->checked) && is_null($this->oldValue)) {
Expand All @@ -47,13 +65,20 @@ public function defaultToUnchecked()
return $this;
}

/**
* @param $state
* @return $this
*/
public function defaultCheckedState($state)
{
$state ? $this->defaultToChecked() : $this->defaultToUnchecked();

return $this;
}

/**
* @return $this
*/
public function check()
{
$this->unsetOldValue();
Expand All @@ -62,6 +87,9 @@ public function check()
return $this;
}

/**
* @return $this
*/
public function uncheck()
{
$this->unsetOldValue();
Expand All @@ -70,6 +98,9 @@ public function uncheck()
return $this;
}

/**
* @param bool $checked
*/
protected function setChecked($checked = true)
{
$this->checked = $checked;
Expand All @@ -80,6 +111,9 @@ protected function setChecked($checked = true)
}
}

/**
* @return Checkbox
*/
protected function checkBinding()
{
$currentValue = (string) $this->getAttribute('value');
Expand All @@ -93,6 +127,9 @@ protected function checkBinding()
}
}

/**
* @return string
*/
public function render()
{
$this->checkBinding();
Expand Down
10 changes: 9 additions & 1 deletion src/AdamWathan/Form/Elements/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@

namespace AdamWathan\Form\Elements;

/**
* Class Date
* @package AdamWathan\Form\Elements
*/
class Date extends Text
{
protected $attributes = [
'type' => 'date',
];

public function value($value)
/**
* @param $value
* @return $this
*/
public function value($value)
{
if ($value instanceof \DateTime) {
$value = $value->format('Y-m-d');
Expand Down
Loading