Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
35 / 35
100.00% covered (success)
100.00%
13 / 13
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractApplication
100.00% covered (success)
100.00%
35 / 35
100.00% covered (success)
100.00%
13 / 13
25
100.00% covered (success)
100.00%
1 / 1
 setName
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setVersion
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getVersion
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasVersion
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 config
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 load
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 registerConfig
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 addConfigValue
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 updateConfigValue
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 deleteConfigValue
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 mergeConfig
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
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-2024 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
16use Pop\Config;
17use InvalidArgumentException;
18
19/**
20 * Abstract application class
21 *
22 * @category   Pop
23 * @package    Pop
24 * @author     Nick Sagona, III <dev@nolainteractive.com>
25 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
26 * @license    http://www.popphp.org/license     New BSD License
27 * @version    4.2.0
28 */
29abstract class AbstractApplication implements ApplicationInterface
30{
31
32    /**
33     * Name
34     * @var ?string
35     */
36    protected ?string $name = null;
37
38    /**
39     * Version
40     * @var ?string
41     */
42    protected ?string $version = null;
43
44    /**
45     * Application config
46     * @var mixed
47     */
48    protected mixed $config = null;
49
50    /**
51     * Set name
52     *
53     * @param  string $name
54     * @return static
55     */
56    public function setName(string $name): static
57    {
58        $this->name = $name;
59        return $this;
60    }
61
62    /**
63     * Get name
64     *
65     * @return string
66     */
67    public function getName(): string
68    {
69        return $this->name;
70    }
71
72    /**
73     * Determine if the name is set
74     *
75     * @return bool
76     */
77    public function hasName(): bool
78    {
79        return ($this->name !== null);
80    }
81
82    /**
83     * Set version
84     *
85     * @param  string $version
86     * @return static
87     */
88    public function setVersion(string $version): static
89    {
90        $this->version = $version;
91        return $this;
92    }
93
94    /**
95     * Get version
96     *
97     * @return string
98     */
99    public function getVersion(): string
100    {
101        return $this->version;
102    }
103
104    /**
105     * Determine if version has been set
106     *
107     * @return bool
108     */
109    public function hasVersion(): bool
110    {
111        return ($this->version !== null);
112    }
113
114    /**
115     * Access application config
116     *
117     * @return mixed
118     */
119    public function config(): mixed
120    {
121        return $this->config;
122    }
123
124    /**
125     * Optional method that can be used to load custom operations/configurations for an application to run
126     *
127     * @return AbstractApplication
128     */
129    public function load(): AbstractApplication
130    {
131        return $this;
132    }
133
134    /**
135     * Register a new configuration with the application
136     *
137     * @param  mixed $config
138     * @throws InvalidArgumentException
139     * @return AbstractApplication
140     */
141    public function registerConfig(mixed $config): AbstractApplication
142    {
143        if (!is_array($config) && !($config instanceof \ArrayAccess)) {
144            throw new \InvalidArgumentException(
145                'Error: The config must be either an array itself, implement ArrayAccess or extend ArrayObject'
146            );
147        }
148
149        $this->config = $config;
150
151        return $this;
152    }
153
154    /**
155     * Add new value to config
156     *
157     * @param  string $name
158     * @param  string $value
159     * @return AbstractApplication
160     */
161    public function addConfigValue(string $name, string $value): AbstractApplication
162    {
163        if (!isset($this->config[$name])) {
164            $this->config[$name] = $value;
165        }
166        return $this;
167    }
168
169    /**
170     * Update existing value in config
171     *
172     * @param  string $name
173     * @param  string $value
174     * @return AbstractApplication
175     */
176    public function updateConfigValue(string $name, string $value): AbstractApplication
177    {
178        if (isset($this->config[$name])) {
179            $this->config[$name] = $value;
180        }
181        return $this;
182    }
183
184    /**
185     * Replace existing value in config
186     *
187     * @param  string $name
188     * @return AbstractApplication
189     */
190    public function deleteConfigValue(string $name): AbstractApplication
191    {
192        if (isset($this->config[$name])) {
193            unset($this->config[$name]);
194        }
195        return $this;
196    }
197
198    /**
199     * Merge new or altered config values with the existing config values
200     *
201     * @param  mixed $config
202     * @param  bool  $preserve
203     * @throws Config\Exception
204     * @return AbstractApplication
205     */
206    public function mergeConfig(mixed $config, bool $preserve = false): AbstractApplication
207    {
208        if ($this->config instanceof Config\Config) {
209            $this->config->merge($config, $preserve);
210        } else if (is_array($config) || ($config instanceof \ArrayAccess) || ($config instanceof \ArrayObject)) {
211            if ($config instanceof Config\Config) {
212                $config = $config->toArray();
213            }
214            if ($this->config !== null) {
215                $this->config = ($preserve) ? array_merge_recursive($this->config, $config) :
216                    array_replace_recursive($this->config, $config);
217            } else {
218                $this->config = $config;
219            }
220        }
221
222        return $this;
223    }
224
225}