Skip to content

Commit

Permalink
Add exclude functionality to addFilter() method
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksagona committed Jun 17, 2016
1 parent 33c29d8 commit de184bc
Show file tree
Hide file tree
Showing 44 changed files with 185 additions and 135 deletions.
148 changes: 142 additions & 6 deletions src/AbstractForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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]);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Element/AbstractElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion src/Element/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Element/CheckboxSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Element/ElementInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion src/Element/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
2 changes: 1 addition & 1 deletion src/Element/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Element/Input/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Element/Input/Captcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Element/Input/Checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Element/Input/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Element/Input/Csrf.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Element/Input/Datalist.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Element/Input/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Element/Input/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Element/Input/DateTimeLocal.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Element/Input/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Element/Input/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
2 changes: 1 addition & 1 deletion src/Element/Input/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Element/Input/Hidden.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Element/Input/Month.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Element/Input/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit de184bc

Please sign in to comment.