Skip to content

Commit

Permalink
Merge pull request #12 from KepplerPl/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
andrei-popa88 committed Dec 9, 2018
2 parents 17073ed + f0a3c84 commit 7ef67be
Show file tree
Hide file tree
Showing 17 changed files with 1,043 additions and 102 deletions.
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

0 comments on commit 7ef67be

Please sign in to comment.