]> git.mxchange.org Git - core.git/blobdiff - inc/main/classes/factories/objects/class_ObjectFactory.php
renamed lib-local.php -> lib-lfdb.php because it really loads the "legendary"
[core.git] / inc / main / classes / factories / objects / class_ObjectFactory.php
index 2a5a46ef4b7ba8db105c1daca8b2bd306d2653cb..46d2b6e02ba8b133887cee21acfdf776fbcb1324 100644 (file)
@@ -4,6 +4,8 @@ namespace CoreFramework\Factory;
 
 // Import framework stuff
 use CoreFramework\Configuration\FrameworkConfiguration;
+use CoreFramework\Generic\EmptyVariableException;
+use CoreFramework\Loader\NoClassException;
 
 /**
  * An general object factory
@@ -64,7 +66,7 @@ class ObjectFactory extends BaseFactory {
                }
 
                // Create method name
-               $methodName = sprintf('create%s', $className);
+               $methodName = sprintf('create%s', self::stripNamespaceFromClassName($className));
 
                // Run the user function
                $objectInstance = call_user_func_array(array($className, $methodName), $args);
@@ -94,4 +96,30 @@ class ObjectFactory extends BaseFactory {
                return $objectInstance;
        }
 
+       /**
+        * Strips of namespace from given class name. An exception is thrown when
+        * there is no namespace found as this is the now the norm for any objects
+        * being created with this object factory.
+        *
+        * @param       $fullClassName  Class name with namespace
+        * @return      $shortClassName Stripped class name (no namespace)
+        * @throws      InvalidClassNameException       If the class name does not follow naming convention
+        */
+       private static function stripNamespaceFromClassName ($fullClassName) {
+               // The class name should contain at least 2 back-slashes, so split at them
+               $classNameParts = explode("\\", $fullClassName);
+
+               // At least 3 parts should be there
+               if (count($classNameParts) < 3) {
+                       // Namespace scheme is: Project\Package[\SubPackage...]
+                       throw new InvalidClassNameException($fullClassName, self::EXCEPTION_INVALID_CLASS_NAME);
+               } // END - if
+
+               // Get last element
+               $shortClassName = array_pop($classNameParts);
+
+               // Return it
+               return $shortClassName;
+       }
+
 }