alexlcdee 8 vuotta sitten
commit
957cb62547

+ 54 - 0
.gitignore

@@ -0,0 +1,54 @@
+# Created by .ignore support plugin (hsz.mobi)
+### Composer template
+composer.phar
+/vendor/
+
+# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
+# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
+composer.lock
+
+
+### JetBrains template
+# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
+# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
+
+# User-specific stuff:
+.idea/**/workspace.xml
+.idea/**/tasks.xml
+.idea/dictionaries
+
+# Sensitive or high-churn files:
+.idea/**/dataSources/
+.idea/**/dataSources.ids
+.idea/**/dataSources.xml
+.idea/**/dataSources.local.xml
+.idea/**/sqlDataSources.xml
+.idea/**/dynamic.xml
+.idea/**/uiDesigner.xml
+
+# Gradle:
+.idea/**/gradle.xml
+.idea/**/libraries
+
+# Mongo Explorer plugin:
+.idea/**/mongoSettings.xml
+
+## File-based project format:
+*.iws
+
+## Plugin-specific files:
+
+# IntelliJ
+/out/
+
+# mpeltonen/sbt-idea plugin
+.idea_modules/
+
+# JIRA plugin
+atlassian-ide-plugin.xml
+
+# Crashlytics plugin (for Android Studio and IntelliJ)
+com_crashlytics_export_strings.xml
+crashlytics.properties
+crashlytics-build.properties
+fabric.properties

+ 21 - 0
.idea/deployment.xml

@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="PublishConfigData">
+    <serverData>
+      <paths name="medgis">
+        <serverdata>
+          <mappings>
+            <mapping local="$PROJECT_DIR$" web="/" />
+          </mappings>
+        </serverdata>
+      </paths>
+      <paths name="zyorna">
+        <serverdata>
+          <mappings>
+            <mapping local="$PROJECT_DIR$" web="/" />
+          </mappings>
+        </serverdata>
+      </paths>
+    </serverData>
+  </component>
+</project>

+ 8 - 0
.idea/modules.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectModuleManager">
+    <modules>
+      <module fileurl="file://$PROJECT_DIR$/.idea/registry-container.iml" filepath="$PROJECT_DIR$/.idea/registry-container.iml" />
+    </modules>
+  </component>
+</project>

+ 4 - 0
.idea/php.xml

@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="PhpProjectSharedConfiguration" php_language_level="7" />
+</project>

+ 8 - 0
.idea/registry-container.iml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="WEB_MODULE" version="4">
+  <component name="NewModuleRootManager">
+    <content url="file://$MODULE_DIR$" />
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+  </component>
+</module>

+ 17 - 0
composer.json

@@ -0,0 +1,17 @@
+{
+  "name": "alexlcdee/registry-container",
+  "description": "container for storing objects and searching it",
+  "minimum-stability": "stable",
+  "license": "proprietary",
+  "authors": [
+    {
+      "name": "Alexander Larkin",
+      "email": "info@alexlcdee.ru"
+    }
+  ],
+  "autoload": {
+    "psr-4": {
+      "AlexLcDee\\RegistryContainer\\": "src/"
+    }
+  }
+}

+ 49 - 0
src/CategoriesContainer.php

@@ -0,0 +1,49 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: lcdee
+ * Date: 31.05.2017
+ * Time: 12:57
+ */
+
+namespace AlexLcDee\RegistryContainer;
+
+
+class CategoriesContainer
+{
+    /**
+     * @var ItemsContainer[]
+     */
+    private $data = [];
+
+    /**
+     * @param string $key
+     * @param ItemsContainer $value
+     */
+    public function set(string $key, ItemsContainer $value)
+    {
+        $this->data[$key] = $value;
+    }
+
+    /**
+     * @param string $key
+     * @return bool
+     */
+    public function contains(string $key)
+    {
+        return isset($this->data[$key]);
+    }
+
+    /**
+     * @param string $key
+     * @return ItemsContainer
+     * @throws RegistryException
+     */
+    public function get(string $key)
+    {
+        if(!$this->contains($key)) {
+            throw new RegistryException('Category not found in registry of shown items');
+        }
+        return $this->data[$key];
+    }
+}

+ 94 - 0
src/ItemsContainer.php

