<commit_before><?php

/**
 * @copyright Copyright (c) 2016 <NAME>
 * @link http://junaidatari.com Website
 * @see http://www.github.com/blacksmoke26/yii2-cdn
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 */

namespace yii2cdn;

use yii\base\InvalidParamException;
use yii\base\InvalidValueException;
use yii\base\UnknownPropertyException;
use yii\helpers\ArrayHelper;
use yii2cdn\traits\Url;
use yii2cdn\traits\File;
use yii2cdn\traits\Attributes;

/**
 * Yii2 CDN Component object
 *
 * @package yii2cdn
 * @author <NAME> <<EMAIL>>
 *
 * @access public
 * @version 0.1
 */
class Component {
	/**
	 * Used traits
	 */
	use Url;
	use File;
	use Attributes;

	/**
	 * Component ID
	 * @var string
	 */
	protected $id;

	/**
	 * Component's base url
	 * @var string
	 */
	protected $baseUrl;

	/**
	 * Component's base path
	 * @var string
	 */
	protected $basePath;

	/**
	 * Component attributes
	 * @var array
	 */
	protected $attributes = [];

	/**
	 * Sections list
	 * @var Section[]
	 */
	protected $sections = [];

	/**
	 * Component constructor.
	 *
	 * @param string $id Component ID
	 * @param array $config Configuration
	 */
	public function __construct ( array $config ) {
		$this->baseUrl = $config['baseUrl'];
		$this->basePath = $config['basePath'];
		$this->attributes = (array) $config['componentAttributes'];
		$this->id = $config['id'];
		$this->buildSections($config);
	}

	/**
	 * Create the component section object from config<br>
	 * Note: Empty sections will be removed
	 *
	 * @param array $config Configuration object
	 */
	protected function buildSections ( array $config ) {
		foreach ( $config['sections'] as $name ) {

			if ( !array_key_exists($name, $config) || empty($config[$name]) ) {
				continue;
			}

			$faName = isset($config['fileAttrs'][$name])
				? $config['fileAttrs'][$name]
				: [];

			$_attributes = isset ($config['fileAttrs'])
				? $faName
				: [];

			/// Section attributes
			/** @var array $sectAttributes */
			$sectAttributes = (array) $config['sectionsAttributes'][$name];

			// Defined all sections attributes
			if (  $this->getAttr('@sectionsAttrs') !== null ) {
				$sectAttributes = ArrayHelper::merge((array) $this->getAttr('@sectionsAttrs'), $sectAttributes);
			}

			$hasSectionUrlName = true;

			// (@hideNameInPathUrls) Source directory url
			if ( isset($sectAttributes['noNameInPathUrls']) && $sectAttributes['noNameInPathUrls'] ) {
				$hasSectionUrlName = false;
			}

			$basePath = isset($sectAttributes['src']) && trim($sectAttributes['src'])
				? rtrim($this->basePath, '\\/') .DIRECTORY_SEPARATOR . ltrim($sectAttributes['src'],'\\/')
				: $this->basePath . ($hasSectionUrlName ? DIRECTORY_SEPARATOR . $name : '');

			$baseUrl = isset($sectAttributes['src']) && trim($sectAttributes['src'])
				? rtrim($this->baseUrl, '/') . '/' . ltrim($sectAttributes['src'],'\\/')
				: $this->baseUrl . ($hasSectionUrlName ? "/{$name}" : '');

			// Create section(s) component
			/** @var Section $section */			
			$this->sections[$name] = \Yii::createObject($config['sectionClass'], [[
				'component' => $this->id,
				'section' =>$name,
				'files' => $config[$name],
				'baseUrl' => $baseUrl,
				'basePath' => str_replace('/', DIRECTORY_SEPARATOR, $basePath),
				'attributes' => $sectAttributes,
				'fileAttributes' => $_attributes,
				'fileClass' => $config['fileClass'],
				'preComponents' => $config['preComponents']
			]]);

			unset($config['preComponents']);
		}

		// Remove sections attributes
		unset($this->attributes['@sectionsAttrs']);
	}

