-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add exclude functionality to addFilter() method
- Loading branch information
1 parent
33c29d8
commit de184bc
Showing
44 changed files
with
185 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
* @author Nick Sagona, III <[email protected]> | ||
* @copyright Copyright (c) 2009-2016 NOLA Interactive, LLC. (http://www.nolainteractive.com) | ||
* @license http://www.popphp.org/license New BSD License | ||
* @version 2.0.1 | ||
* @version 2.0.2 | ||
*/ | ||
abstract class AbstractForm extends Child implements \ArrayAccess | ||
{ | ||
|
@@ -46,6 +46,18 @@ abstract class AbstractForm extends Child implements \ArrayAccess | |
*/ | ||
protected $filters = []; | ||
|
||
/** | ||
* Fields to exclude filtering by type | ||
* @var array | ||
*/ | ||
protected $filterExcludeByType = []; | ||
|
||
/** | ||
* Fields to exclude filtering by name | ||
* @var array | ||
*/ | ||
protected $filterExcludeByName = []; | ||
|
||
/** | ||
* Form field groups | ||
* @var array | ||
|
@@ -129,14 +141,41 @@ public function setMethod($method) | |
* | ||
* @param mixed $call | ||
* @param mixed $params | ||
* @param mixed $excludeByType | ||
* @param mixed $excludeByName | ||
* @return AbstractForm | ||
*/ | ||
public function addFilter($call, $params = null) | ||
public function addFilter($call, $params = null, $excludeByType = null, $excludeByName = null) | ||
{ | ||
$this->filters[] = [ | ||
'call' => $call, | ||
'params' => $params | ||
]; | ||
|
||
if (null !== $excludeByType) { | ||
if (!is_array($excludeByType)) { | ||
$excludeByType = [$excludeByType]; | ||
} | ||
foreach ($excludeByType as $type) { | ||
if (!isset($this->filterExcludeByType[$type])) { | ||
$this->filterExcludeByType[$type] = []; | ||
} | ||
$this->filterExcludeByType[$type][] = $call; | ||
} | ||
} | ||
|
||
if (null !== $excludeByName) { | ||
if (!is_array($excludeByName)) { | ||
$excludeByName = [$excludeByName]; | ||
} | ||
foreach ($excludeByName as $name) { | ||
if (!isset($this->filterExcludeByName[$name])) { | ||
$this->filterExcludeByName[$name] = []; | ||
} | ||
$this->filterExcludeByName[$name][] = $call; | ||
} | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
|
@@ -145,16 +184,18 @@ public function addFilter($call, $params = null) | |
* | ||
* @param array $filters | ||
* @throws Exception | ||
* @param mixed $excludeByType | ||
* @param mixed $excludeByName | ||
* @return AbstractForm | ||
*/ | ||
public function addFilters(array $filters) | ||
public function addFilters(array $filters, $excludeByType = null, $excludeByName = null) | ||
{ | ||
foreach ($filters as $filter) { | ||
if (!isset($filter['call'])) { | ||
throw new Exception('Error: The \'call\' key must be set.'); | ||
} | ||
$params = (isset($filter['params'])) ? $filter['params'] : null; | ||
$this->addFilter($filter['call'], $params); | ||
$this->addFilter($filter['call'], $params, $excludeByType, $excludeByName); | ||
} | ||
return $this; | ||
} | ||
|
@@ -220,6 +261,92 @@ public function getFields() | |
return $this->fields; | ||
} | ||
|
||
/** | ||
* Alias method to getElements()) | ||
* | ||
* @return array | ||
*/ | ||
public function elements() | ||
{ | ||
return $this->getElements(); | ||
} | ||
|
||
/** | ||
* Get the elements of the form object. | ||
* | ||
* @return array | ||
*/ | ||
public function getElements() | ||
{ | ||
$children = $this->getChildren(); | ||
$elements = []; | ||
|
||
foreach ($children as $child) { | ||
if ($child instanceof Element\AbstractElement){ | ||
$elements[] = $child; | ||
} | ||
} | ||
|
||
return $elements; | ||
} | ||
|
||
/** | ||
* Alias method to getElement() | ||
* | ||
* @param string $elementName | ||
* @return Element\AbstractElement | ||
*/ | ||
public function element($elementName) | ||
{ | ||
return $this->getElement($elementName); | ||
} | ||
|
||
/** | ||
* Get an element object of the form by name. | ||
* | ||
* @param string $elementName | ||
* @return Element\AbstractElement | ||
*/ | ||
public function getElement($elementName) | ||
{ | ||
$i = $this->getElementIndex($elementName); | ||
return (null !== $i) ? $this->getChild($this->getElementIndex($elementName)) : null; | ||
} | ||
|
||
/** | ||
* Get the index of an element object of the form by name. | ||
* | ||
* @param string $elementName | ||
* @return int | ||
*/ | ||
public function getElementIndex($elementName) | ||
{ | ||
$name = null; | ||
$elem = null; | ||
$index = null; | ||
$elems = $this->getChildren(); | ||
|
||
foreach ($elems as $i => $e) { | ||
if ($e->getNodeName() == 'fieldset') { | ||
$children = $e->getChildren(); | ||
foreach ($children as $c) { | ||
if ($c->getNodeName() == 'input') { | ||
$attribs = $c->getAttributes(); | ||
$name = str_replace('[]', '', $attribs['name']); | ||
} | ||
} | ||
} else { | ||
$attribs = $e->getAttributes(); | ||
$name = $attribs['name']; | ||
} | ||
if ($name == $elementName) { | ||
$index = $i; | ||
} | ||
} | ||
|
||
return $index; | ||
} | ||
|
||
/** | ||
* Get fieldConfig | ||
* | ||
|
@@ -316,8 +443,17 @@ protected function filterValues(array $values = null) | |
protected function applyFilter(&$array, $call, $params = []) | ||
{ | ||
array_walk_recursive($array, function(&$value, $key, $userdata) { | ||
$params = array_merge([$value], $userdata[1]); | ||
$value = call_user_func_array($userdata[0], $params); | ||
$element = $this->getElement($key); | ||
$type = ((null !== $element) && ($element instanceof \Pop\Form\Element\AbstractElement)) ? | ||
$element->getType() : -1; | ||
|
||
if (((!isset($this->filterExcludeByType[$type])) || | ||
(isset($this->filterExcludeByType[$type]) && !in_array($userdata[0], $this->filterExcludeByType[$type]))) && | ||
((!isset($this->filterExcludeByName[$key])) || | ||
(isset($this->filterExcludeByName[$key]) && !in_array($userdata[0], $this->filterExcludeByName[$key])))) { | ||
$params = array_merge([$value], $userdata[1]); | ||
$value = call_user_func_array($userdata[0], $params); | ||
} | ||
}, [$call, $params]); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,7 @@ | |
* @author Nick Sagona, III <[email protected]> | ||
* @copyright Copyright (c) 2009-2016 NOLA Interactive, LLC. (http://www.nolainteractive.com) | ||
* @license http://www.popphp.org/license New BSD License | ||
* @version 2.0.1 | ||
* @version 2.0.2 | ||
*/ | ||
abstract class AbstractElement extends Child implements ElementInterface | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ | |
* @author Nick Sagona, III <[email protected]> | ||
* @copyright Copyright (c) 2009-2016 NOLA Interactive, LLC. (http://www.nolainteractive.com) | ||
* @license http://www.popphp.org/license New BSD License | ||
* @version 2.0.1 | ||
* @version 2.0.2 | ||
*/ | ||
|
||
class Button extends AbstractElement | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
* @author Nick Sagona, III <[email protected]> | ||
* @copyright Copyright (c) 2009-2016 NOLA Interactive, LLC. (http://www.nolainteractive.com) | ||
* @license http://www.popphp.org/license New BSD License | ||
* @version 2.0.1 | ||
* @version 2.0.2 | ||
*/ | ||
|
||
class CheckboxSet extends AbstractElement | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ | |
* @author Nick Sagona, III <[email protected]> | ||
* @copyright Copyright (c) 2009-2016 NOLA Interactive, LLC. (http://www.nolainteractive.com) | ||
* @license http://www.popphp.org/license New BSD License | ||
* @version 2.0.1 | ||
* @version 2.0.2 | ||
*/ | ||
interface ElementInterface | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,6 @@ | |
* @author Nick Sagona, III <[email protected]> | ||
* @copyright Copyright (c) 2009-2016 NOLA Interactive, LLC. (http://www.nolainteractive.com) | ||
* @license http://www.popphp.org/license New BSD License | ||
* @version 2.0.1 | ||
* @version 2.0.2 | ||
*/ | ||
class Exception extends \Exception {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ | |
* @author Nick Sagona, III <[email protected]> | ||
* @copyright Copyright (c) 2009-2016 NOLA Interactive, LLC. (http://www.nolainteractive.com) | ||
* @license http://www.popphp.org/license New BSD License | ||
* @version 2.0.1 | ||
* @version 2.0.2 | ||
*/ | ||
|
||
class Input extends AbstractElement | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
* @author Nick Sagona, III <[email protected]> | ||
* @copyright Copyright (c) 2009-2016 NOLA Interactive, LLC. (http://www.nolainteractive.com) | ||
* @license http://www.popphp.org/license New BSD License | ||
* @version 2.0.1 | ||
* @version 2.0.2 | ||
*/ | ||
|
||
class Button extends Element\Input | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ | |
* @author Nick Sagona, III <[email protected]> | ||
* @copyright Copyright (c) 2009-2016 NOLA Interactive, LLC. (http://www.nolainteractive.com) | ||
* @license http://www.popphp.org/license New BSD License | ||
* @version 2.0.1 | ||
* @version 2.0.2 | ||
*/ | ||
|
||
class Captcha extends Text | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
* @author Nick Sagona, III <[email protected]> | ||
* @copyright Copyright (c) 2009-2016 NOLA Interactive, LLC. (http://www.nolainteractive.com) | ||
* @license http://www.popphp.org/license New BSD License | ||
* @version 2.0.1 | ||
* @version 2.0.2 | ||
*/ | ||
|
||
class Checkbox extends Element\Input | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
* @author Nick Sagona, III <[email protected]> | ||
* @copyright Copyright (c) 2009-2016 NOLA Interactive, LLC. (http://www.nolainteractive.com) | ||
* @license http://www.popphp.org/license New BSD License | ||
* @version 2.0.1 | ||
* @version 2.0.2 | ||
*/ | ||
|
||
class Color extends Element\Input | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ | |
* @author Nick Sagona, III <[email protected]> | ||
* @copyright Copyright (c) 2009-2016 NOLA Interactive, LLC. (http://www.nolainteractive.com) | ||
* @license http://www.popphp.org/license New BSD License | ||
* @version 2.0.1 | ||
* @version 2.0.2 | ||
*/ | ||
|
||
class Csrf extends Hidden | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
* @author Nick Sagona, III <[email protected]> | ||
* @copyright Copyright (c) 2009-2016 NOLA Interactive, LLC. (http://www.nolainteractive.com) | ||
* @license http://www.popphp.org/license New BSD License | ||
* @version 2.0.1 | ||
* @version 2.0.2 | ||
*/ | ||
|
||
class Datalist extends Text | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
* @author Nick Sagona, III <[email protected]> | ||
* @copyright Copyright (c) 2009-2016 NOLA Interactive, LLC. (http://www.nolainteractive.com) | ||
* @license http://www.popphp.org/license New BSD License | ||
* @version 2.0.1 | ||
* @version 2.0.2 | ||
*/ | ||
|
||
class Date extends Element\Input | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
* @author Nick Sagona, III <[email protected]> | ||
* @copyright Copyright (c) 2009-2016 NOLA Interactive, LLC. (http://www.nolainteractive.com) | ||
* @license http://www.popphp.org/license New BSD License | ||
* @version 2.0.1 | ||
* @version 2.0.2 | ||
*/ | ||
|
||
class DateTime extends Element\Input | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
* @author Nick Sagona, III <[email protected]> | ||
* @copyright Copyright (c) 2009-2016 NOLA Interactive, LLC. (http://www.nolainteractive.com) | ||
* @license http://www.popphp.org/license New BSD License | ||
* @version 2.0.1 | ||
* @version 2.0.2 | ||
*/ | ||
|
||
class DateTimeLocal extends Element\Input | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
* @author Nick Sagona, III <[email protected]> | ||
* @copyright Copyright (c) 2009-2016 NOLA Interactive, LLC. (http://www.nolainteractive.com) | ||
* @license http://www.popphp.org/license New BSD License | ||
* @version 2.0.1 | ||
* @version 2.0.2 | ||
*/ | ||
|
||
class Email extends Element\Input | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,6 @@ | |
* @author Nick Sagona, III <[email protected]> | ||
* @copyright Copyright (c) 2009-2016 NOLA Interactive, LLC. (http://www.nolainteractive.com) | ||
* @license http://www.popphp.org/license New BSD License | ||
* @version 2.0.1 | ||
* @version 2.0.2 | ||
*/ | ||
class Exception extends \Exception {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
* @author Nick Sagona, III <[email protected]> | ||
* @copyright Copyright (c) 2009-2016 NOLA Interactive, LLC. (http://www.nolainteractive.com) | ||
* @license http://www.popphp.org/license New BSD License | ||
* @version 2.0.1 | ||
* @version 2.0.2 | ||
*/ | ||
|
||
class File extends Element\Input | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
* @author Nick Sagona, III <[email protected]> | ||
* @copyright Copyright (c) 2009-2016 NOLA Interactive, LLC. (http://www.nolainteractive.com) | ||
* @license http://www.popphp.org/license New BSD License | ||
* @version 2.0.1 | ||
* @version 2.0.2 | ||
*/ | ||
|
||
class Hidden extends Element\Input | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
* @author Nick Sagona, III <[email protected]> | ||
* @copyright Copyright (c) 2009-2016 NOLA Interactive, LLC. (http://www.nolainteractive.com) | ||
* @license http://www.popphp.org/license New BSD License | ||
* @version 2.0.1 | ||
* @version 2.0.2 | ||
*/ | ||
|
||
class Month extends Element\Input | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
* @author Nick Sagona, III <[email protected]> | ||
* @copyright Copyright (c) 2009-2016 NOLA Interactive, LLC. (http://www.nolainteractive.com) | ||
* @license http://www.popphp.org/license New BSD License | ||
* @version 2.0.1 | ||
* @version 2.0.2 | ||
*/ | ||
|
||
class Number extends Element\Input | ||
|
Oops, something went wrong.