Advertisement
turbosasa

configuration.class

Jan 19th, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.83 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Klase kas atbild par konfigurāciju izgūšan un saglabāšanu
  5.  */
  6. class Configuration {
  7.  
  8.    private $configs = array();
  9.  
  10.    public function __construct() {
  11.       $configData = include('config.php');
  12.       $this->configs = $configData;
  13.    }
  14.  
  15.    /**
  16.     *
  17.     * Saglabājam konfigurācijas
  18.     * @param type $configs : konfigurācijas
  19.     */
  20.    public function saveConfigs($configs) {
  21.       $data = '<?php return ' . var_export($this->parseObjectToArray($configs), true) . ';';
  22.       file_put_contents('config.php', $data);
  23.    }
  24.  
  25.    /**
  26.     * Iegūstam konfigurācijas
  27.     */
  28.    public function getConfigs() {
  29.       return $this->parseArrayToObject($this->configs);
  30.    }  
  31.  
  32.    /**
  33.     *
  34.     * Pārveido masīvu par objektu
  35.     * @param type $array : masīvs kurš tiks pārveidots par objektu
  36.     * @return objekts
  37.     */
  38.    private function parseArrayToObject($array) {
  39.       $object = new stdClass();
  40.       if (is_array($array) && count($array) > 0) {
  41.          foreach ($array as $name => $value) {
  42.             $name = strtolower(trim($name));
  43.             if (!empty($name)) {
  44.                $object->$name = $value;
  45.             }
  46.          }
  47.       }
  48.       return $object;
  49.       //return $restQuery = $this->recast('Configs', $object);
  50.    }
  51.  
  52.    /**
  53.     *
  54.     * Pārveido objektu par masīvu
  55.     * @param object: objekts ko pārvaidot
  56.     * Warning: 'object' jāsatur objekts savādāk tiek atgriests tukš masīvs
  57.     */
  58.    private function parseObjectToArray($object) {
  59.       $array = array();
  60.       if (is_object($object)) {
  61.          $array = get_object_vars($object);
  62.          foreach ($array as $name => $value) {
  63.  
  64.             if (is_object($value)) {
  65.                $array[$name] = parseObjectToArray($value);
  66.             }
  67.          }
  68.       }
  69.       return $array;
  70.    }
  71.  
  72. }
  73. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement