I'm busy rewriting some from my spaghetti code into OOP classes. I'm still learning OOP and very new to the concept. From help from this answer from @EliasVanOotegemto on a previous question I've asked I have written the following class which sets up conditionals according to 4 values passed to it by a user and checking these values against URL parameters whether they exist or not
Here is the class with its interface:
ConditionsRefInterface.php
namespace PG\Referrer\Single\Post;
interface ConditionsReferInterface
{
/**
* @param (string)$authorReferrer
* @return $this
*/
public function setAuthorReferrer($isAuthorReferrer);
/**
* @param (string)$dateReferrer
* @return $this
*/
public function setDateReferrer($isDateReferrer);
/**
* @param (string)$searchReferrer
* @return $this
*/
public function setSearchReferrer($isSearchReferrer);
/**
* @param (string)$taxReferrer
* @return $this
*/
public function setTaxReferrer($isTaxReferrer);
/**
* @return (bool)
*/
public function isAuthorReferrer();
/**
* @return (bool)
*/
public function isDateReferrer();
/**
* @return (bool)
*/
public function isSearchReferrer();
/**
* @return (bool)
*/
public function isTaxReferrer();
}
ConditionsRef.php
namespace PG\Referrer\Single\Post;
class ConditionsRefer implements ConditionsReferInterface {
/**
* @var $authorReferrer = null
*/
protected $isAuthorReferrer = null;
/**
* @var $dateReferrer = null
*/
protected $isDateReferrer = null;
/**
* @var $searchReferrer = null
*/
protected $isSearchReferrer = null;
/**
* @var $taxReferrer = null
*/
protected $isTaxReferrer = null;
/**
* @param array $values = null;
*
* Accepted parameters that can be passed to $values.
* @param (string) authorReferrer
* @param (string) dateReferrer
* @param (string) searchReferrer
* @param (string) taxReferrer
*
*/
public function __construct(array $values = null)
{
if(isset($values['authorReferrer']))
$this->setAuthorReferrer($values['authorReferrer']);
if(isset($values['dateReferrer']))
$this->setDateReferrer($values['dateReferrer']);
if(isset($values['searchReferrer']))
$this->setSearchReferrer($values['searchReferrer']);
if(isset($values['taxReferrer']))
$this->setTaxReferrer($values['taxReferrer']);
}
/**
* @param (string)$authorReferrer
* @return $this
*/
public function setAuthorReferrer($isAuthorReferrer)
{
$isAuthorReferrer = filter_var($isAuthorReferrer, FILTER_SANITIZE_STRING);
$isAuthorReferrer = isset($_GET[$isAuthorReferrer]);
$this->isAuthorReferrer = $isAuthorReferrer;
return $this;
}
/**
* @param (string)$dateReferrer
* @return $this
*/
public function setDateReferrer($isDateReferrer)
{
$isDateReferrer = filter_var($isDateReferrer, FILTER_SANITIZE_STRING);
$isDateReferrer = isset($_GET[$isDateReferrer]);
$this->isDateReferrer = $isDateReferrer;
return $this;
}
/**
* @param (string)$searchReferrer
* @return $this
*/
public function setSearchReferrer($isSearchReferrer)
{
$isSearchReferrer = filter_var($isSearchReferrer, FILTER_SANITIZE_STRING);
$isSearchReferrer = isset($_GET[$isSearchReferrer]);
$this->isSearchReferrer = $isSearchReferrer;
return $this;
}
/**
* @param (string)$taxReferrer
* @return $this
*/
public function setTaxReferrer($isTaxReferrer)
{
$isTaxReferrer = filter_var($isTaxReferrer, FILTER_SANITIZE_STRING);
$isTaxReferrer = isset($_GET[$isTaxReferrer]);
$this->isTaxReferrer = $isTaxReferrer;
return $this;
}
/**
* @return (bool)
*/
public function isAuthorReferrer()
{
return $this->isAuthorReferrer;
}
/**
* @return (bool)
*/
public function isDateReferrer()
{
return $this->isDateReferrer;
}
/**
* @return (bool)
*/
public function isSearchReferrer()
{
return $this->isSearchReferrer;
}
/**
* @return (bool)
*/
public function isTaxReferrer()
{
return $this->isTaxReferrer;
}
}
This works.
I have written another class that will use the conditions set by the class above. This is how it works
User sets the arguments in this new class (
QueryArgumentsRef.php
) which is passed to theConditionsRef
class through the constructo methodpublic function __construct(ConditionsReferInterface $conditionalReferrer, array $values = null)
The
get*
methods from theConditionsRef
class is then used to get the conditional values through$this->conditionalReferrer->isAuthorReferrer()
for instance$referArgs
returns values according to the user set arguments
Here is the interface and class:
QueryArgumentsRefInterface.php
namespace PG\Referrer\Single\Post;
interface QueryArgumentsReferInterface
{
/**
* @return (array) arguments
*/
public function getReferrerArgs();
}
QueryArgumentsRef.php
namespace PG\Referrer\Single\Post;
class QueryArgumentsRefer implements QueryArgumentsReferInterface
{
/**
* @var $conditionalReferrer = null
*/
protected $conditionalReferrer = null;
/**
* @var $values = null
*/
protected $values = null;
/**
* @var $referArgs = null
*/
protected $referArgs = null;
/**
* @param $conditionalReferrer
* @param array $values = null;
*
* Accepted parameters that can be passed to $values.
* @param (string) authorReferrer
* @param (string) dateReferrer
* @param (string) searchReferrer
* @param (string) taxReferrer
*
*/
public function __construct(ConditionsReferInterface $conditionalReferrer, array $values = null)
{
$this->conditionalReferrer = $conditionalReferrer;
$this->values = $values;
if(isset($values['authorReferrer']))
$this->conditionalReferrer->setAuthorReferrer($values['authorReferrer']);
if(isset($values['dateReferrer']))
$this->conditionalReferrer->setDateReferrer($values['dateReferrer']);
if(isset($values['searchReferrer']))
$this->conditionalReferrer->setSearchReferrer($values['searchReferrer']);
if(isset($values['taxReferrer']))
$this->conditionalReferrer->setTaxReferrer($values['taxReferrer']);
$this->referrerArgs();
}
public function referrerArgs()
{
switch (true) {
case ($this->conditionalReferrer->isAuthorReferrer() && isset($this->values['authorReferrer'])):
/**
* If the referrer came from an author archive page
*/
$author = $this->values['authorReferrer'];
$author = filter_var($author, FILTER_SANITIZE_STRING);
$referArgs = ['author' => (int) $_GET[$author]];
break;
case ($this->conditionalReferrer->isDateReferrer() && isset($this->values['dateReferrer'])):
/**
* If the referrer came from a date archive page
*/
$date = $this->values['dateReferrer'];
//$referArgs = (STILL TO WORKOUT);
break;
case ($this->conditionalReferrer->isSearchReferrer() && isset($this->values['searchReferrer'])):
/**
* If the referrer came from an search page
*/
$search = $this->values['searchReferrer'];
$search = filter_var($search, FILTER_SANITIZE_STRING);
$search_terms = $_GET[$search];
$referArgs = ['s' => (string) $search_terms];
break;
case ($this->conditionalReferrer->isTaxReferrer() && isset($this->values['taxReferrer'])):
/**
* If the referrer came from a taxonomy archive page
*/
$tax = $this->values['taxReferrer'];
$tax = filter_var($tax, FILTER_SANITIZE_STRING);
$terms = $_GET[$tax];
$terms = explode( ' ', $terms );
$taxonomy_objects = get_taxonomy( $terms[0] );
$hierarchical = $taxonomy_objects->hierarchical;
$referArgs = ['tax_query' => [
[
'taxonomy' => (string) $terms[0],
'terms' => (int) $terms[1],
'include_children' => false
]
]];
break;
default:
$referArgs = null;
}
$this->referArgs = (array) $referArgs;
return $this;
}
/**
* @return (array) query arguments according to referrer
*/
public function getReferrerArgs()
{
return $this->referArgs;
}
}
I can now use it at follows:
$a = new QueryArgumentsRefer(new ConditionsRefer(), ['authorReferrer' => 'aq', 'taxReferrer' => 'tq']);
var_dump($a->getReferrerArgs());
where var_dump($a->getReferrerArgs());
produces the following output if I'm on a page where the query variable tq
is set in the URL and the user have set the taxReferrer
arguments
array(1) {
["tax_query"]=>
array(1) {
[0]=>
array(3) {
["taxonomy"]=>
string(8) "category"
["terms"]=>
string(1) "1"
["include_children"]=>
bool(false)
}
}
}
This will later be used to set up a new instance of WP_Query
in another class.
Is the above code correct, or is there flaws that needs to be addressed? I welcome any constructive feedback, whether good or bad.