1 : <?php
2 : /**
3 : * LICENSE
4 : *
5 : * "THE BEER-WARE LICENSE" (Revision 42):
6 : * "Sven Strittmatter" <ich@weltraumschaf.de> wrote this file.
7 : * As long as you retain this notice you can do whatever you want with
8 : * this stuff. If we meet some day, and you think this stuff is worth it,
9 : * you can buy me a beer in return.
10 : *
11 : * @author Sven Strittmatter <ich@weltraumschaf.de>
12 : * @copyright Copyright (c) 2010, Sven Strittmatter.
13 : * @version 0.2.2
14 : * @license http://www.weltraumschaf.de/the-beer-ware-license.txt
15 : */
16 :
17 : /**
18 : * Simple abstraction of an imutable configuration array.
19 : *
20 : * You can simply pass an array to the constructor.
21 : *
22 : * Example:
23 : * <code>
24 : * <?php
25 : * $arr = array('foo' => 'bar', 'baz' = true);
26 : * $conf = new Api_Kwick_Config($arr);
27 : *
28 : * if ($conf->has('foo')) {
29 : * echo $conf->get('foo');
30 : * }
31 : *
32 : * // ...
33 : * </code>
34 : *
35 : * You can also load the config from a file like:
36 : * <code>
37 : * <?php
38 : * // config.php
39 : * return array('foo' => 'bar', 'baz' = true);
40 : * </code>
41 : *
42 : * Load the file:
43 : * <code>
44 : * <?php
45 : * $arr = include 'config.php';
46 : * $conf = new Api_Kwick_Config($arr);
47 : *
48 : * if ($conf->has('foo')) {
49 : * echo $conf->get('foo');
50 : * }
51 : *
52 : * // ...
53 : * </code>
54 : */
55 : class Api_Kwick_Config {
56 : /**
57 : * Assoc array holds the configuration values.
58 : *
59 : * @var array
60 : */
61 : private $config;
62 :
63 : /**
64 : * Initializes the configuration with given array.
65 : *
66 : * The passed array should be assoc. You access the values bey theres key.
67 : *
68 : * @param array $config The configuration array.
69 : */
70 : public function __construct(array $config) {
71 13 : $this->config = $config;
72 13 : }
73 :
74 : /**
75 : * Returns whether a configuration key exists.
76 : *
77 : * @param string $name Name of the configuration key.
78 : *
79 : * @return bool
80 : */
81 : public function has($name) {
82 8 : return array_key_exists((string) $name, $this->config);
83 : }
84 :
85 : /**
86 : * Returns a configuration by its key name.
87 : *
88 : * If the key does not exists the parameter passed by $default will
89 : * be returned.
90 : *
91 : * @param string $name Name of the configuration key.
92 : * @param mixed $default Fallback value. Default is null.
93 : *
94 : * @return mixed
95 : */
96 : public function get($name, $default = null) {
97 8 : if ($this->has($name)) {
98 5 : return $this->config[(string) $name];
99 : }
100 :
101 7 : return $default;
102 : }
103 : }
|