Building complex applications can sometimes be a pain when extending or editing later on; I read online about the S.O.L.I.D principles of OOD, I want to understand if I am following a good approach that I can follow when developing the rest of the app.
I need answers to the following questions
- Is the code well-documented?
- The use of namespaces, Was it necessary ?
- Does the class violates any of the SOLID principles ?
$ic = new static;
Is it wrong to use this approach for inline usage?- Using static methods for easy access in a single line of code to creating and getting taxonomies, Are there better ideas ?
App\Taxonomy::register("categories")->isHierarchical()->produce()
The usage, Is it developer friendly to you ? Or are there other approaches developers would rather ?
Some of the ideas I had in mind when writing down the Class:
- I used namespaces because the project will have many classes and sub packages that would need categorizing for better usage by developers when they develop mini apps (plugins).
- I used static methods for single line usage to make it easier for developers to register new taxonomies and set options into one line of code.
Here is the class
namespace App;
/**
* Class Taxonomy
*
* @package App
*/
class Taxonomy {
/**
* Store all the taxonomies
*
* @since 0.5
*
* @var array
*/
protected static $taxonomies = array();
/**
* Default settings for creating taxonomies
*
* @since 0.5
*
* @var array
*/
protected static $tax_defaults = array(
'key' => null,
'hierarchical' => false
);
/**
* A Temporary variable that holds the taxonomy
* settings until the taxonomy is being produced
* then the temporary variable is being reset
* to an empty array
*
* @since 0.5
*
* @var array
*/
protected static $tax = array();
/**
* Temporarily holds an error because
* in some cases errors occurs when for example
* using a duplicate taxonomy key
*
* @since 0.5
*
* @var bool
*/
protected static $error = false;
/**
* Register a new taxonomy to be later on
* added to a data set
*
* @since 0.5
*
* @param $taxonomy_key String Unique key used for the taxonomy as an identifier
* @return static
*/
public static function register( $taxonomy_key ){
$ic = new static;
self::reset();
if( isset( self::$taxonomies[ $taxonomy_key ] ) )
self::$error = "taxonomy-already-registered";
self::$tax = self::$tax_defaults;
self::$tax['key'] = $taxonomy_key;
return $ic;
}
/**
* Set the taxonomy to hierarchical
*
* @since 0.5
*
* @return static
*/
static function isHierarchical(){
$ic = new static;
self::$tax['hierarchical'] = true;
return $ic;
}
/**
* Final step, This method is used to store
* the taxonomy to App global taxonomies variable
*
* @since 0.5
*
* @return bool
*/
static function produce(){
if( self::$error !== false )
return self::$error;
$key = self::$tax['key'];
self::$taxonomies[$key] = self::$tax;
self::reset();
}
/**
* Return the object of the Taxonomy
*
* @since 0.5
*
* @param $taxonomy_key
* @return bool
*/
static function get( $taxonomy_key ){
return isset( self::$taxonomies[$taxonomy_key] ) ? self::$taxonomies[$taxonomy_key] : false;
}
/**
* Reset temporary variables
*
* @since 0.5
*/
protected static function reset(){
self::$tax = array();
}
}
Usage example
// Register Taxonomies
App\Taxonomy::register("categories")->isHierarchical()->produce();
App\Taxonomy::register("tags")->produce();
// Get Taxonomies
App\Taxonomy::get("tags"); // Will return an array of the information registered