Skip to content
This repository has been archived by the owner on Nov 28, 2019. It is now read-only.

Commit

Permalink
=fixed bug and change many things. inited v:1.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
zareismail committed Jun 10, 2018
1 parent 464c133 commit d44ab14
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 28 deletions.
23 changes: 22 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
Version 1.1.0
Version 1.1.2
-----------------

### Added

* Added FormNotFond Exception for not existence child's.

### Fixed

* Retrieving input by dot notation.
* Improve the name prefix.


### Changed

* Appending rows into field stack by real name without prefix.
* Appending rows into rendered stack by real name without prefix.
* Removed form instance parameters from merge callback method.
* Passing rendered row into internal rendering event.


Version 1.1.1
-----------------

### Added
Expand Down
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
"name": "Esmaiel Zareh",
"email": "[email protected]"
}
],
"version": "1.1.0",
],
"require": {
"laravelcollective/html": "^5.2.0"
},
"autoload": {
"psr-4": {
"Annisa\\Form\\": "form/src/"
"Annisa\\Form\\": "src/"
}
},
"extra": {
Expand Down
74 changes: 50 additions & 24 deletions src/AnnisaBuilder.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Annisa\Form;

use Annisa\Form\Exceptions\FormNotFound;
use Illuminate\Support\HtmlString;
use Annisa\Form\Contracts\Form;
use Closure;

Expand Down Expand Up @@ -289,7 +291,7 @@ public function getChild(string $name)
return $this->childs->get($name);
}

throw new NotExistsForm($name);
throw new FormNotFound("Form {$name} Not Found.");
}

/**
Expand Down Expand Up @@ -358,11 +360,13 @@ protected function fetchData()
{
$validInputs = collect([]);

foreach ($this->rows() as $row) {
if($name = array_get($row, 'name')) {
$validInputs->put($name, $this->getInput($name));
}
}
foreach ($this->rows() as $name => $row) {
$inputName = array_get($row, 'name', $name);

$validInputs->put(
$name, $this->getInput($this->inputKey($inputName))
);
}

$this->arrayTransform($validInputs->toArray());

Expand All @@ -372,6 +376,19 @@ protected function fetchData()
]);
}

/**
* Make dot notaion name for array input.
*
* @param string $name
* @return string
*/
public function inputKey($name)
{
return preg_replace_callback('/\[([^\[\]]*)\]/', function($matches) {
return ".{$matches[1]}";
}, $name);
}

/**
* Getting input from request.
*
Expand Down Expand Up @@ -409,8 +426,8 @@ public function builder($builder = null)
*/
public function merge($rowName, Closure $callback)
{
$this->rows = $this->rows->map(function($row) use ($rowName, $callback) {
return ($rowName == $row['name']) ? $callback($row) : $row;
$this->rows = $this->rows->map(function($row, $name) use ($rowName, $callback) {
return ($rowName == $name) ? $callback($row) : $row;
});

return $this;
Expand All @@ -427,8 +444,10 @@ public function merge($rowName, Closure $callback)
public function element(string $type, string $name)
{
$args = (array) array_except(func_get_args(), [0,1]);
$key = $name;
$name = $this->appendPrefix($name);

$this->rows->put($this->appendPrefix($name), compact('type', 'name') + $args);
$this->rows->put($key, compact('type', 'name') + $args);

return $this;
}
Expand Down Expand Up @@ -461,22 +480,22 @@ public function render($rows = [], $force=false)

$this->runBuilder();

foreach ($this->rows((array) $rows, $force) as $row) {
foreach ($this->rows((array) $rows, $force) as $name => $row) {

$this->event('row.rendering', $row);

echo $this->toHtml($row);
$rendered = $this->toHtml($row);

$this->event('row.rendered', $row);
$this->event('row.rendered', $rendered = $this->toHtml($row));

$this->renderedRows($row['name']);
}
echo $rendered;

$this->childs()->each(function ($child) use ($rows) {
return empty($rows)? $child->render() : false;
});
$this->renderedRows($name);
}

return '';
return (
new HtmlString(empty($rows)? '' : $this->childs()->implode(''))
)->toHtml();
}

/**
Expand Down Expand Up @@ -545,12 +564,12 @@ public function rows($name = [], $force=false)
{
$names = collect((array) $name)->flip();

return $this->rows->filter(function($row) use ($names, $force) {
if($names->count() && !$names->has($row['name'])) {
return $this->rows->filter(function($row, $name) use ($names, $force) {
if($names->count() && !$names->has($name)) {
return false;
}

return $force || !$this->isRendered($row['name']);
return $force || !$this->isRendered($name);
});
}

Expand All @@ -576,7 +595,7 @@ protected function event($event, $row = null)
{
foreach ((array) $this->events->get($event) as $callback) {
if(is_callable($callback)) {
call_user_func_array($callback, [$this, $row]);
$callback($row);
}
}
}
Expand Down Expand Up @@ -620,9 +639,16 @@ public function toHtml($row)
*/
protected function appendPrefix(string $name)
{
$trimmed = rtrim($name, ']');
if(! empty($this->prefix)) {

preg_match_all('/[^\[\]]+/', $name, $matches);

$wrraped = implode($matches[0], '][');

return "{$this->prefix}[{$wrraped}]";
}

return isset($this->prefix)? "{$this->prefix}[$trimmed]" : $name;
return $name;
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/Exceptions/FormNotFound.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
namespace Annisa\Form\Exceptions;

use Exception;

class FormNotFound extends Exception
{
}

0 comments on commit d44ab14

Please sign in to comment.