From 545826b55c460f6bdbfc848f706ce053d32f4b76 Mon Sep 17 00:00:00 2001 From: Kyle Johnson Date: Wed, 29 May 2013 07:37:00 -0400 Subject: [PATCH] A method to make named parameters strongly defined. Added a method that processes named parameters and ensures all mandatory ones exist and substitutes missing optional ones with default values. This makes named parameters strongly defined and easier to read. Thanks to http://developinginthedark.com/posts/cakephp-tip-1-named-parameters for the idea. --- web/app/Controller/AppController.php | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/web/app/Controller/AppController.php b/web/app/Controller/AppController.php index a737d6167..cfb08ec51 100644 --- a/web/app/Controller/AppController.php +++ b/web/app/Controller/AppController.php @@ -32,7 +32,7 @@ App::uses('Controller', 'Controller'); * @link http://book.cakephp.org/2.0/en/controllers.html#the-app-controller */ class AppController extends Controller { - public $helpers = array('Html', 'Form', 'Js' => array('Jquery')); + public $helpers = array('Html', 'Form'); public $components = array('Cookie', 'Session'); public function beforeFilter() { @@ -64,4 +64,25 @@ class AppController extends Controller { $this->set('daemonStatus', ('Stopped')); } } + + function extractNamedParams($mandatory, $optional = array()) { + $params = $this->params['named']; + + if(empty($params)) { + return false; + } + + $mandatory = array_flip($mandatory); + $all_named_keys = array_merge($mandatory, $optional); + $valid = array_intersect_key($params, $all_named_keys); + $output = array_merge($optional, $valid); + $diff = array_diff_key($all_named_keys, $output); + + if (empty($diff)) { + return $output; + } else { + return false; + } + + } }