@@ -0,0 +1,94 @@
+<?php
+
+namespace AlexLcDee\RegistryContainer;
+
+
+class ItemsContainer
+{
+    /**
+     * @var RegistrableItem[]
+     */
+    private $data = [];
+
+    private $attributesMap = [];
+
+    private $type;
+
+    private $current = 0;
+
+    /**
+     * ItemsContainer constructor.
+     * @param string $type type name of items inside container
+     */
+    public function __construct($type)
+    {
+        $this->type = $type;
+    }
+
+    /**
+     * @param RegistrableItem $item
+     * @throws RegistryException
+     */
+    public function add(RegistrableItem $item)
+    {
+        if (!($item instanceof $this->type)) {
+            throw new RegistryException(strtr('Item must be an instance of {class}', [
+                '{class}' => $this->type
+            ]));
+        }
+
+        $this->data[] = $item;
+
+        $this->mapAttributes($item, $this->current);
+
+        $this->current++;
+    }
+
+    /**
+     * @param RegistrableItem $item
+     * @param int $itemPosition
+     */
+    private function mapAttributes(RegistrableItem $item, int $itemPosition)
+    {
+        if (!count($this->attributesMap)) {
+            foreach ($item->getAttributes() as $attributeName => $attribute) {
+                if (is_scalar($attribute)) {
+                    $this->attributesMap[$attributeName] = [
+                        $attribute => $itemPosition
+                    ];
+                }
+            }
+        } else {
+            foreach ($item->getAttributes() as $attributeName => $attribute) {
+                if (is_scalar($attribute)) {
+                    $this->attributesMap[$attributeName][$attribute] = $itemPosition;
+                }
+            }
+        }
+    }
+
+    /**
+     * @param string $attributeName
+     * @return array
+     */
+    public function getListOf(string $attributeName)
+    {
+        if (isset($this->attributesMap[$attributeName])) {
+            return array_keys($this->attributesMap[$attributeName]);
+        }
+        return [];
+    }
+
+    /**
+     * @param string $attributeName
+     * @param mixed $value
+     * @return null|RegistrableItem
+     */
+    public function getItemBy(string $attributeName, $value)
+    {
+        if (isset($this->attributesMap[$attributeName]) && isset($this->attributesMap[$attributeName][$value])) {
+            return $this->data[$this->attributesMap[$attributeName][$value]];
+        }
+        return null;
+    }
+}

+ 19 - 0
src/RegistrableItem.php

@@ -0,0 +1,19 @@
+<?php
+
+namespace AlexLcDee\RegistryContainer;
+
+
+interface RegistrableItem
+{
+    /**
+     * @param bool $names
+     * @return array attribute values indexed by attribute names.
+     */
+    public function getAttributes($names = true);
+
+    /**
+     * @param $name
+     * @return mixed the attribute value. Null if the attribute is not set or does not exist.
+     */
+    public function getAttribute($name);
+}

+ 53 - 0
src/Registry.php

@@ -0,0 +1,53 @@
+<?php
+
+namespace AlexLcDee\RegistryContainer;
+
+
+class Registry
+{
+    /**
+     * @var CategoriesContainer
+     */
+    private static $container;
+
+    public static function init($typesMap)
+    {
+        if (static::$container !== null) {
+            throw new RegistryException('Registry must be initialized once');
+        }
+        static::$container = new CategoriesContainer();
+        foreach ($typesMap as $category => $type) {
+            static::$container->set($category, $type);
+        }
+    }
+
+    /**
+     * @param string $category
+     * @param RegistrableItem $item
+     * @throws RegistryException
+     */
+    public static function add($category, RegistrableItem $item)
+    {
+        if (!static::$container->contains($category)) {
+            throw new RegistryException(strtr('Registry does not contain category "{category}"', [
+                '{category}' => $category
+            ]));
+        }
+        static::$container->get($category)->add($item);
+    }
+
+    /**
+     * @param string $category
+     * @return ItemsContainer
+     * @throws RegistryException
+     */
+    public static function get($category)
+    {
+        if (!static::$container->contains($category)) {
+            throw new RegistryException(strtr('Registry does not contain category "{category}"', [
+                '{category}' => $category
+            ]));
+        }
+        return static::$container->get($category);
+    }
+}

+ 9 - 0
src/RegistryException.php

@@ -0,0 +1,9 @@
+<?php
+
+namespace AlexLcDee\RegistryContainer;
+
+
+class RegistryException extends \Exception
+{
+
+}

+ 17 - 0
src/RegistryInterface.php

@@ -0,0 +1,17 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: lcdee
+ * Date: 31.05.2017
+ * Time: 15:03
+ */
+
+namespace AlexLcDee\RegistryContainer;
+
+
+interface RegistryInterface
+{
+    public static function add($category, RegistrableItem $item);
+
+    public static function get($category);
+}