<commit_before><?php
/**
 * @version 	$Id$
 * @category	Koowa
 * @package		Koowa_Identifier
 * @copyright	Copyright (C) 2007 - 2010 <NAME>. All rights reserved.
 * @license		GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
 */

/**
 * Domain Object Identifier
 *
 * Wraps identifiers of the form [application::]type.package.[.path].name
 * in an object, providing public accessors and methods for derived formats.
 *
 * @author      <NAME> <<EMAIL>>
 * @category    Koowa
 * @package     Koowa_Factory
 * @subpackage  Identifier
 */
class KIdentifier implements KIdentifierInterface
{
    /**
     * The application name
     *
     * @var string
     */
    protected $_application = '';

    /**
     * The identifier type [com|plg|lib|mod]
     * 
     * @var string
     */
    protected $_type = '';
    
    /**
     * The identifier package
     * 
     * @var string
     */
    protected $_package = '';

    /**
     * The identifier path 
     *   
     * @var array
     */
    protected $_path = array();

    /**
     * The identifier object name
     *
     * @var string
     */
    protected $_name = '';
    
    /**
     * The file path
     *
     * @var string
     */
    protected $_filepath = '';
    
    /**
     * The classname
     *
     * @var string
     */
    protected $_classname = '';
    
    /**
     * The identifier
     *
     * @var string
     */
    protected $_identifier = '';
    
    /**
     * Constructor
     *
     * @param   string|object   Identifier string or object in [application::]type.package.[.path].name format
     * @throws  KIndetifierException if the identfier is not valid
     */
    public function __construct($identifier)
    {
        // We also accept objects to allow for auto-cloning
        $identifier = (string) $identifier;
        
        if(strpos($identifier, '.') === FALSE) {
            throw new KIdentifierException('Wrong identifier format : '.$identifier);
        }
        
        //Cache the identifier to increase performance
        $this->_identifier = $identifier;
        
        //Set the application name (if present)
        if(strpos($identifier, '::')) { 
            list($this->_application, $identifier) = explode('::', $identifier);
        }

        //Explode the parts
        $parts = explode('.', $identifier);

        // Set the extension
        $this->_type = array_shift($parts);
        
        // Set the extension
        $this->_package = array_shift($parts);

        // Set the name (last part)
        if(count($parts)) {
            $this->_name = array_pop($parts);
        }

        // Set the path (rest)
        if(count($parts)) {
            $this->_path = $parts;
        }
    }
    
    /** 
     * Implements the virtual class properties
     * 
     * This functions creates a string representation of the identifier.
     * 
     * @param   string  The virtual property to set.
     * @param   string  Set the virtual property to this value.
     */
    public function __set($property, $value)
    {
        if(isset($this->{'_'.$property})) 
        {
            $this->{'_'.$property} = $value;
            
            //Recreate the identifier
            $this->_identifier = '';
            $this->_identifier = (string) $this;
        }
    }
    
    /**
     * Implements access to virtual properties by reference so that it appears to be 
     * a public property.
     * 
     * @param   string  The virtual property to return.
     * @return  array   The value of the virtual property.
     */
    public function &__get($property)
    {
        if(isset($this->{'_'.$property})) {
            return $this->{'_'.$property};
        }
    }
    
    /**
     * This function checks if a virtual property is set.<commit_msg>Added new static KIndentifier::registerApplication() function. Function registers an application and path. An exception is thrown if an application that hasn't been registered is used in an identifier. The application path is being exposed through the new 'basepath' class property.<commit_after><?php
/**
 * @version 	$Id$
 * @category	Koowa
 * @package		Koowa_Identifier
 * @copyright	Copyright (C) 2007 - 2010 <NAME>. All rights reserved.
 * @license		GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
 */

/**
 * Domain Object Identifier
 *
 * Wraps identifiers of the form [application::]type.package.[.path].name
 * in an object, providing public accessors and methods for derived formats.
 *
 * @author      <NAME> <<EMAIL>>
 * @category    Koowa
 * @package     Koowa_Factory
 * @subpackage  Identifier
 */
class KIdentifier implements KIdentifierInterface
{
    /**
     * An associative array of application paths
     * 
     * @var array
     */
    protected static $_applications = array();
    
    /**
     * The identifier
     *
     * @var string
     */
    protected $_identifier = '';
    
    /**
     * The application name
     *
     * @var string
     */
    protected $_application = '';

    /**
     * The identifier type [com|plg|lib|mod]
     * 
     * @var string
     */
    protected $_type = '';
    
    /**
     * The identifier package
     * 
     * @var string
     */
    protected $_package = '';

    /**
     * The identifier path 
     *   
     * @var array
     */
    protected $_path = array();

    /**
     * The identifier object name
     *
     * @var string
     */
    protected $_name = '';
    
    /**
     * The file path
     *
     * @var string
     */
    public $filepath = '';
    
    /**
     * The base path
     *
     * @var string
     */
    public $basepath = '';
    
    /**
     * The classname
     *
     * @var string
     */
    public $classname = '';
    
    /**
     * Constructor
     *
     * @param   string|object   Identifier string or object in [application::]type.package.[.path].name format
     * @throws  KIndetifierException if the identfier is not valid
     */
    public function __construct($identifier)
    {
        // We also accept objects to allow for auto-cloning
        $identifier = (string) $identifier;
        
        //Check if the identifier is valid
        if(strpos($identifier, '.') === FALSE) {
            throw new KIdentifierException('Wrong identifier format : '.$identifier);
        }
         
        //Set the application name
        if(strpos($identifier, '::')) { 
            list($this->application, $identifier) = explode('::', $identifier);
        }

        //Explode the parts
        $parts = explode('.', $identifier);

        // Set the extension
        $this->_type = array_shift($parts);
        
        // Set the extension
        $this->_package = array_shift($parts);

        // Set the name (last part)
        if(count($parts)) {
            $this->_name = array_pop($parts);
        }

        // Set the path (rest)
        if(count($parts)) {
            $this->_path = $parts;
        }
        
         //Cache the identifier to increase performance
        $this->_identifier = $identifier;
    }
    
    /**
	 * Register an application path
	 * 
	 * @param string	The name of the application
	 * @param string	The path of the application
	 * return void
     */
    public static function registerApplication($application, $path)
    {
        self::$_applications[$application] = $path;
    }
        
    /** 
     * Implements the virtual class properties
     * 
     * This functions creates a string representation of the identifier.
     * 
     * @param   string  The virtual property to set.
     * @param   string  Set the virtual property to this value.
     */
    public function __set($property, $value)
    {
        if(isset($this->{'_'.$property})) 
        {
            $this->{'_'.$property} = $value;
            
            //Set the basepath
            if($property == 'application')
            { 
               if(!isset(self::$_applications[$value])) {
                    throw new KIdentifierException('Unknow application : '.$value);  
               }
               
               $this->basepath = self::$_applications[$value];
            }
                
            //Force recreation of the identifier string
            $this->_identifier = '';
        }
    }
    
    /**
     * Implements access to virtual properties by reference so that it appears to be 
     * a public property.
     * 
     * @param   string  The virtual property to return.
     * @return  array   The value of the virtual property.
     */
    public function &__get($property)
    {
        if(isset($this->{'_'.$property})) {
            return $this->{'_'.$property};
        }
    }
    
    /**
     * This function checks if a virtual property is set.
{'fraction_non_alphanumeric': 0.12135810965817848, 'fraction_numerical': 0.0020646937370956643, 'mean_word_length': 1.7811802232854865, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2003752', 'n_tokens_mistral': 2412, 'n_tokens_neox': 2342, 'n_words': 680}