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