	/**
	 * Get a javaScript files list
	 * @param boolean $asUrl (optional) True will return files url only (default: false)
	 * @param boolean $unique (optional) True will remove duplicate elements (default: true)
	 * @return array List of js section files
	 */
	public function getJsFiles ( $asUrl = false, $unique = true ) {
		$files = $this->getSection ( 'js' )->getFiles ( true, $unique );

		if ( !$asUrl ) {
			return $files;
		}

		return array_values ( $files );
	}

	/**
	 * Get a section(s) object by ID
	 * @param string|array Section name|List of sections name
	 * @param boolean $throwException (optional) True will throw exception when section name not found (default: true)
	 * @throws \yii\base\InvalidParamException When null given as section
	 * @throws \yii\base\UnknownPropertyException When section name not found
	 * @return Section|array|null Section | Sections List | Null when not found
	 */
	public function getSection ( $name, $throwException = true ) {
		/** @var array $sections */
		$sections = $name;

		if ( !is_array($name) ) {
			$sections = [$name];
		}

		$list = [];

		foreach ( $sections as $name ) {
			if ( !$this->sectionExists($name, $throwException ) ) {
				continue;
			}

			$list[] = $this->sections[$name];
		}

		return count($list) == 1 ? array_shift($list) : $list;
	}

