Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
83.33% covered (success)
83.33%
5 / 6
CRAP
95.65% covered (success)
95.65%
22 / 23
AbstractApplication
0.00% covered (danger)
0.00%
0 / 1
83.33% covered (success)
83.33%
5 / 6
18
95.65% covered (success)
95.65%
22 / 23
 config
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 registerConfig
100.00% covered (success)
100.00%
1 / 1
4
100.00% covered (success)
100.00%
5 / 5
 addConfigValue
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
3 / 3
 updateConfigValue
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
3 / 3
 deleteConfigValue
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
3 / 3
 mergeConfig
0.00% covered (danger)
0.00%
0 / 1
7.10
87.50% covered (success)
87.50%
7 / 8
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2021 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop;
15
16/**
17 * Abstract application class
18 *
19 * @category   Pop
20 * @package    Pop
21 * @author     Nick Sagona, III <dev@nolainteractive.com>
22 * @copyright  Copyright (c) 2009-2021 NOLA Interactive, LLC. (http://www.nolainteractive.com)
23 * @license    http://www.popphp.org/license     New BSD License
24 * @version    3.7.0
25 */
26abstract class AbstractApplication implements ApplicationInterface
27{
28
29    /**
30     * Application config
31     * @var mixed
32     */
33    protected $config = null;
34
35    /**
36     * Access application config
37     *
38     * @return mixed
39     */
40    public function config()
41    {
42        return $this->config;
43    }
44
45    /**
46     * Register a new configuration with the application
47     *
48     * @param  mixed $config
49     * @throws \InvalidArgumentException
50     * @return AbstractApplication
51     */
52    public function registerConfig($config)
53    {
54        if (!is_array($config) && !($config instanceof \ArrayAccess) && !($config instanceof \ArrayObject)) {
55            throw new \InvalidArgumentException(
56                'Error: The config must be either an array itself, implement ArrayAccess or extend ArrayObject'
57            );
58        }
59
60        $this->config = $config;
61
62        return $this;
63    }
64
65    /**
66     * Add new value to config
67     *
68     * @param  string $name
69     * @param  string $value
70     * @return AbstractApplication
71     */
72    public function addConfigValue($name, $value)
73    {
74        if (!isset($this->config[$name])) {
75            $this->config[$name] = $value;
76        }
77        return $this;
78    }
79
80    /**
81     * Update existing value in config
82     *
83     * @param  string $name
84     * @param  string $value
85     * @return AbstractApplication
86     */
87    public function updateConfigValue($name, $value)
88    {
89        if (isset($this->config[$name])) {
90            $this->config[$name] = $value;
91        }
92        return $this;
93    }
94
95    /**
96     * Replace existing value in config
97     *
98     * @param  string $name
99     * @return AbstractApplication
100     */
101    public function deleteConfigValue($name)
102    {
103        if (isset($this->config[$name])) {
104            unset($this->config[$name]);
105        }
106        return $this;
107    }
108
109    /**
110     * Merge new or altered config values with the existing config values
111     *
112     * @param  mixed $config
113     * @param  boolean $preserve
114     * @throws Config\Exception
115     * @return AbstractApplication
116     */
117    public function mergeConfig($config, $preserve = false)
118    {
119        if ($this->config instanceof \Pop\Config\Config) {
120            $this->config->merge($config, $preserve);
121        } else if (is_array($config) || ($config instanceof \ArrayAccess) || ($config instanceof \ArrayObject)) {
122            if (null !== $this->config) {
123                $this->config = ($preserve) ? array_merge_recursive($this->config, $config) :
124                    array_replace_recursive($this->config, $config);
125            } else {
126                $this->config = $config;
127            }
128        }
129
130        return $this;
131    }
132
133}