You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
4.7 KiB
PHTML

<?php
namespace Artmark\Assets;
/**
* Description of Asset
*
* @author Andrey Pokidov <pokidov@e-traffic.ru>
*/
class Asset
{
const TYPE_JAVASCRIPT = 'js';
const TYPE_CSS = 'css';
const POSITION_HEAD = 'head';
const POSITION_TOP = 'top';
const POSITION_BOTTOM = 'bottom';
private $name = '';
private $type = self::TYPE_JAVASCRIPT;
private $position = self::POSITION_HEAD;
private $url = '';
private $version = '';
/**
* Создаёт новый объекс Script, описывающий данные о скрипте
* @param string $url URL скрипта
* @param string|int $version Версия скрипта
* @param string $type Тип скрипта, может принимать одно из значений Script::TYPE_JAVASCRIPT или Script::TYPE_CSS
* @param string $position Место скрипта в коде страницы, может принимать одно из значений:
* <ul>
* <li>Script::POSITION_HEAD в секции &lt;head&gt;...&lt;/head&gt;</li>
* <li>Script::POSITION_TOP сразу после открывающего тега &lt;body&gt;</li>
* <li>Script::POSITION_BOTTOM непосредственно перед закрывающим тегом &lt;/body&gt;</li>
* </ul>
*/
public function __construct($name, $url, $version, $type = self::TYPE_JAVASCRIPT, $position = self::POSITION_HEAD)
{
self::checkName($name);
self::checkUrl($url);
self::checkVersion($url);
self::checkAssetType($type);
self::checkAssetPosition($position);
$this->name = self::correctName($name);
$this->url = trim($url);
$this->version = is_string($version) ? trim($version) : $version;
$this->type = $type;
$this->position = $position;
}
public function name()
{
return $this->name;
}
public function type()
{
return $this->type;
}
public function isJavaScript()
{
return $this->type == self::TYPE_JAVASCRIPT;
}
public function isCss()
{
return $this->type == self::TYPE_CSS;
}
public function position()
{
return $this->position;
}
public function isAtHead()
{
return $this->position == self::POSITION_HEAD;
}
public function isAtTop()
{
return $this->position == self::POSITION_TOP;
}
public function isAtBottom()
{
return $this->position == self::POSITION_BOTTOM;
}
public function url()
{
return $this->url;
}
public function urlWithVersion()
{
if (!$this->hasVersion())
{
return $this->url;
}
return $this->url . (strpos($this->url, '?') === false ? '?' : '&') . 'v=' . urlencode($this->version);
}
public function hasVersion()
{
return !empty($this->version);
}
public function version()
{
return $this->version;
}
public static function correctName($name)
{
return strtolower(trim(strval($name)));
}
public static function checkName($name)
{
if (!is_string($name)) {
throw new AssetException('Incorrect name type: `' . gettype($name) . '`, the name of an asset must be a string');
}
if (trim($name) == '') {
throw new AssetException('The name of an asset cannot be an empty string');
}
}
public static function checkUrl($url)
{
if (!is_string($url)) {
throw new AssetException('Incorrect URL type: `' . gettype($url) . '`, the URL of an asset must be a string');
}
if (trim($url) == '') {
throw new AssetException('The URL of an asset cannot be an empty string');
}
}
public static function checkVersion($verison)
{
if (!is_string($verison) && !is_int($verison)) {
throw new AssetException('Incorrect version type: `' . gettype($verison) . '`, the version of an asset must be a string or an integer number');
}
}
public static function checkAssetType($type)
{
if ($type != self::TYPE_JAVASCRIPT && $type != self::TYPE_CSS) {
throw new AssetException('Incorrect asset type: `' . $type . '`');
}
}
public static function checkAssetPosition($position)
{
if ($position != self::POSITION_HEAD && $position != self::POSITION_TOP && $position != self::POSITION_BOTTOM) {
throw new AssetException('Incorrect asset position: `' . $position . '`');
}
}
}