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