Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Configuration {
- private $data = array(), $preffix, $suffix, $html = ''; // Class properties
- public $include_default = false; // Include also everything in default.
- public $sort = false; // By default there is no sorting
- public $sort_type = 'normal'; // Specifies the sort type (normal => 'low to high) and (reverse => 'high to low)
- public $sort_by = 'array'; // Sort by array by "array" or "key"
- public function __set($name, $value) {
- $this->data[$name] = $value;
- }
- public function __get($name) {
- if (array_key_exists($name, $this->data)) {
- return $this->data[$name];
- }
- }
- /**
- * Returns output in form of HTML or as a plain array,
- * currently there is css and js output implementet.
- * * Suggetion are very welcome
- *
- * @author Kreshnik Hasanaj
- * @version 0.3
- * @param string $type is the array elements key.
- * @param string $name is the key of the next array element within the main array if none specified (default) will be called.
- * @param bool $build determines if the output should be html or just the array (true means html false means array)
- * @return the output which will be returned.
- */
- public function get_output($type, $name = 'default', $build = true) {
- $this->html = '';
- if (array_key_exists($type, $this->data)) {
- if (!empty($this->data[$type][$name]) || !empty($this->data[$type]['default'])) {
- if ($this->include_default === true) {
- if (!empty($this->data[$type][$name]) && $name != 'default') {
- $arr_elements = $this->sort_elements(array_merge($this->data[$type]['default'], $this->data[$type][$name]));
- } else {
- $arr_elements = $this->sort_elements($this->data[$type]['default']);
- }
- } else {
- $arr_elements = $this->sort_elements($this->data[$type][$name]);
- }
- if ($build === true) {
- $this->get_template($type);
- foreach ($arr_elements as $elem => $value) {
- if ($this->sort == true) {
- switch ($this->sort_by) {
- case 'array':
- $this->html .= sprintf($this->preffix, $value) . $this->suffix;
- break;
- case 'key':
- $this->html .= sprintf($this->preffix, $value[0]) . $this->suffix;
- break;
- }
- } else {
- $this->html .= sprintf($this->preffix, $value) . $this->suffix;
- }
- }
- return $this->html;
- } else {
- return $arr_elements;
- }
- } else {
- return 'The array with the given key is empty.';
- }
- } else {
- return 'No array with the given key exists.';
- }
- }
- /**
- * @method get_template associates the preffix and suffix
- * @param string $type determines the template we want to use for the preffix and suffix
- */
- private function get_template($type) {
- switch ($type) {
- case 'css':
- $this->preffix = "<link rel='stylesheet' type='text/css' href='%s' />";
- $this->suffix = "\n";
- break;
- case 'js':
- $this->preffix = "<script type='text/javascript' src='%s'></script>";
- $this->suffix = "\n";
- break;
- }
- }
- /**
- * @method sort_elements sorts the array if proper configuration is made
- * @param array $array the array that will be sorted
- */
- private function sort_elements($array) {
- if ($this->sort == true) {
- switch ($this->sort_by) {
- case 'array':
- switch ($this->sort_type) {
- case 'normal':
- asort($array);
- break;
- case 'reverse':
- arsort($array);
- break;
- }
- break;
- case 'key':
- switch (strtolower($this->sort_type)) {
- case 'normal':
- ksort($array);
- break;
- case 'reverse':
- krsort($array);
- break;
- }
- break;
- }
- return $array;
- } else {
- return $array;
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement