<?php // vim:set et ts=4 sw=4 sts=4:
# Copyright (C) 2003-2014 hipplay Inc.
#
# This is UNPUBLISHED PROPRIETARY SOURCE CODE OF hipplay Inc.
# The copyright notice above does not evidence any actual or intended
# publication of such source code. The contents of this file may not
# be disclosed to third parties, copied or duplicated in any form, in
# whole or in part, without the prior written permission of
# hipplay Inc.
#


namespace HipPlayConnect\Configuration;


class AxConfigurationFile {

    const EQUALS_SEPARATED = '/^([a-zA-Z0-9_]+)=(.+)$/';
    const SPACE_SEPARATED  = '/^([A-Z]\w+)\s+(.+)\s*$/';

    protected $fileName;
    protected $format;
    protected $values = NULL;

    public function __construct ($fileName, $format) {
        $this->fileName = $fileName;
        $this->format = $format;
    }

    /**
     * Retrieve the value of the specified key from the Branding configuration.
     *
     * @param string $key     The configuration value to retrieve.
     * @param mixed  $default Value to return if the configured value
     *                        does not exist or is empty.
     *
     * @return string The configured value, or the supplied default.
     */
    public function getKey ($key, $default = NULL) {
        if ($this->values === NULL)
            if (!$this->loadConfig())
                return $default;

        if (!isset($this->values[$key]))
            return $default;

        $value = $this->values[$key];
        if (empty($value))
            return $default;

        return $value;
    }

    public function hasKey ($key) {
        if ($this->values === NULL)
            if (!$this->loadConfig())
                return FALSE;

        return isset($this->values[$key])
               && !empty($this->values[$key]);
    }

    /**
     * Load the branding configuration file.
     *
     * @return bool <tt>TRUE</tt> if the file was successfully read,
     *              <tt>FALSE</tt> if there was an error reading the file.
     */
    private function loadConfig () {
        $values = self::load(
                $this->fileName,
                $this->format
        );

        if (NULL === $values) {
            $this->values = array();
            return FALSE;
        }

        $this->values = $values;
        return TRUE;
    }


    /**
     * Load the configuration file into an associative array.
     *
     * @param string $fileName The configuration file to load.
     * @param string $format   The regular expression to parse each line with.
     *
     * @return array An associative array mapping the configuration keys
     *               from the specified file to their values.
     */
    public static function load($fileName, $format=self::SPACE_SEPARATED)
    {
        $values = array();

        $configFile = @file($fileName);
        if( !$configFile ) {
            error_log("Could not load $fileName");
            return NULL;
        }

        foreach( $configFile as $configLine )
            if( preg_match( $format, $configLine, $RE ) )
                $values[$RE[1]] = trim($RE[2]);

        return $values;
    }

} 