Advertisement
Kresha7

Site configuration v0.3

Jun 10th, 2012
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.92 KB | None | 0 0
  1. <?php
  2.  
  3. class Configuration {
  4.  
  5.     private $data = array(), $preffix, $suffix, $html = ''; // Class properties
  6.    
  7.     public $include_default = false; // Include also everything in default.
  8.     public $sort = false; // By default there is no sorting
  9.     public $sort_type = 'normal'; // Specifies the sort type (normal => 'low to high) and (reverse => 'high to low)
  10.     public $sort_by = 'array'; // Sort by array by "array" or "key"
  11.  
  12.     public function __set($name, $value) {
  13.         $this->data[$name] = $value;
  14.     }
  15.  
  16.     public function __get($name) {
  17.         if (array_key_exists($name, $this->data)) {
  18.             return $this->data[$name];
  19.         }
  20.     }
  21.  
  22.     /**
  23.      * Returns output in form of HTML or as a plain array,
  24.      * currently there is css and js output implementet.
  25.      * * Suggetion are very welcome
  26.      *
  27.      * @author Kreshnik Hasanaj
  28.      * @version 0.3
  29.      * @param string $type is the array elements key.
  30.      * @param string $name is the key of the next array element within the main array if none specified (default) will be called.
  31.      * @param bool $build determines if the output should be html or just the array (true means html false means array)
  32.      * @return the output which will be returned.
  33.      */
  34.     public function get_output($type, $name = 'default', $build = true) {
  35.         $this->html = '';
  36.         if (array_key_exists($type, $this->data)) {
  37.             if (!empty($this->data[$type][$name]) || !empty($this->data[$type]['default'])) {
  38.                 if ($this->include_default === true) {
  39.                     if (!empty($this->data[$type][$name]) && $name != 'default') {
  40.                         $arr_elements = $this->sort_elements(array_merge($this->data[$type]['default'], $this->data[$type][$name]));
  41.                     } else {
  42.                         $arr_elements = $this->sort_elements($this->data[$type]['default']);
  43.                     }
  44.                 } else {
  45.                     $arr_elements = $this->sort_elements($this->data[$type][$name]);
  46.                 }
  47.                 if ($build === true) {
  48.                     $this->get_template($type);
  49.                     foreach ($arr_elements as $elem => $value) {
  50.                         if ($this->sort == true) {
  51.                             switch ($this->sort_by) {
  52.                                 case 'array':
  53.                                     $this->html .= sprintf($this->preffix, $value) . $this->suffix;
  54.                                     break;
  55.                                 case 'key':
  56.                                     $this->html .= sprintf($this->preffix, $value[0]) . $this->suffix;
  57.                                     break;
  58.                             }
  59.                         } else {
  60.                             $this->html .= sprintf($this->preffix, $value) . $this->suffix;
  61.                         }
  62.                     }
  63.                     return $this->html;
  64.                 } else {
  65.                     return $arr_elements;
  66.                 }
  67.             } else {
  68.                 return 'The array with the given key is empty.';
  69.             }
  70.         } else {
  71.             return 'No array with the given key exists.';
  72.         }
  73.     }
  74.  
  75.     /**
  76.      * @method get_template associates the preffix and suffix
  77.      * @param string $type determines the template we want to use for the preffix and suffix
  78.      */
  79.     private function get_template($type) {
  80.         switch ($type) {
  81.             case 'css':
  82.                 $this->preffix = "<link rel='stylesheet' type='text/css' href='%s' />";
  83.                 $this->suffix = "\n";
  84.                 break;
  85.  
  86.             case 'js':
  87.                 $this->preffix = "<script type='text/javascript' src='%s'></script>";
  88.                 $this->suffix = "\n";
  89.                 break;
  90.         }
  91.     }
  92.    
  93.     /**
  94.      * @method sort_elements sorts the array if proper configuration is made
  95.      * @param array $array the array that will be sorted
  96.      */
  97.     private function sort_elements($array) {
  98.         if ($this->sort == true) {
  99.             switch ($this->sort_by) {
  100.                 case 'array':
  101.                     switch ($this->sort_type) {
  102.                         case 'normal':
  103.                             asort($array);
  104.                             break;
  105.                         case 'reverse':
  106.                             arsort($array);
  107.                             break;
  108.                     }
  109.                     break;
  110.                 case 'key':
  111.                     switch (strtolower($this->sort_type)) {
  112.                         case 'normal':
  113.                             ksort($array);
  114.                             break;
  115.                         case 'reverse':
  116.                             krsort($array);
  117.                             break;
  118.                     }
  119.                     break;
  120.             }
  121.             return $array;
  122.         } else {
  123.             return $array;
  124.         }
  125.     }
  126.  
  127. }
  128.  
  129. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement