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