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\ObjectFactory;
7 use Org\Mxchange\CoreFramework\Resolver\BaseResolver;
8
9 // Import SPL stuff
10 use \InvalidArgumentException;
11
12 /**
13  * A generic action resolver class
14  *
15  * @author              Roland Haeder <webmaster@shipsimu.org>
16  * @version             0.0.0
17  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 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 BaseActionResolver extends BaseResolver {
35         /**
36          * Validated action name
37          */
38         private $actionName = '';
39
40         /**
41          * Protected constructor
42          *
43          * @param       $className      Name of the class
44          * @return      void
45          */
46         protected function __construct ($className) {
47                 // Call parent constructor
48                 parent::__construct($className);
49         }
50
51         /**
52          * Setter for action name
53          *
54          * @param       $actionName     Last validated action name
55          * @return      void
56          */
57         protected final function setActionName ($actionName) {
58                 $this->actionName = (string) $actionName;
59         }
60
61         /**
62          * Getter for action name
63          *
64          * @return      $actionName     Last validated action name
65          */
66         public final function getActionName () {
67                 return $this->actionName;
68         }
69
70         /**
71          * Checks whether the given action is valid
72          *
73          * @param       $namespace              Namespace to look in
74          * @param       $actionName             The default action we shall execute
75          * @return      $isValid                Whether the given action is valid
76          * @throws      InvalidArgumentException        Thrown if given action is not set
77          */
78         public function isActionValid ($namespace, $actionName) {
79                 // By default nothing shall be valid
80                 $isValid = false;
81
82                 // Is a action set?
83                 if (empty($namespace)) {
84                         // Then thrown an exception here
85                         throw new InvalidArgumentException('Parameter "namespace" is empty');
86                 } elseif (empty($actionName)) {
87                         // Then thrown an exception here
88                         throw new InvalidArgumentException('Parameter "actionName" is empty');
89                 }
90
91                 // Create class name
92                 $className = sprintf(
93                         '%s\%s%sAction',
94                         $namespace,
95                         $this->getCapitalizedClassPrefix(),
96                         self::convertToClassName($actionName)
97                 );
98
99                 // Now, let us create the full name of the action class
100                 $this->setClassName($className);
101
102                 // Is this class already loaded?
103                 if (class_exists($this->getClassName())) {
104                         // This class does exist. :-)
105                         $isValid = true;
106                 } // END - if
107
108                 // Set action name
109                 $this->setActionName($actionName);
110
111                 // Return the result
112                 return $isValid;
113         }
114
115         /**
116          * "Loads" current action and instances it if not yet cached
117          *
118          * @return      $actionInstance                 A loaded action instance
119          */
120         protected function loadAction () {
121                 // Init action instance
122                 $actionInstance = NULL;
123
124                 // Create class name
125                 $className = sprintf(
126                         '%s\%s%sAction',
127                         $this->getNamespace(),
128                         $this->getCapitalizedClassPrefix(),
129                         self::convertToClassName($actionName)
130                 );
131
132                 // ... and set it
133                 $this->setClassName($className);
134
135                 // Initiate the action
136                 $actionInstance = ObjectFactory::createObjectByName($this->getClassName(), array($this));
137
138                 // Return the result
139                 return $actionInstance;
140         }
141
142 }