]> git.mxchange.org Git - core.git/blobdiff - framework/main/classes/factories/class_BaseFactory.php
Continued:
[core.git] / framework / main / classes / factories / class_BaseFactory.php
index 33622e1a999c0714fcce28f589fca913ba0706e1..73f534d2e28966e23c789a449ad38614cc29a2eb 100644 (file)
@@ -5,12 +5,15 @@ namespace Org\Mxchange\CoreFramework\Factory;
 // Import framework stuff
 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
 
+// Import SPL stuff
+use \InvalidArgumentException;
+
 /**
  * A general (base) factory
  *
  * @author             Roland Haeder <webmaster@shipsimu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2022 Core Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.shipsimu.org
  *
@@ -36,33 +39,41 @@ abstract class BaseFactory extends BaseFrameworkSystem {
        /**
         * Counter of all objects
         */
-       private static $objectCounters = array();
+       private static $objectCounters = [];
 
        /**
         * Protected constructor
         *
-        * @param       $fullClassName  Name of the real class (not BaseFactory)
+        * @param       $className      Name of the real class (not BaseFactory)
         * @return      void
         */
-       protected function __construct ($fullClassName) {
+       protected function __construct (string $className) {
                // Call parent constructor
-               parent::__construct($fullClassName);
+               parent::__construct($className);
        }
 
        /**
         * Count given object
         *
         * @param       $fullClassName  Name of the class we shall count
+        * @return      void
+        * @throws      InvalidArgumentException        If a parameter is not valid
         */
-       protected static final function countObject ($fullClassName) {
+       protected static final function countObject (string $fullClassName) {
+               // Is the parameter valid?
+               if (empty($fullClassName)) {
+                       // No empty class name
+                       throw new InvalidArgumentException('fullClassName is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
+               }
+
                // Count it up in total sum
                self::$total++;
 
                // Do we have an entry?
-               if (!isset(self::$objectCounters[$fullClassName])) {
+               if (!self::isClassCounted($fullClassName)) {
                        // No, then generate one
                        self::$objectCounters[$fullClassName] = 0;
-               } // END - if
+               }
 
                // Count it up again
                //* NOISY-DEBUG: */ print __METHOD__.': className=' .$fullClassName . PHP_EOL;
@@ -87,4 +98,22 @@ abstract class BaseFactory extends BaseFrameworkSystem {
                return self::$objectCounters;
        }
 
+       /**
+        * Checks whether given full class name is already counted
+        *
+        * @param       $fullClassName  Full name of class
+        * @return      $isCounted      Whether given class name is counted
+        * @throws      InvalidArgumentException        If a parameter is not valid
+        */
+       public static final function isClassCounted (string $fullClassName) {
+               // Is the parameter valid?
+               if (empty($fullClassName)) {
+                       // No empty class name
+                       throw new InvalidArgumentException('fullClassName is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
+               }
+
+               // Return isset() result
+               return isset(self::$objectCounters[$fullClassName]);
+       }
+
 }