	/**
	 * Check that given section exists
	 * @param string $name Section name to check
	 * @param boolean $throwException (optional) True will throw exception when section name not found (default: false)
	 * @throws \yii\base\InvalidValueException When section name is empty
	 * @throws \yii\base\UnknownPropertyException When section name not found
	 * @return True on exist | False when not found
	 */
	public function sectionExists ( $name, $throwException = true ) {
		if ( empty($name) ) {
			if ( $throwException ) {
				throw new InvalidValueException ('Section name cannot be empty');
			}

			return false;
		}<commit_msg>fixed bug: none of the inheritable attributes exists fixed bug: functional attribute 'noNameInPathUrls' url was wrong.<commit_after><?php

/**
 * @copyright Copyright (c) 2016 <NAME>
 * @link http://junaidatari.com Website
 * @see http://www.github.com/blacksmoke26/yii2-cdn
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 */

namespace yii2cdn;

use yii\base\InvalidParamException;
use yii\base\InvalidValueException;
use yii\base\UnknownPropertyException;
use yii\helpers\ArrayHelper;
use yii2cdn\traits\Url;
use yii2cdn\traits\File;
use yii2cdn\traits\Attributes;

/**
 * Yii2 CDN Component object
 *
 * @package yii2cdn
 * @author <NAME> <<EMAIL>>
 *
 * @access public
 * @version 0.1
 */
class Component {
	/**
	 * Used traits
	 */
	use Url;
	use File;
	use Attributes;

	/**
	 * Component ID
	 * @var string
	 */
	protected $id;

	/**
	 * Component's base url
	 * @var string
	 */
	protected $baseUrl;

	/**
	 * Component's base path
	 * @var string
	 */
	protected $basePath;

	/**
	 * Component attributes
	 * @var array
	 */
	protected $attributes = [];

	/**
	 * Sections list
	 * @var Section[]
	 */
	protected $sections = [];

	/**
	 * Component constructor.
	 *
	 * @param string $id Component ID
	 * @param array $config Configuration
	 */
	public function __construct ( array $config ) {
		$this->baseUrl = $config['baseUrl'];
		$this->basePath = $config['basePath'];
		$this->attributes = (array) $config['componentAttributes'];
		$this->id = $config['id'];
		$this->buildSections($config);
	}

	/**
	 * Create the component section object from config<br>
	 * Note: Empty sections will be removed
	 *
	 * @param array $config Configuration object
	 */
	protected function buildSections ( array $config ) {
		foreach ( $config['sections'] as $name ) {

			if ( !array_key_exists($name, $config) || empty($config[$name]) ) {
				continue;
			}

			$noNameInPathUrls = false;

			/// Section attributes
			/** @var array $sectAttributes */
			$sectAttributes = (array) $config['sectionsAttributes'][$name];

			// Defined all sections attributes
			if (  $this->getAttr('@sectionsAttrs') !== null ) {
				$sectAttributes = ArrayHelper::merge((array) $this->getAttr('@sectionsAttrs'), $sectAttributes);
			}

			$faName = isset($config['fileAttrs'][$name])
				? $config['fileAttrs'][$name]
				: [];

			$_attributes = isset ($config['fileAttrs'])
				? $faName
				: [];

			// (@hideNameInPathUrls) Source directory url
			if ( isset($sectAttributes['noNameInPathUrls']) ) {
				$noNameInPathUrls = $sectAttributes['noNameInPathUrls'];
			}

			$basePath = isset($sectAttributes['src']) && trim($sectAttributes['src'])
				? rtrim($this->basePath, '\\/') .DIRECTORY_SEPARATOR . ltrim($sectAttributes['src'],'\\/')
				: $this->basePath . (!$noNameInPathUrls ? DIRECTORY_SEPARATOR . $name : '');

			$baseUrl = isset($sectAttributes['src']) && trim($sectAttributes['src'])
				? rtrim($this->baseUrl, '/') . '/' . ltrim($sectAttributes['src'],'\\/')
				: $this->baseUrl . (!$noNameInPathUrls ? "/{$name}" : '');

			// Create section(s) component
			/** @var Section $section */			
			$this->sections[$name] = \Yii::createObject($config['sectionClass'], [[
				'component' => $this->id,
				'section' =>$name,
				'files' => $config[$name],
				'baseUrl' => $baseUrl,
				'basePath' => str_replace('/', DIRECTORY_SEPARATOR, $basePath),
				'attributes' => $sectAttributes,
				'fileAttributes' => $_attributes,
				'fileClass' => $config['fileClass'],
				'preComponents' => $config['preComponents']
			]]);

			unset($config['preComponents']);
		}

		// Remove sections attributes
		unset($this->attributes['@sectionsAttrs']);
	}

	/**
	 * Get a javaScript files list
	 * @param boolean $asUrl (optional) True will return files url only (default: false)
	 * @param boolean $unique (optional) True will remove duplicate elements (default: true)
	 * @return array List of js section files
	 */
	public function getJsFiles ( $asUrl = false, $unique = true ) {
		$files = $this->getSection ( 'js' )->getFiles ( true, $unique );

		if ( !$asUrl ) {
			return $files;
		}

		return array_values ( $files );
	}

	/**
	 * Get a section(s) object by ID
	 * @param string|array Section name|List of sections name
	 * @param boolean $throwException (optional) True will throw exception when section name not found (default: true)
	 * @throws \yii\base\InvalidParamException When null given as section
	 * @throws \yii\base\UnknownPropertyException When section name not found
	 * @return Section|array|null Section | Sections List | Null when not found
	 */
	public function getSection ( $name, $throwException = true ) {
		/** @var array $sections */
		$sections = $name;

		if ( !is_array($name) ) {
			$sections = [$name];
		}

		$list = [];

		foreach ( $sections as $section ) {
			if ( !$this->sectionExists($section, $throwException ) ) {
				continue;
			}

			$list[] = $this->sections[$section];
		}

		return count($list) == 1
			? array_shift($list)
			: $list;
	}

	/**
	 * Check that given section exists
	 * @param string $name Section name to check
	 * @param boolean $throwException (optional) True will throw exception when section name not found (default: false)
	 * @throws \yii\base\InvalidValueException When section name is empty
	 * @throws \yii\base\UnknownPropertyException When section name not found
	 * @return True on exist | False when not found
	 */
	public function sectionExists ( $name, $throwException = true ) {
		if ( empty($name) ) {
			if ( $throwException ) {
				throw new InvalidValueException ('Section name cannot be empty');
			}

			return false;
		}
{'fraction_non_alphanumeric': 0.15946575833632126, 'fraction_numerical': 0.0028684116170670493, 'mean_word_length': 3.689785624211854, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 67, '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': '575680', 'n_tokens_mistral': 3887, 'n_tokens_neox': 3399, 'n_words': 894}