]> git.mxchange.org Git - core.git/blob - framework/main/classes/factories/class_BaseFactory.php
73f534d2e28966e23c789a449ad38614cc29a2eb
[core.git] / framework / main / classes / factories / class_BaseFactory.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Factory;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
7
8 // Import SPL stuff
9 use \InvalidArgumentException;
10
11 /**
12  * A general (base) factory
13  *
14  * @author              Roland Haeder <webmaster@shipsimu.org>
15  * @version             0.0.0
16  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2022 Core Developer Team
17  * @license             GNU GPL 3.0 or any newer version
18  * @link                http://www.shipsimu.org
19  *
20  * This program is free software: you can redistribute it and/or modify
21  * it under the terms of the GNU General Public License as published by
22  * the Free Software Foundation, either version 3 of the License, or
23  * (at your option) any later version.
24  *
25  * This program is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28  * GNU General Public License for more details.
29  *
30  * You should have received a copy of the GNU General Public License
31  * along with this program. If not, see <http://www.gnu.org/licenses/>.
32  */
33 abstract class BaseFactory extends BaseFrameworkSystem {
34         /**
35          * Total objects generated
36          */
37         private static $total = 0;
38
39         /**
40          * Counter of all objects
41          */
42         private static $objectCounters = [];
43
44         /**
45          * Protected constructor
46          *
47          * @param       $className      Name of the real class (not BaseFactory)
48          * @return      void
49          */
50         protected function __construct (string $className) {
51                 // Call parent constructor
52                 parent::__construct($className);
53         }
54
55         /**
56          * Count given object
57          *
58          * @param       $fullClassName  Name of the class we shall count
59          * @return      void
60          * @throws      InvalidArgumentException        If a parameter is not valid
61          */
62         protected static final function countObject (string $fullClassName) {
63                 // Is the parameter valid?
64                 if (empty($fullClassName)) {
65                         // No empty class name
66                         throw new InvalidArgumentException('fullClassName is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
67                 }
68
69                 // Count it up in total sum
70                 self::$total++;
71
72                 // Do we have an entry?
73                 if (!self::isClassCounted($fullClassName)) {
74                         // No, then generate one
75                         self::$objectCounters[$fullClassName] = 0;
76                 }
77
78                 // Count it up again
79                 //* NOISY-DEBUG: */ print __METHOD__.': className=' .$fullClassName . PHP_EOL;
80                 self::$objectCounters[$fullClassName]++;
81         }
82
83         /**
84          * Static getter for total object count
85          *
86          * @return      $total  Total amount of generated objects
87          */
88         public static final function getTotal () {
89                 return self::$total;
90         }
91
92         /**
93          * Static getter for all object counters
94          *
95          * @return      $objectCounters         An array with all object counters
96          */
97         public static final function getAllCounters () {
98                 return self::$objectCounters;
99         }
100
101         /**
102          * Checks whether given full class name is already counted
103          *
104          * @param       $fullClassName  Full name of class
105          * @return      $isCounted      Whether given class name is counted
106          * @throws      InvalidArgumentException        If a parameter is not valid
107          */
108         public static final function isClassCounted (string $fullClassName) {
109                 // Is the parameter valid?
110                 if (empty($fullClassName)) {
111                         // No empty class name
112                         throw new InvalidArgumentException('fullClassName is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
113                 }
114
115                 // Return isset() result
116                 return isset(self::$objectCounters[$fullClassName]);
117         }
118
119 }