Bläddra i källkod

return clone of registered item

alexlcdee 8 år sedan
förälder
incheckning
13c3d72887
3 ändrade filer med 34 tillägg och 11 borttagningar
  1. 6 3
      src/CategoriesContainer.php
  2. 2 1
      src/ItemsContainer.php
  3. 26 7
      src/Registry.php

+ 6 - 3
src/CategoriesContainer.php

@@ -17,31 +17,34 @@ class CategoriesContainer
     private $data = [];
 
     /**
+     * Add new category in container
      * @param string $key
      * @param ItemsContainer $value
      */
-    public function set(string $key, ItemsContainer $value)
+    public function add(string $key, ItemsContainer $value)
     {
         $this->data[$key] = $value;
     }
 
     /**
+     * Check if container has category
      * @param string $key
      * @return bool
      */
-    public function contains(string $key)
+    public function has(string $key)
     {
         return isset($this->data[$key]);
     }
 
     /**
+     * Get category from container
      * @param string $key
      * @return ItemsContainer
      * @throws RegistryException
      */
     public function get(string $key)
     {
-        if(!$this->contains($key)) {
+        if(!$this->has($key)) {
             throw new RegistryException('Category not found in registry of shown items');
         }
         return $this->data[$key];

+ 2 - 1
src/ItemsContainer.php

@@ -87,7 +87,8 @@ class ItemsContainer
     public function getItemBy(string $attributeName, $value)
     {
         if (isset($this->attributesMap[$attributeName]) && isset($this->attributesMap[$attributeName][$value])) {
-            return $this->data[$this->attributesMap[$attributeName][$value]];
+            $item = clone $this->data[$this->attributesMap[$attributeName][$value]];
+            return $item;
         }
         return null;
     }

+ 26 - 7
src/Registry.php

@@ -15,17 +15,34 @@ class Registry implements RegistryInterface
      */
     private $container;
 
-    private function __construct($typesMap)
+    /**
+     * Registry constructor.
+     * @param array $typesMap
+     */
+    private function __construct(array $typesMap)
     {
         $this->container = new CategoriesContainer();
-        foreach ($typesMap as $category => $type) {
-            $this->container->set($category, new ItemsContainer($type));
+        foreach ($typesMap as $category => $className) {
+            $this->container->add($category, new ItemsContainer($className));
         }
     }
 
-    public static function init($typesMap)
+    private function __clone()
+    {
+    }
+
+    private function __wakeup()
+    {
+    }
+
+    /**
+     * @param array $typesMap
+     * @return Registry
+     * @throws RegistryException
+     */
+    public static function init(array $typesMap)
     {
-        if(static::$instance !== null) {
+        if (static::$instance !== null) {
             throw new RegistryException('Registry can be initialized only once.');
         }
         static::$instance = new static($typesMap);
@@ -33,13 +50,14 @@ class Registry implements RegistryInterface
     }
 
     /**
+     * Add item to category of container
      * @param string $category
      * @param RegistrableItem $item
      * @throws RegistryException
      */
     public static function add($category, RegistrableItem $item)
     {
-        if (!static::$instance->container->contains($category)) {
+        if (!static::$instance->container->has($category)) {
             throw new RegistryException(strtr('Registry does not contain category "{category}"', [
                 '{category}' => $category
             ]));
@@ -48,13 +66,14 @@ class Registry implements RegistryInterface
     }
 
     /**
+     * Get category from container
      * @param string $category
      * @return ItemsContainer
      * @throws RegistryException
      */
     public static function get($category)
     {
-        if (!static::$instance->container->contains($category)) {
+        if (!static::$instance->container->has($category)) {
             throw new RegistryException(strtr('Registry does not contain category "{category}"', [
                 '{category}' => $category
             ]));