Why does my class constructor feels bloat and faulty? - php

I'm fairly new to OOP and I'm struggling with setting up my classes. I'm working on a project where I have the following classes: dogTag, dog, user.
This is my current dogTagclass:
class DogTag
{
const POST_TYPE = 'dog-tag';
private ?int $id = null;
private string $number;
private ?Dog $dog;
private ?User $user;
private bool $isActive = false;
private bool $isLost = false;
private string $generationDate;
private ?string $activationDate;
private string $productionStatus;
public function __construct($id = null, string $number, ?Dog $dog = null, ?User $user = null, bool $isActive, bool $isLost, string $generationDate, ?string $activationDate, string $productionStatus)
{
$this->id = $id;
$this->number = $number;
$this->dog = $dog;
$this->user = $user;
$this->isActive = $isActive;
$this->isLost = $isLost;
$this->generationDate = $generationDate;
$this->activationDate = $activationDate;
$this->productionStatus = $productionStatus;
}
Let me explain the functionality of the dog tag. Dog Tags are being generated by a generator and stored into the database. So a dog tag only needs the following arguments when being generated: number (this is a unique generated number and not a unique database ID), isActive, isLost (could be optional), generationDate, productionStatus. All other arguments are not necessarily needed for the generation.
So my first question is: "Do I need to set the optional arguments in the contructor?".
Let me explain why I did this for the moment. When a user receives the unique dog tag number they can activate the dog tag. Therefor I use a method called activateDogTag. For a dog tag to be activated it needs to be attached to a user. Therefor I use the $user argument in the constructor.
Here comes my second question: "Should I use a method setUser(User $user) where I inject the user object into the dogtag class?".
Another problem that I'm struggling with is that I also use an id argument. This will be a bit harder to explain why I'm using this and why it feels wrong.
I'm also using the dogTag class to I can instantiate it with all the data from the database. For the moment I have a static DogTag::get($id) method. This class grabs the data from the database by the ID an instantiates a new DogTag class where I fill all the arguments with the data received from the database.
For information: I'm using WordPress)
public static function get($postId): ?DogTag
{
$post = get_post($postId);
if (empty($post)) {
return null;
}
$dogTagId = $post->ID;
$generationDate = $post->post_date;
$dogTagNumber = get_post_meta($post->ID, 'dog_tag_number', true);
$dogId = get_post_meta($post->ID, 'dog_id', true);
if (empty($dogId)) {
$dog = null;
} else {
$dog = Dog::get($dogId);
}
$userId = $post->post_author;
$user = $userId ? User::get($userId) : null;
$isActive = get_post_meta($dogTagId, 'dog_tag_is_active', true);
$isLost = get_post_meta($dogTagId, 'dog_tag_is_lost', true);
$activationDate = get_post_meta($dogTagId, 'dog_tag_activation_date', true);
$productionStatus = get_post_meta($dogTagId, 'dog_tag_production_status', true);
$dogTag = new self($dogTagId, $dogTagNumber, $dog, $user, $isActive, $isLost,
$generationDate, $activationDate, $productionStatus);
$dogTag->post = get_post($postId);
return $dogTag;
}
This way all the arguments are filled and are available within the class.
So where am I going wrong and what I'm I doing correct? The problem is that I can't really find any solution in my online courses for these problems as they don't go this deep.
Where I'm also struggling is eg with the static activation class (DogTag::activateDogTag):
public static function activateDogTag()
{
// Here goes the form
$dogTag = self::getDogTagByDogTagNumber('4OV9NHOPXLAB6X9B');
// $dogTag = self::getDogTagByDogTagNumber('VBD6JZODTZ6L4YU7');
if (!$dogTag) {
var_dump('no dog tag with this ID found');
}
if(true == $dogTag->getIsActive()) {
var_dump('Dog Tag is already active');
}
if ($dogTag->getUser()) {
var_dump('user is set');
return;
}
var_dump('Activate');
$userId = get_current_user_id();
if (0 === $userId) {
var_dump('not logged in as a user');
}
$dogTag->setIsActive(true);
$dogTag->setUser($userId);
$user = $dogTag->getUser();
var_dump($dogTag);
// Update dog Tag
$args = [
'post_type' => 'dog-tag',
'post_status' => 'publish',
'post_title' => $dogTag->getNumber(),
'post_date' => $dogTag->generationDate,
'post_author' => $dogTag->getUser()->getId(),
'meta_input' => [
'dog_tag_number' => $dogTag->getNumber(),
'dog_tag_is_active' => $dogTag->getIsActive(),
'dog_tag_is_lost' => $dogTag->getIsLost(),
'dog_tag_production_status' => $dogTag->getProductionStatus(),
]
];
var_dump($args);
$postId = wp_update_post($args);
$dogTag->setId($postId);
return $dogTag;
}
Is this a good practice:
$dogTag->setIsActive(true);
$dogTag->setUser($userId);
$user = $dogTag->getUser();
and after the data is stored into the database via wp_update_post by setting the id property via DogTag->setId($postId)?

First of all, if it works then you are not doing anything wrong.
Question 1
It's usually better practice to start with required parameters, followed by optional parameters, if it feels too bloated you could remove all optional parameters from the constructor and create setters for those.
ID/Fetching
What you have here is fine, you could create another class DogTagFactory/DogTagRepository which handles the creation/database process for you if you and reduce bloat.
Overall try not to get too stuck in the chase of "perfect" best practice, it's always going to depend on the situation and usually in the end it does not even matter all that much.

Related

Access an instance variable in constructor

I have a class that I call via:
$tagIt_Instance1 = new TagIt;
$tagIt_Instance1->tag_table = 'tags';
// if maintenace is set to true, fill out these (for update)
$tagIt_Instance1->tag_object_table = 'projects'; // the name of your table
$tagIt_Instance1->tag_object_fieldname = 'project_tags'; // the name of your field
$tagIt_Instance1->tag_object_id = isset($_GET['id']) ? $_GET['id'] : ''; // the corresponding id for the row of the table
// end maintenance
if (isset($_GET['tag_it']) && $_GET['tag_it'] == true) {
$tagIt_Instance1->TagItAjax(
isset($_GET['action']) ? $_GET['action'] : '',
isset($_GET['term']) ? $_GET['term'] : '',
isset($_GET['match_id']) ? $_GET['match_id'] : '',
isset($_GET['tag_object_id']) ? $_GET['tag_object_id'] : ''
);
}
In the class, in the constructor, I want to access $tag_table
class TagIt
{
// edit defaults
private $debug = true;
/* this will reload the page if a new tag cannot be added. suggested to leave true as otherwise the user
could submit a page with a tag that is not in the system because of said error. if maintenance is enabled
then the tag would be removed eventually when matching occurs, but lets keep the db clean */
private $reloadOnError = true;
private $confirmationMsgAdd = 'Do you want to add the new tag'; // no question mark or tag identifier, thats handled by jquery
private $confirmationMsgDel = 'Do you want to delete the tag';
private $confirmationMsgDel2 = 'from the database as well';
private $errorNewTag = 'An error occured. Could not process new tag!';
private $generalErrorMsg = 'An error occured.';
// do allow users to delete special cases
private $special_cases = array('featured');
private $select2JS = '<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/select2/3.5.0/select2.min.js"></script>';
private $select2CSS = '<link rel="stylesheet" href="/neou_cms/plugins/select2.css" type="text/css" media="all">';
public $tag_table = '';
/* enabling this will make sure that only tags in the database are
kept for a specific row when tagit checks for matches */
public $maintenance = true;
// do not edit!!! ///////////////////////
// if maintenace is set to true these become applicable (leave blank here)
public $tag_object_table = '';
public $tag_object_fieldname = '';
public $tag_object_id = '';
private $location = '';
public function __construct()
{
// we don't want the query string messing up the AJAX request, thus we remove it
$this->location = strtok($_SERVER["REQUEST_URI"], '?');
echo $this->tag_table;
}
However, whenever I try and access $this->tag_table, I always get an empty variable, despite $this->tag_table working in later functions of the script. I feel like it may be a scope issue, I am not sure.
Your $this->tag_table is empty in the constructor because it's still referencing the unset/empty variable (public $tag_table = '';).
Which is evident with your code:
$tagIt_Instance1 = new TagIt;
$tagIt_Instance1->tag_table = 'tags';
In the above example you're constructing the object and then you only set $tag_table.
You could modify your __construct() to have the tag table passed to it if you require:
function __construct($tag_table) {
// we don't want the query string messing up the AJAX request, thus we remove it
$this->location = strtok($_SERVER["REQUEST_URI"], '?');
// set the tag table, allowing you to access
$this->tag_table = $tag_table;
echo $this->tag_table;
}
Meaning you can skip one step:
$tagIt_Instance1 = new TagIt('tags');
It's in fact, common practice to pass "defining" variables like this through the __construct() method while instantiating a class.

OOP (beneficial usage)

for my question on how to use OOP in a beneficial way I assume as an example a BASKET to which its owner (Tom) having a certain ADDRESS (NY) can add ARTICLES (Bike, Car). Finally a BILL is printed containg all these information.
My problem is: How to handle collecting the information desired (here: owner, city, amount of items) from several objects? Because I think it is stupid to do this manually as done below (see 4.), isn't it? (even more since the amount of information increases in reality)
So what is the "clean way" for creating the bill / collecting the information needed in this example?
<?php
$a = new basket('Tom','NY');
$a->add_item("Bike",1.99);
$a->add_item("Car",2.99);
$b = new bill( $a );
$b->do_print();
1.
class basket {
private $owner = "";
private $addr = "";
private $articles = array();
function basket( $name, $city ) {
// Constructor
$this->owner = $name;
$this->addr = new addresse( $city );
}
function add_item( $name, $price ) {
$this->articles[] = new article( $name, $price );
}
function item_count() {
return count($this->articles);
}
function get_owner() {
return $this->owner;
}
function get_addr() {
return $this->addr;
}
}
2.
class addresse {
private $city;
function addresse( $city ) {
// Constructor
$this->city = $city;
}
function get_city() {
return $this->city;
}
}
3.
class article {
private $name = "";
private $price = "";
function article( $n, $p ) {
// Constructor
$this->name = $n;
$this->price = $p;
}
}
4.
class bill {
private $recipient = "";
private $city = "";
private $amount = "";
function bill( $basket_object ) {
$this->recipient = $basket_object->get_owner();
$this->city = $basket_object->get_addr()->get_city();
$this->amount = $basket_object->item_count();
}
function do_print () {
echo "Bill for " . $this->recipient . " living in " . $this->city . " for a total of " . $this->amount . " Items.";
}
}
If you do Tell Dont Ask, you would indeed add a render method to the bill to which you would pass an instance of BillRenderer. Bill would then tell BillRenderer how to render the Bill. This is in accordance with InformationExpert and High Cohesion principles that suggest methods to be on the objects with the most information to fulfill the task.
class Bill
{
…
public function renderAs(BillRenderer $billRenderer)
{
$billRenderer->setRecipient($this->owner);
$billRenderer->setAddress($this->address);
…
return $billRenderer->render();
}
}
BillRenderer (an interface) would then know the output format, e.g. you'd write concrete renderers for PlainText or HTML or PDF:
class TxtBillRenderer implements BillRenderer
{
…
public function render()
{
return sprintf('Invoice for %s, %s', $this->name, $this->address);
}
}
echo $bill->renderAs(new TxtBillRenderer);
If your Bill contains other objects, those would implement a renderAs method as well. The Bill would then pass the renderer down to these objects.
Both basket as well as bill could have a relation to a positions item - an object representing an ordered list of zero or more items with a count and price.
As such a list is an object of it's own it's easy to pass around:
$bill = new Bill($buyer, $address, $basket->getPositions());
However the printing of the bill should be done by the BillPrinter, because it's not the job of the bill to print itself:
$billPrinter = new BillPrinter($bill, $printerDevice);
$billPrinter->print();
First of all , in PHP5 the constructor it public function __construct(). What you are using there is the PHP4 way. And then ther eare other issues with your code:
instead of passing the name of the city to the Basket ( do you mean Cart ?), you should be creating the address object instance and passing it.
do not add items based on name an amount of money to the basket, instead add the whole instance of item, otherwise you will have a lot of problems when switching site language or currency.
the Articles (do you mean Items ?) should be created based on ID, not based on name. The reasons for that are the same as above + you will have issues with uniqueness. And then some of items might have lower price, when bought in combination. You need a way to safely identify them.
As for cleaning up the code there:
you should stop creating instance from given parameters in the constructor. While it is not always a bad thing, in your case you are making a mess there.
Bill should not be responsible for printing itself.
Something like :
class Basket
{
// -- other code
public function handleInvoice( Bill $invoice )
{
$invoice->chargeFor( $this->items );
$invoice->chargeTo( $this->account );
return $invoice->process();
}
}
.. then use it as
$cart = new Basket(..);
// some operation with it
$invoice = new Bill;
$cart->handleInvoice($invoice);
$printer = new PDFPrinter;
// OR new JpegPrinter; OR new FakePrinter OR anything else
$printer->print( $invoice );
This would give you an instance of Bill outside the class which then you can either print or send to someone.
Also , you might benefit from watching the willowing lecture:
Inheritance, Polymorphism, & Testing
Don't Look For Things!
Clean Code I: Arguments

OOP PHP with FB Graph API

I am creating a Student object using OOP in PHP via the Facebook Graph API. My problem is that not all users share the same amount of data on FB so if a particular user doesn't list certain variables instantiated in the object, I get undefined variable messages. What is the best way to prepare for this, i.e. create an object whether or not the user has shared all of the data in the object or not? Code below:
<?php
require_once('class.Student.php');
$name = $user_profile['name'];
$hometown = $user_profile['hometown']['name'];
$location = $user_profile['location']['name'];
$birthday = $user_profile['birthday'];
$highschool = $user_profile['education'][0]['school']['name'];
$hsgrad = $user_profile['education'][0]['year']['name'];
$collegeid = $user_profile['education'][1]['school']['id'];
$college = $user_profile['education'][1]['school']['name'];
$majorid = $user_profile['education'][1]['concentration'][0]['id'];
$major = $user_profile['education'][1]['concentration'][0]['name'];
$grad = $user_profile['education'][1]['year']['name'];
$company = $user_profile['work'][0]['employer']['name'];
$jobtitle = $user_profile['work'][0]['position']['name'];
$startdate = $user_profile['work'][0]['start_date'];
$interest = $interests['data'][0]['name'];
$interestid = $interests['data'][0]['id'];
$objStudent = new Student($name,$hometown,$location,$birthday,$highschool,$hsgrad,$collegeid,$college,$majorid,$major,$grad,$company,$jobtitle,$startdate,$interest,$interestid);
?>
And the class itself:
<?php
class Student {
public $name;
public $hometown;
public $location;
public $birthday;
public $highschool;
public $hsgrad;
public $collegeid;
public $college;
public $majorid;
public $major;
public $grad;
public $company;
public $jobtitle;
public $startdate;
public $interest;
public $interestid;
public function __construct($name,$hometown,$location,$birthday,$highschool,$hsgrad,$collegeid,$college,$majorid,$major,$grad,$company,$jobtitle,$startdate,$interest,$interestid) {
$this->name = $name;
$this->hometown = $hometown;
$this->location = $location;
$this->birthday = $birthday;
$this->highschool = $highschool;
$this->hsgrad = $hsgrad;
$this->collegeid = $collegeid;
$this->college = $college;
$this->majorid = $majorid;
$this->major = $major;
$this->grad = $grad;
$this->company = $company;
$this->jobtitle = $jobtitle;
$this->startdate = $startdate;
$this->interest = $interest;
$this->interestid = $interestid;
}
I understand how to handle this in the functions of the class, using a simple isset such as:
function goalRecommender () {
if (isset($this->interest)) {
if ($this->interest =='Computer Science') {
echo "<p>Based on your interest in Computer Science, we recommend working in the software industry. Furthermore, your interest in user interface design would be thoroughly put to use at Facebook, one of the fastest growing technology companies in the world. If you would like to pursue another goal,
search our database to the right or click one of the 'popular goal' links.<p>";
}
elseif ($this->interests =='Finance') {
echo "<p>You're interested in Finance</p>";
}
elseif ($this->interests =='Film') {
echo "<p>You're interested in Film</p>";
}
elseif ($this->interests =='Marketing') {
echo "<p>You're interested in Marketing</p>";
} else {
echo "<p>Choose a goal.</p>";
}
} else {
echo "<p>What are you interested in?
<select>
<option>Finance</option>
<option>Marketing</option>
<option>Film</option>
<option>Software</option>
</select></p>";
}
}
But I'm just learning OOP in PHP and am unsure how to do it when instantiating an object. Any guidance would be sincerely appreciated.
There's a lot for your class:
Have your Student constructor take an array. This will reduce the pain of variable adjustments, especially if you end up extending Student.Clarification: rather than passing in the name, college, hometown, birthday, etc, you pass in an array that has all that same information.
$name = $user_profile['name'];
$hometown = $user_profile['hometown']['name'];
//becomes
$user['name'] = $user_profile['name'];
$user['hometown'] = $user_profile['hometown']['name'];
It's faster. The cost of any parameter isn't much, but when you have tons like this it can at least make a mark.
It's easier to call. You no longer have to remember the order of variables. "Is it name, hometown, location, birthday? Or name, birthday, location, hometown?" You have tons of variables, there's no way to easily remember that.
It's easier to change. Adding a variable? No problem, add it to the array and check for it inside the function. Removing a variable? That's hard to do, but not if it's in an array.
public function __construct(
$name,
$hometown,
$location,
$birthday,
$highschool,
$hsgrad,
$collegeid,
$college,
$majorid,
$major,
$grad,
$company,
$jobtitle,
$startdate,
$interest,
$interestid) {
//becomes
public function __construct(array $info) {
Your variables should be protected or private, not public. This is pretty much industry standard.
You'll want to provide getter and setter methods because the variables are inaccessible. Many IDE's can do this for you.
As for your error messages:
You need to check to make sure the variable exists before you actually access it. Use the isset() function for this:
$hometown = isset($user_profile['hometown']['name']) ? $user_profile['hometown']['name'] : null;

Passing global variables and arguments to a function in PHP

Hello and thanks for being there,
I would like to pass a variable ($user) from a previous function to another one, but I need to use the arguments of the new function to pass the values that will render this new one.
Is there any way I can pass a variable from another function to a new function that only expects three arguments, and none of them is the variable from the previous function?
Example:
function my_function($country, $age, $colour) {
if ($user = true) {
echo "User is from " . $country . " and " . $age . " and his favourite colour is " . $colour;
}
}
my_function("italy", 19, "red");
It works if I put inside function my_function:
global $user;
but I believe using global variables is not a good practice.
Any idea on how to pass it as an argument? Should I just add it as another variable after $colour in the arguments of the new function?
Thanks a lot for your help :)
You can pass it as an argument, or better do this:
if ($user) my_function("italy", 19, "red");
since you don't have to use the $user variable inside that function.
You can use this function but best practice will be using class.
i .e if you call my_function("italy", 19, "red"), $user will be false by default
function my_function($country, $age, $colour, $user=false) {
if ($user == true) {
echo "User is from " $country . "and " . $age . " and his favourite colour is " . $colour;
}
}
my_function("italy", 19, "red",true);
Well, global variables are a bad practice, but a viable one in your case.
I would definitely recommend you looking into/using a Object Oriented approached.
There are really a bunch of different ways to achieve what your trying.
One way would be to encapsulate your code into a object.
<?php
class My_Cool_Class {
public $user = false;
public function myFunction($country, $age, $color) {
if ($this->user)
echo "User is from {$country} and {$age} years old and his favourite colour is {$color}";
}
}
$class = new My_Cool_Class();
$class->user = new User();
$class->myFunction("Italy", 19, "red");
Or you could implement a Singleton Pattern to easily get access to your object/functions from anywhere.
<?php
class My_Cool_Class {
public $user = false;
protected static $_instance;
public function getIntance() {
if(!self::$_instance)
self::$_instance = new self();
return self::$_instance;
}
public function setUser($user) {
$this->user = $user;
}
public function myFunction($country, $age, $color) {
if ($this->user)
echo "User is from {$country} and {$age} years old and his favourite colour is {$color}";
}
}
//Set User from anywhere with this
My_Cool_Class::getInstance()->setUser($user);
//Call your function anywhere with this.
My_Cool_Class::getInstance()->myFunction("Italy", 19, "red");
If your previous function is something like this:
/**
* Callback function for so_user_data() that tells if we want to give you info about the user or not.
* #param (string) $user | Accepts a Username as input
* #return (boolean) | true if the User is 'Rob'
*/
function so_user_fn( $user )
{
$allowed_users = array(
'Rob'
,'Jay'
,'Silent Bob'
);
if ( in_array $user, $allowed_users ) )
return true;
// false if not 'Rob'
return false;
}
/**
* Shows the country, age & fav. colour of a user or denies displaying this information
* if the user is not a public v.i.p. or someone other we want to give you info about.
* #uses so_user_fn() | used to determine if the user is 'Rob'
* #param (string) $user | Username
* #param (string) $country | (optional) Country of origin
* #param (integer) $age | (optional) Age of the user
* #param (string) $colour | (optional) Fav. Colour
*/
function so_user_data( $user, $country = 'an unknown country', $age = 'unknown', $colour = 'not known' )
{
$output = "$user is from {$country} and {$age} years old. His favourite colour is {$colour}.";
// Only print the output if the user is 'Rob'
if ( so_user_test( $user ) )
return print $output;
return print 'We are not allowed to give you information about this user.';
}
You can call it like this:
so_user_data( 'Fancy Franzy' );
// outputs: We are not allowed to give you information about this user.
so_user_data( 'Rob' );
// outputs: Rob is from an unknown country and unknown years old. His favourite colour is not known.
so_user_data( 'Silent Bob', 'U.S.A.', '38', 'brown' );
// outputs: Silent Bob is from U.S.A. and 38 years old. His favourite colour is brown.

How to Make Zend_Controller_Router resets un-wanted parameter by default

I'm using Zend-Framework 1.9.5 to make a web-application, But it's Url_Helper was quite tricky to me in the matter of parameter reset!, I know it's a good feature (parameter preserving) but in most cases I don't need it!.
So I'm thinking of overriding the default Router to force it loosing parameters Unless I ask for it or maybe specifying a certain parameters that it keeps like (lang, or something like that).
Also I want to make it the default router so I don't have to edit my Controllers, Views to get that done!
Any suggestions?
Update:
I spent the whole morning trying to write my url helper Admin_View_Helper_Xurl, But I couldn't do anything that solves the problem:
<?php
class Admin_View_Helper_Xurl extends Zend_View_Helper_Abstract
{
public function xurl(array $urlOptions = array(), $name = 'default', $reset = false, $encode = true)
{
$router = Zend_Controller_Front::getInstance()->getRouter();
$wanted_params = array('module', 'controller', 'action', 'lang', 'page', 'search');
$route = $router->getCurrentRoute();
$something = anyWayToGetThatObjectOrClass();
$params = $something->getParams();
foreach($params as $key => $val) {
if (!in_array($key, $wanted_params)) {
$params[$key] = null; // OR uset($params[$key]);
}
}
$something->clearParams();
$something->setParams($params);
return $router->assemble($urlOptions, $name, $reset, $encode);
}
}
I tried to get current URL parameters and filter them and clear the current parameters and pass my filtered ones but I couldn't do anything that does it without hard-code editing one Zend_Framework code :(.
Thanks
When generating a link a view, you can ask the helper to get rid of all aparamters with a simple boolean :
<?php echo $this->url(array('controller' => 'index', action => 'action'), 'default', true); ?>
The last parameter tells whether to reset parameters or not.
I came up with this solution. It took 7 hours to be functional.
class Zend_View_Helper_Xurl extends Zend_View_Helper_Abstract
{
const RESET_ALL = 'all';
const RESET_CUSTOM = 'normal';
const RESET_NON_MVC = 'mvc';
const RESET_NONE = 'none';
protected $_wantedParams = array('module', 'controller', 'action', 'lang', 'page', 'search');
protected $_router;
/**
* Generates an url given the name of a route.
*
* #access public
*
* #param array $urlOptions Options passed to the assemble method of the Route object.
* #param mixed $name The name of a Route to use. If null it will use the current Route
* #param bool $reset Whether or not to reset the route defaults with those provided
* #return string Url for the link href attribute.
*/
public function __construct()
{
$router = Zend_Controller_Front::getInstance()->getRouter();
$this->_router = clone $router;
}
public function xurl(array $urlOptions = array(), $reset = 'mvc', $encode = true)
{
$urlOptions = $this->_getFilteredParams($urlOptions, $reset);
return $this->_router->assemble($urlOptions, $name, true, $encode);
}
protected function _getFilteredParams($data = array(), $level)
{
// $filteredValues = array();
$request = Zend_Controller_Front::getInstance()->getRequest();
$filteredValues = $request->getUserParams();
$$filteredValues['module'] = $request->getModuleName();
$$filteredValues['controller'] = $request->getControllerName();
$$filteredValues['action'] = $request->getActionName();
switch ($level) {
case self::RESET_ALL:
$filteredValues['module'] = null;
$filteredValues['controller'] = null;
$filteredValues['action'] = null;
// break omitted intentionally
case self::RESET_NON_MVC:
$filteredValues['page'] = null;
$filteredValues['lang'] = null;
$filteredValues['search'] = null;
// break omitted intentionally
case self::RESET_CUSTOM:
foreach ($filteredValues as $key=>$val) {
if (!in_array($key, $this->_wantedParams)) {
$filteredValues[$key] = null;
}
}
break;
case self::RESET_NONE:
break;
default:
throw new RuntimeException('Unsuppoted Xurl URL helper reset level.');
break;
}
foreach ($filteredValues as $key => $val) {
if (!array_key_exists($key, $data)) {
$data[$key] = $val;
}
}
return $data;
}
}
Clearly it's a View Helper class, may be not the best solution but it works fine with me for now.

Categories