Back to FormHelper class

Method loadClass

protected static string|bool
loadClass
(mixed $entity, mixed $type)
Load a class for one of the form's entities of a particular type.
Parameters
  • string $entity One of the form entities (field or rule).
  • string $type Type of an entity.
Returns
  • string|bool Class name on success or false otherwise.
Since
  • 1.7.0
Class: FormHelper
Project: Joomla

Method loadClass - Source code

/**
 * Load a class for one of the form's entities of a particular type.
 * Currently, it makes sense to use this method for the "field" and "rule" entities
 * (but you can support more entities in your subclass).
 *
 * @param   string  $entity  One of the form entities (field or rule).
 * @param   string  $type    Type of an entity.
 *
 * @return  string|boolean  Class name on success or false otherwise.
 *
 * @since   1.7.0
 */
protected static function loadClass($entity, $type)
{
    // Check if there is a class in the registered namespaces
    foreach (self::addPrefix($entity) as $prefix) {
        // Treat underscores as namespace
        $name = Normalise::toSpaceSeparated($type);
        $name = str_ireplace(' ', '\\', ucwords($name));
        $subPrefix = '';
        if (strpos($name, '.')) {
            list($subPrefix, $name) = explode('.', $name);
            $subPrefix = ucfirst($subPrefix) . '\\';
        }
        // Compile the classname
        $class = rtrim($prefix, '\\') . '\\' . $subPrefix . ucfirst($name) . ucfirst($entity);
        // Check if the class exists
        if (class_exists($class)) {
            return $class;
        }
    }
    $prefix = 'J';
    if (strpos($type, '.')) {
        list($prefix, $type) = explode('.', $type);
    }
    $class = StringHelper::ucfirst($prefix, '_') . 'Form' . StringHelper::ucfirst($entity, '_') . StringHelper::ucfirst($type, '_');
    if (class_exists($class)) {
        return $class;
    }
    // Get the field search path array.
    $paths = self::addPath($entity);
    // If the type is complex, add the base type to the paths.
    if ($pos = strpos($type, '_')) {
        // Add the complex type prefix to the paths.
        for ($i = 0, $n = \count($paths); $i < $n; $i++) {
            // Derive the new path.
            $path = $paths[$i] . '/' . strtolower(substr($type, 0, $pos));
            // If the path does not exist, add it.
            if (!\in_array($path, $paths)) {
                $paths[] = $path;
            }
        }
        // Break off the end of the complex type.
        $type = substr($type, $pos + 1);
    }
    // Try to find the class file.
    $type = strtolower($type) . '.php';
    foreach ($paths as $path) {
        $file = Path::find($path, $type);
        if (!$file) {
            continue;
        }
        require_once $file;
        if (class_exists($class)) {
            break;
        }
    }
    // Check for all if the class exists.
    return class_exists($class) ? $class : false;
}