|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Jeremeamia\PhpPatterns\Collection; |
| 4 | + |
| 5 | +/** |
| 6 | + * The Registry Pattern |
| 7 | + * |
| 8 | + * Common Aliases: Map, Container, Collection |
| 9 | + * Related Patterns: Singleton Registry, Map |
| 10 | + * |
| 11 | + * Martin Fowler describes the Registry pattern as "A well-known object that |
| 12 | + * other objects can use to find common objects and services." In other words, |
| 13 | + * it is an object (whether a static, singleton, or instance) that contains |
| 14 | + * other objects or data, and keeps a references to those by a name or key. It's |
| 15 | + * essentially a simplified associative array with an object-oriented interface. |
| 16 | + * |
| 17 | + * Registries are commonly used to keep track of objects and data that need to |
| 18 | + * be accessed in a global or wide context. Because of this, they are often designed |
| 19 | + * to be a static or singleton class, even though that is not necessary, and is |
| 20 | + * generally discouraged. |
| 21 | + * |
| 22 | + * Also, the basic interface and functionality of a Registry can be re-used to |
| 23 | + * form the basis of other array-like patterns. Therefore, the RegistryTrait is |
| 24 | + * used by many other patterns in the PhpPatterns collection. |
| 25 | + */ |
| 26 | +trait RegistryTrait |
| 27 | +{ |
| 28 | + /** |
| 29 | + * Get an object/value out of the Registry |
| 30 | + * |
| 31 | + * @param string $key The key of the object/value to retrieve |
| 32 | + * @param mixed $default The value to return if the key is missing |
| 33 | + * |
| 34 | + * @return mixed The object/value from the Registry |
| 35 | + */ |
| 36 | + public function get($key, $default = null) |
| 37 | + { |
| 38 | + return $this->has($key) ? $this->data[$key] : $default; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Store an object/value in the Registry |
| 43 | + * |
| 44 | + * @param string $key The key of the object/value being set |
| 45 | + * @param mixed $value The object/value to store |
| 46 | + * |
| 47 | + * @return self The instance of the Registry for chaining |
| 48 | + */ |
| 49 | + public function set($key, $value) |
| 50 | + { |
| 51 | + $this->data[$key] = $value; |
| 52 | + |
| 53 | + return $this; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Check to see if a key exists in the Registry |
| 58 | + * |
| 59 | + * @param string $key The key of the object/value to check |
| 60 | + * |
| 61 | + * @return bool Whether or not the ey exists |
| 62 | + */ |
| 63 | + public function has($key) |
| 64 | + { |
| 65 | + return array_key_exists($key, $this->data); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Remove an object/value from the Registry |
| 70 | + * |
| 71 | + * @param string $key The key of the object/value to remove |
| 72 | + * |
| 73 | + * @return self The instance of the object for chaining |
| 74 | + */ |
| 75 | + public function remove($key) |
| 76 | + { |
| 77 | + unset($this->data[$key]); |
| 78 | + |
| 79 | + return $this; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Removes all objects/values from the Registry |
| 84 | + * |
| 85 | + * @return self The instance of the object for chaining |
| 86 | + */ |
| 87 | + public function clear() |
| 88 | + { |
| 89 | + $this->data = []; |
| 90 | + |
| 91 | + return $this; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Returns all objects/values in the Registry as an associative array |
| 96 | + * |
| 97 | + * @return array All of the objects/data |
| 98 | + */ |
| 99 | + public function all() |
| 100 | + { |
| 101 | + return $this->data; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Checks if the Registry is empty |
| 106 | + * |
| 107 | + * @return bool Whether or not its empty |
| 108 | + */ |
| 109 | + public function isEmpty() |
| 110 | + { |
| 111 | + return (0 === count($this->data)); |
| 112 | + } |
| 113 | +} |
0 commit comments