<?php
/********************************************************************************
 * (c)1337 aheadware.com - All rights reserved
 ********************************************************************************/

/**
 * Class AHDB_Core_Locator
 *
 * Locator Class used with AHDB_Core_Autoloader
 *
 */
class AHDB_Core_Locator {

    public $prefix = NULL;
    protected $basepath = NULL;
    protected $suffix = '.php';

    /**
     * Create locator for AHDB_Core_Autoloader.
     *
     * Example:
     * Class has filename like
     * A_B_C
     *
     * Path of class:
     * src/B/A_B_C.php
     *
     * Appropritae locator:
     * $l = new AHDB_Core_Locator( 'A', 'src', '.php' );
     *
     * @param string $prefix Prefix (ie. unique id) for locator
     * @param string $basepath Basepath of source code hierarchy
     * @param string string $suffix (optional) default is '.php'
     * @throws Exception
     */
    public function __construct( $prefix, $basepath, $suffix = '.php' ) {
        // Validate
        if ( !$prefix || !$basepath ) {
            throw new Exception( "Invalid params." );
        }

        $this->prefix = '' . ucfirst( $prefix );
        $this->basepath = '' . rtrim( $basepath, '/' );
        $this->suffix = $suffix;
    }

    /**
     * Called by autoloader
     *
     * @param string $className
     * @return string Correct path of given class
     */
    public function resolvePath( $className ) {
        $pathParts = explode( '_', $className );
        $path = '';
        for( $i = 1; $i < count( $pathParts )-1; $i++ ) {
            $path .= '/' . strtolower( $pathParts[ $i ] );
        }

        return $this->basepath . $path . '/' . $className . '.php';
    }
} 