-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from KepplerPl/develop
Develop
- Loading branch information
Showing
17 changed files
with
1,043 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, '/'); | ||
} | ||
} |
Oops, something went wrong.