1: <?php
2: /**
3: * The MIT License
4: *
5: * Copyright 2017 David Schoenbauer.
6: *
7: * Permission is hereby granted, free of charge, to any person obtaining a copy
8: * of this software and associated documentation files (the "Software"), to deal
9: * in the Software without restriction, including without limitation the rights
10: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11: * copies of the Software, and to permit persons to whom the Software is
12: * furnished to do so, subject to the following conditions:
13: *
14: * The above copyright notice and this permission notice shall be included in
15: * all copies or substantial portions of the Software.
16: *
17: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23: * THE SOFTWARE.
24: */
25: namespace DSchoenbauer\Orm\Framework;
26:
27: /**
28: * A collection of attributes that define a given object
29: *
30: * @author David Schoenbauer <dschoenbauer@gmail.com>
31: */
32: class AttributeCollection
33: {
34:
35: /**
36: * returns the value that is housed inside an attribute
37: */
38: const BY_VALUE = "byValue";
39:
40: /**
41: * gets the attribute object itself allowing for late binding to a value
42: */
43: const BY_REF = "byRef";
44:
45: private $attributes = [];
46:
47: /**
48: * sets a parameter into the collection
49: * @param string $key a key used to retrieve or identify a given value
50: * @param mixed $value a value to be stored
51: * @return $this
52: */
53: public function set($key, $value)
54: {
55: $this->ensureKey($key)->attributes[$key]->setValue($value);
56: return $this;
57: }
58:
59: /**
60: * Retrieves a parameter if the parameter doesn't exist one is created with the default value
61: * @param string $key a one word label for the value
62: * @param mixed $defaultValue a value to use if the parameter is not present
63: * @param string $type byRef or byValue : byRef will return the attribute
64: * object and byValue will return the attribute value
65: * @return mixed type will define what is returned
66: * @since v1.0.0
67: */
68: public function get($key, $defaultValue = null, $type = self::BY_VALUE)
69: {
70: $attr = &$this->ensureKey($key, $defaultValue)->attributes[$key];
71: if ($type == self::BY_REF) {
72: return $attr;
73: }
74: return $attr->getValue();
75: }
76:
77: /**
78: * Validates that the key is present and if it isn't it adds it in
79: * @param string $key a field that will allow to be retrieved later
80: * @param mixed $value a value to be associated with the key
81: * @return $this
82: * @since v1.0.0
83: */
84: private function ensureKey($key, $value = null)
85: {
86: if (!$this->has($key)) {
87: $this->attributes[$key] = new Attribute($value);
88: }
89: return $this;
90: }
91:
92: /**
93: * Validates the existence of a previously set parameter
94: * @param string $key
95: * @return bool
96: * @since v1.0.0
97: */
98: public function has($key)
99: {
100: return array_key_exists($key, $this->attributes);
101: }
102: }
103: