PHP code example of vinogradsoft / compass
1. Go to this page and download the library: Download vinogradsoft/compass library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
vinogradsoft / compass example snippets
Compass\Url;
$url = Url::createBlank();
$url->setScheme('http')->setUser('grigor')->setPassword('password')->setHost('vinograd.soft')
->setPort('8080')->setPath('/path/to/resource')->setSuffix('.json')
->setArrayQuery(['query' => 'value',])->setFragment('fragment');
$url->updateSource();
echo '<br><br><b>Authority:</b> ', $url->getAuthority();
echo '<br><b>Base Url:</b> ', $url->getBaseUrl();
echo '<br><b>Relative Url:</b> ', $url->getRelativeUrl();
echo '<br><b>Absolute Url:</b> ', $url->getSource(), '<br>';
echo '<br><b>Scheme:</b> ', $url->getScheme();
echo '<br><b>User:</b> ', $url->getUser();
echo '<br><b>Password:</b> ', $url->getPassword();
echo '<br><b>Host:</b> ', $url->getHost();
echo '<br><b>Port:</b> ', $url->getPort();
echo '<br><b>Path:</b> ', $url->getPath();
echo '<br><b>Suffix:</b> ', $url->getSuffix();
echo '<br><b>Query:</b> ', $url->getQuery();
echo '<br><b>Fragment:</b> ', $url->getFragment();
$url->setSource('http://россия.рф');
$url->setConversionIdnToAscii(true)->updateSource();
echo '<br><br><b>new URL:</b> ',$url; #http://xn--h1alffa9f.xn--p1ai
$url = new Url('http://grigor:[email protected] :8080/path/to/resource?query=value#fragment');
$url->setSource('http://grigor:[email protected] :8080/path/to/resource?query=value#fragment');
$url->setAll([
':host' => 'host.ru',
':scheme' => 'http',
':user' => 'user',
':password' => 'password',
':port' => '80',
':path' => ['path', 'to', 'resource'],
'?' => ['key' => 'value', 'key2' => 'value2'],
'#' => 'fragment',
':suffix' => '.json',
]);
$url->setScheme('http')->setUser('grigor')->setPassword('password')->setHost('vinograd.soft')
->setPort('8080')->setPath('/path/to/resource')->setSuffix('.json')
->setArrayQuery(['query' => 'value',])->setFragment('fragment');
const USER_STATE = 1 << 0;
const PASSWORD_STATE = 1 << 1;
const HOST_STATE = 1 << 2;
const PORT_STATE = 1 << 3;
const PATH_STATE = 1 << 0;
const QUERY_STATE = 1 << 1;
const FRAGMENT_STATE = 1 << 2;
const AUTHORITY_WHOLE = self::USER_STATE | self::PASSWORD_STATE | self::HOST_STATE | self::PORT_STATE;
const RELATIVE_URL_WHOLE = self::PATH_STATE | self::QUERY_STATE | self::FRAGMENT_STATE;
/**
* current states
*/
protected int $authoritySate = self::AUTHORITY_WHOLE;
protected int $relativeUrlState = self::RELATIVE_URL_WHOLE;
protected bool $schemeState = true;
$relativeUrlState &= ~Url::QUERY_STATE;
namespace <your\namespace>;
use Compass\DefaultUrlStrategy;
use Compass\Url;
class ReferralUrlStrategy extends DefaultUrlStrategy
{
private bool $isAllowInsertParam = false;
/**
* @inheritDoc
*/
public function updateQuery(array $items): string
{
if ($this->isAllowInsertParam) {
$items['refid'] = 40;
$this->isAllowInsertParam = false;
}
return http_build_query($items, '', '&', PHP_QUERY_RFC3986);
}
/**
* @inheritDoc
*/
public function forceUnlockMethod(
bool &$schemeState,
int &$authoritySate,
int &$relativeUrlState,
array $items,
array $pathItems,
array $queryItems,
bool $updateAbsoluteUrl,
?string $suffix = null
): void
{
if ($items[Url::HOST] === 'another.site') {
$this->isAllowInsertParam = true;
}
$relativeUrlState &= ~Url::QUERY_STATE;
}
}
$url = Url::createBlank();
$url->setUpdateStrategy(new ReferralUrlStrategy());
$url->setSource('https://another.site');
$url->updateSource();
echo $url->getSource(), '<br>'; # https://another.site/?refid=40
$url->setHost('vinograd.soft');
$url->updateSource();
echo $url->getSource(); # https://vinograd.soft
use Compass\Path;
E__/__NAME__Scanner/__NAME2__Driver', '/');
$path->replaceAll([
'__NAME__' => 'User',
'__NAME2__' => 'Filesystem',
]);
$path->updateSource();
echo '<br>', $path; # /User/UserScanner/FilesystemDriver
$path->setAll(['path', 'to', 'file.txt']);
$path->updateSource();
echo '<br>', $path; # path/to/file.txt
$path->set(1, 'newTo');
$path->updateSource();
echo '<br>', $path; # path/newTo/file.txt
$path->setBy('path', 'newPath');
$path->updateSource();
echo '<br>', $path; # newPath/newTo/file.txt
echo '<br>', $path->dirname(); # newPath/newTo
$path->setSuffix('.v');
$path->updateSource();
echo '<br>', $path; # newPath/newTo/file.txt.v
$path->setSource('newPath/newTo/file');
$path->setSuffix('.v');
$path->updateSource();
echo '<br>', $path; # newPath/newTo/file.v
$path->replace('newPath','path');
$path->updateSource();
echo '<br>', $path; # path/newTo/file.v
echo '<br>', $path->getLast(); # file.v
php composer
php composer tests