3 namespace Org\Mxchange\CoreFramework\Resolver\Action;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
7 use Org\Mxchange\CoreFramework\Resolver\BaseResolver;
8 use Org\Mxchange\CoreFramework\Utils\String\StringUtils;
11 use \InvalidArgumentException;
14 * A generic action resolver class
16 * @author Roland Haeder <webmaster@shipsimu.org>
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
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.
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.
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/>.
35 abstract class BaseActionResolver extends BaseResolver {
37 * Validated action name
39 private $actionName = '';
42 * Protected constructor
44 * @param $className Name of the class
47 protected function __construct (string $className) {
48 // Call parent constructor
49 parent::__construct($className);
53 * Setter for action name
55 * @param $actionName Last validated action name
58 protected final function setActionName (string $actionName) {
59 $this->actionName = $actionName;
63 * Getter for action name
65 * @return $actionName Last validated action name
67 public final function getActionName () {
68 return $this->actionName;
72 * Checks whether the given action is valid
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
79 public function isActionValid (string $namespace, string $actionName) {
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');
93 $this->getCapitalizedClassPrefix(),
94 StringUtils::convertToClassName($actionName)
97 // Now, let us create the full name of the action class
98 $this->setClassName($className);
100 // Is this class already loaded?
101 $isValid = class_exists($this->getClassName());
106 $this->setActionName($actionName);
114 * "Loads" current action and instances it if not yet cached
116 * @return $actionInstance A loaded action instance
118 protected function loadAction () {
119 // Init action instance
120 $actionInstance = NULL;
123 $className = sprintf(
125 $this->getNamespace(),
126 $this->getCapitalizedClassPrefix(),
127 StringUtils::convertToClassName($actionName)
131 $this->setClassName($className);
133 // Initiate the action
134 $actionInstance = ObjectFactory::createObjectByName($this->getClassName(), array($this));
137 return $actionInstance;