3 namespace Org\Mxchange\CoreFramework\Resolver\Action;
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;
12 use \InvalidArgumentException;
15 * A generic action resolver class
17 * @author Roland Haeder <webmaster@shipsimu.org>
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
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.
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.
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/>.
36 abstract class BaseActionResolver extends BaseResolver {
38 * Validated action name
40 private $actionName = '';
43 * Protected constructor
45 * @param $className Name of the class
48 protected function __construct (string $className) {
49 // Call parent constructor
50 parent::__construct($className);
54 * Setter for action name
56 * @param $actionName Last validated action name
59 protected final function setActionName (string $actionName) {
60 $this->actionName = $actionName;
64 * Getter for action name
66 * @return $actionName Last validated action name
68 public final function getActionName () {
69 return $this->actionName;
73 * Checks whether the given action is valid
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
80 public function isActionValid (string $namespace, string $actionName) {
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);
94 $this->getCapitalizedClassPrefix(),
95 StringUtils::convertToClassName($actionName)
98 // Now, let us create the full name of the action class
99 $this->setClassName($className);
101 // Is this class already loaded?
102 $isValid = class_exists($this->getClassName());
107 $this->setActionName($actionName);
115 * "Loads" current action and instances it if not yet cached
117 * @return $actionInstance A loaded action instance
119 protected function loadAction () {
120 // Init action instance
121 $actionInstance = NULL;
124 $className = sprintf(
126 $this->getNamespace(),
127 $this->getCapitalizedClassPrefix(),
128 StringUtils::convertToClassName($actionName)
132 $this->setClassName($className);
134 // Initiate the action
135 $actionInstance = ObjectFactory::createObjectByName($this->getClassName(), [$this]);
138 return $actionInstance;