Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #12

Merged
merged 4 commits into from
Dec 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions src/AbstractUrl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
declare(strict_types=1);

namespace Keppler\Url;

/**
* Class AbstractUrl
*
* @package Keppler\Url
*/
abstract class AbstractUrl
{
/**
* @var string
*/
protected $original = null;

/**
* @var
*/
public $path;

/**
* @var
*/
public $query;

/**
* @var array
*/
protected $allowedSchemas = [
'http',
'https',
'mailto',
];

/**
* @var
*/
protected $schema = null;

/**
* @var
*/
protected $authority = null;

/**
* @var
*/
protected $fragment = null;

/**
* @var
*/
protected $username = null;

/**
* @var
*/
protected $host = null;

/**
* @var null
*/
protected $password = null;

/**
* @var
*/
protected $port = null;
}
175 changes: 175 additions & 0 deletions src/Builder/Bags/PathBag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
<?php
declare(strict_types=1);

namespace Keppler\Url\Builder\Bags;

use Keppler\Url\Exceptions\ComponentNotFoundException;

/**
* Class PathBag
*
* @package Keppler\Builder\Bags
*/
class PathBag
{
/**
* @var array
*/
private $pathComponents = [];

/**
* @param array $pathComponents
*/
public function setPathComponents(array $pathComponents): void
{
$this->pathComponents = $pathComponents;
}

/**
* @param string $component
*
* @throws ComponentNotFoundException
*/
public function remove(string $component): void
{
if ( ! $this->has($component)) {
throw new ComponentNotFoundException("The component does not exist.");
}

foreach ($this->pathComponents as $key => $pathComponent) {
if ($pathComponent === $component) {
unset($this->pathComponents[$key]);
break;
}
}
}

/**
* @param string $oldComponent
* @param string $newComponent
*
* @return PathBag
* @throws ComponentNotFoundException
*/
public function overwrite(string $oldComponent, string $newComponent): self
{
if ( ! $this->has($oldComponent)) {
throw new ComponentNotFoundException("The component does not exist.");
}

foreach ($this->pathComponents as $key => $value) {
if ($oldComponent === $value) {
$this->pathComponents[$key] = $newComponent;
break;
}
}

return $this;
}

/**
* @param string $component
*
* @return PathBag
*/
public function append(string $component): self
{
array_push($this->pathComponents, $component);

return $this;
}

/**
* @param string $component
*
* @return PathBag
*/
public function prepend(string $component): self
{
array_unshift($this->pathComponents, $component);

return $this;
}

/**
* @param string $index
* @param string $component
*
* @return PathBag
* @throws ComponentNotFoundException
*/
public function insertAfter(string $index, string $component): self
{
if ( ! $this->has($index)) {
throw new ComponentNotFoundException("The component does not exist.");
}

$newComponents = [];
foreach ($this->pathComponents as $value) {
$newComponents[] = $value;
if ($index === $value) {
$newComponents[] = $component;
}
}

$this->pathComponents = $newComponents;

return $this;
}

/**
* @param string $index
*
* @return bool
*/
public function has(string $index): bool
{
return in_array(trim($index), $this->pathComponents);
}

/**
* @param string $index
* @param string $component
*
* @return PathBag
* @throws ComponentNotFoundException
*/
public function insertBefore(string $index, string $component): self
{
if ( ! $this->has($index)) {
throw new ComponentNotFoundException("The component does not exist.");
}

$newComponents = [];
foreach ($this->pathComponents as $value) {
if ($index === $value) {
$newComponents[] = $component;
}
$newComponents[] = $value;
}

$this->pathComponents = $newComponents;

return $this;
}

/**
* @param bool $withTrailingSlash
*
* @return string
*/
public function buildPath(bool $withTrailingSlash = true): string
{
if (empty($this->pathComponents)) {
return '';
}

$path = '/';

array_map(function ($value) use (&$path) {
$path .= $value.'/';
}, $this->pathComponents);

return true === $withTrailingSlash ? $path : rtrim($path, '/');
}
}
Loading