Continued:
[core.git] / framework / main / classes / resolver / action / class_BaseActionResolver.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Resolver\Action;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
7 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
8 use Org\Mxchange\CoreFramework\Resolver\BaseResolver;
9 use Org\Mxchange\CoreFramework\Utils\Strings\StringUtils;
10
11 // Import SPL stuff
12 use \InvalidArgumentException;
13
14 /**
15  * A generic action resolver class
16  *
17  * @author              Roland Haeder <webmaster@shipsimu.org>
18  * @version             0.0.0
19  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
20  * @license             GNU GPL 3.0 or any newer version
21  * @link                http://www.shipsimu.org
22  *
23  * This program is free software: you can redistribute it and/or modify
24  * it under the terms of the GNU General Public License as published by
25  * the Free Software Foundation, either version 3 of the License, or
26  * (at your option) any later version.
27  *
28  * This program is distributed in the hope that it will be useful,
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31  * GNU General Public License for more details.
32  *
33  * You should have received a copy of the GNU General Public License
34  * along with this program. If not, see <http://www.gnu.org/licenses/>.
35  */
36 abstract class BaseActionResolver extends BaseResolver {
37         /**
38          * Validated action name
39          */
40         private $actionName = '';
41
42         /**
43          * Protected constructor
44          *
45          * @param       $className      Name of the class
46          * @return      void
47          */
48         protected function __construct (string $className) {
49                 // Call parent constructor
50                 parent::__construct($className);
51         }
52
53         /**
54          * Setter for action name
55          *
56          * @param       $actionName     Last validated action name
57          * @return      void
58          */
59         protected final function setActionName (string $actionName) {
60                 $this->actionName = $actionName;
61         }
62
63         /**
64          * Getter for action name
65          *
66          * @return      $actionName     Last validated action name
67          */
68         public final function getActionName () {
69                 return $this->actionName;
70         }
71
72         /**
73          * Checks whether the given action is valid
74          *
75          * @param       $namespace              Namespace to look in
76          * @param       $actionName             The default action we shall execute
77          * @return      $isValid                Whether the given action is valid
78          * @throws      InvalidArgumentException        Thrown if given action is not set
79          */
80         public function isActionValid (string $namespace, string $actionName) {
81                 // Is a action set?
82                 if (empty($namespace)) {
83                         // Then thrown an exception here
84                         throw new InvalidArgumentException('Parameter "namespace" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
85                 } elseif (empty($actionName)) {
86                         // Then thrown an exception here
87                         throw new InvalidArgumentException('Parameter "actionName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
88                 }
89
90                 // Create class name
91                 $className = sprintf(
92                         '%s\%s%sAction',
93                         $namespace,
94                         $this->getCapitalizedClassPrefix(),
95                         StringUtils::convertToClassName($actionName)
96                 );
97
98                 // Now, let us create the full name of the action class
99                 $this->setClassName($className);
100
101                 // Is this class already loaded?
102                 $isValid = class_exists($this->getClassName());
103
104                 // Is it valid?
105                 if ($isValid) {
106                         // Set action name
107                         $this->setActionName($actionName);
108                 }
109
110                 // Return the result
111                 return $isValid;
112         }
113
114         /**
115          * "Loads" current action and instances it if not yet cached
116          *
117          * @return      $actionInstance                 A loaded action instance
118          */
119         protected function loadAction () {
120                 // Init action instance
121                 $actionInstance = NULL;
122
123                 // Create class name
124                 $className = sprintf(
125                         '%s\%s%sAction',
126                         $this->getNamespace(),
127                         $this->getCapitalizedClassPrefix(),
128                         StringUtils::convertToClassName($actionName)
129                 );
130
131                 // ... and set it
132                 $this->setClassName($className);
133
134                 // Initiate the action
135                 $actionInstance = ObjectFactory::createObjectByName($this->getClassName(), [$this]);
136
137                 // Return the result
138                 return $actionInstance;
139         }
140
141 }