tsmsogn/datafilter

CakePHP plugin: Applies the callback to the all or specific request data

Installs: 6

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 0

Open Issues: 1

Type:cakephp-plugin

dev-master 2016-04-25 02:19 UTC

This package is auto-updated.

Last update: 2024-10-29 04:31:09 UTC


README

Applies the callback to the all or specific request data

Build Status

Requirements

  • CakePHP 2.x

Installation

Put your app plugin directory as Datafilter.

Enable plugin

In 2.0 you need to enable the plugin your app/Config/bootstrap.php file:

<?php
CakePlugin::load('Datafilter', array('bootstrap' => false, 'routes' => false));
?>

Usage

In controller:

<?php
App::uses('AppController', 'Controller');

class PostsController extends Controller {

	public $components = array('Datafilter.Datafilter');

	public function foo() {
		// Applies strtoupper() for all values
		/*
		$this->request->data looks like:
		Array
		(
			[Post] => Array
			(
				[key] => value
			)
		)
		*/

		$this->Datafilter->applyFilter('__ALL__', 'strtoupper');

		/*
		$this->request->data looks like:
		Array
		(
			[Post] => Array
			(
				[key] => VALUE
			)
		)
		*/
	}

	public function bar() {
		// Applies strtoupper() for specific field(s)
		/*
		$this->request->data looks like:
		Array
		(
			[Post] => Array
			(
				[key1] => value1
				[key2] => value2
			)
		)
		*/

		$this->Datafilter->applyFilter('Post.key1', 'strtoupper');

		/*
		$this->request->data looks like:
		Array
		(
			[Post] => Array
			(
				[key1] => VALUE1
				[key2] => value2
			)
		)
		*/
	}

	public function baz() {
		// Applies user function for specific field(s) with {}'s
		/*
		$this->request->data looks like:
		Array
		(
			[Post] => Array
			(
				[key1] =>  value1
			)
			[Tag] => Array
			(
				[0] => Array
				(
					[name] => value2
				)
				[1] => Array
				(
					[name] => value3
				)
			)
		)
		*/

		$this->Datafilter->applyFilter(array('Tag.{n}.name'), function($value) {
			return 'prefix_' . $value;
		});

		/*
		$this->request->data looks like:
		Array
		(
			[Post] => Array
			(
				[key1] => value1
			)
			[Tag] => Array
			(
				[0] => Array
				(
					[name] => prefix_value2
				)
				[1] => Array
				(
					[name] => prefix_value3
				)
			)
		)
		*/
	}

}

License

The MIT License (MIT) tsmsogn