Skip to content

Commit efd4f74

Browse files
committed
Let's go!
0 parents  commit efd4f74

18 files changed

+976
-0
lines changed

.github/dependabot.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Please see the documentation for all configuration options:
2+
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
3+
4+
version: 2
5+
updates:
6+
7+
- package-ecosystem: "github-actions"
8+
directory: "/"
9+
schedule:
10+
interval: "weekly"
11+
labels:
12+
- "dependencies"

.github/update-changelog.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: "Update Changelog"
2+
3+
on:
4+
release:
5+
types: [released]
6+
7+
permissions:
8+
contents: write
9+
10+
jobs:
11+
update:
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- name: Checkout code
16+
uses: actions/checkout@v4
17+
with:
18+
ref: main
19+
20+
- name: Update Changelog
21+
uses: stefanzweifel/changelog-updater-action@v1
22+
with:
23+
latest-version: ${{ github.event.release.name }}
24+
release-notes: ${{ github.event.release.body }}
25+
26+
- name: Commit updated CHANGELOG
27+
uses: stefanzweifel/git-auto-commit-action@v5
28+
with:
29+
branch: main
30+
commit_message: Update CHANGELOG
31+
file_pattern: CHANGELOG.md

.gitignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.DS_Store
2+
.idea
3+
.phpunit.result.cache
4+
.vscode
5+
build
6+
composer.lock
7+
coverage
8+
docs
9+
node_modules
10+
phpunit.xml
11+
phpstan.neon
12+
testbench.yaml
13+
vendor

.php-cs-fixer.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PhpCsFixer\Config;
6+
use PhpCsFixer\Finder;
7+
use PhpCsFixer\Runner\Parallel\ParallelConfigFactory;
8+
9+
$finder = Finder::create()
10+
->in(__DIR__) // Adjust to the directories you want to scan
11+
->exclude('vendor') // Exclude vendor directory
12+
->name('*.php')
13+
->notName('*.blade.php');
14+
15+
return (new Config())
16+
->setParallelConfig(ParallelConfigFactory::detect())
17+
->setFinder($finder)
18+
->setRiskyAllowed(true) // Allow risky rules if needed
19+
->setRules([
20+
'@PSR12' => true,
21+
'array_syntax' => ['syntax' => 'short'],
22+
'binary_operator_spaces' => ['default' => 'align_single_space_minimal'],
23+
'blank_line_after_namespace' => true,
24+
'blank_line_after_opening_tag' => true,
25+
'concat_space' => ['spacing' => 'one'],
26+
'method_argument_space' => ['on_multiline' => 'ensure_fully_multiline'],
27+
'no_unused_imports' => true,
28+
'ordered_imports' => ['sort_algorithm' => 'alpha'],
29+
'single_import_per_statement' => true,
30+
'single_quote' => true,
31+
'trailing_comma_in_multiline' => true,
32+
'simplified_if_return' => true,
33+
]);

CHANGELOG.md

Whitespace-only changes.

README.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# URI
2+
3+
This PHP package provides a robust and flexible way to manipulate and handle URIs, including schemes, hosts, paths, queries, and fragments. It is built on top of `league/uri` and extends its functionality with an intuitive API and additional features like query string handling and fluent URI modification.
4+
5+
## Inspiration
6+
7+
This package was inspired and is based on Laravel's Uri (`Illuminate\Support\Uri`) from Laravel 11. However, since Laravel 11 requires PHP 8, this package was created to provide similar functionality for PHP 7.4. Some pieces of code have been directly copied from Laravel's implementation to ensure compatibility and feature parity.
8+
9+
## Features
10+
11+
- Parse and manipulate URIs
12+
- Handle query strings with ease, including merging, replacing, and removing keys
13+
- Build URIs fluently
14+
- Decode URI strings
15+
16+
## Installation
17+
18+
Install via Composer:
19+
20+
```bash
21+
composer require niladam/uri
22+
```
23+
24+
## Usage
25+
26+
### Creating and Parsing a URI
27+
28+
```php
29+
use Niladam\Uri\Uri;
30+
31+
$uri = Uri::of('https://user:password@example.com:8080/some/subfolder?view=full#section');
32+
33+
// Access URI components
34+
$scheme = $uri->scheme(); // "https"
35+
$user = $uri->user(); // "user"
36+
$password = $uri->password(); // "password"
37+
$host = $uri->host(); // "example.com"
38+
$port = $uri->port(); // 8080
39+
$path = $uri->path(); // "some/subfolder"
40+
$query = $uri->query()->all(); // ['view' => 'full']
41+
$fragment = $uri->fragment(); // "section"
42+
43+
// String representation of the URI
44+
echo (string)$uri; // "https://user:password@example.com:8080/some/subfolder?view=full#section"
45+
```
46+
47+
### Building a URI Fluently
48+
49+
```php
50+
$uri = Uri::of()
51+
->withScheme('https')
52+
->withHost('example.com')
53+
->withUser('john', 'password')
54+
->withPort(1234)
55+
->withPath('/account/profile')
56+
->withFragment('overview')
57+
->replaceQuery(['view' => 'detailed']);
58+
59+
echo (string)$uri; // "https://john:password@example.com:1234/account/profile?view=detailed#overview"
60+
```
61+
62+
### Working with Query Strings
63+
64+
#### Parsing Queries
65+
66+
```php
67+
$uri = Uri::of('https://example.com?name=Taylor&tags=php&tags=laravel&flagged');
68+
69+
// Access all query parameters
70+
$query = $uri->query()->all();
71+
// [
72+
// 'name' => 'Taylor',
73+
// 'tags' => ['php', 'laravel'],
74+
// 'flagged' => ''
75+
// ]
76+
```
77+
78+
#### Modifying Queries
79+
80+
```php
81+
// Merging Queries
82+
$uri = $uri->withQuery(['tags' => 'framework', 'new' => 'value']);
83+
// Query: "tags=php&tags=laravel&tags=framework&new=value"
84+
85+
// Replacing Queries
86+
$uri = $uri->replaceQuery(['key' => 'value']);
87+
// Query: "key=value"
88+
89+
// Removing Query Keys
90+
$uri = $uri->withoutQuery(['tags', 'new']);
91+
// Query: "key=value"
92+
93+
// Pushing Values onto a Query Key
94+
$uri = $uri->pushOntoQuery('tags', 'newTag');
95+
// Query: "tags=php&tags=laravel&tags=newTag"
96+
```
97+
98+
### Decoding a URI
99+
100+
```php
101+
$decodedUri = $uri->decode();
102+
// Converts encoded URI components to their decoded equivalents
103+
```
104+
105+
## Testing
106+
107+
Run the tests using PHPUnit:
108+
109+
```bash
110+
vendor/bin/phpunit
111+
```
112+
113+
## Contributing
114+
115+
1. Fork the repository.
116+
2. Create a feature branch.
117+
3. Make your changes.
118+
4. Submit a pull request.
119+
120+
## License
121+
122+
This package is open-source and licensed under the MIT License.
123+

composer.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "niladam/uri",
3+
"description": "Manipulate and work with your URIs",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Madalin Tache",
9+
"email": "niladam@gmail.com",
10+
"role": "Developer"
11+
}
12+
],
13+
"require": {
14+
"php": ">=7.4",
15+
"symfony/var-dumper": "5.4",
16+
"league/uri": "^6.0"
17+
},
18+
"require-dev": {
19+
"friendsofphp/php-cs-fixer": "^3.65",
20+
"phpunit/phpunit": "9.6.22"
21+
},
22+
"autoload": {
23+
"psr-4": {
24+
"Niladam\\Uri\\": "src/"
25+
}
26+
},
27+
"autoload-dev": {
28+
"psr-4": {
29+
"Niladam\\Uri\\Tests\\": "tests/"
30+
}
31+
},
32+
"scripts": {
33+
"fix": "php-cs-fixer fix --config=.php-cs-fixer.php",
34+
"test": "vendor/bin/phpunit --colors"
35+
}
36+
}

phpunit.xml.dist

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
cacheResultFile=".phpunit.cache/test-results"
6+
colors="true"
7+
executionOrder="depends,defects"
8+
beStrictAboutOutputDuringTests="true"
9+
beStrictAboutTodoAnnotatedTests="true"
10+
convertDeprecationsToExceptions="true"
11+
convertErrorsToExceptions="true"
12+
convertNoticesToExceptions="true"
13+
convertWarningsToExceptions="true"
14+
failOnRisky="true"
15+
failOnWarning="true"
16+
verbose="true">
17+
<testsuites>
18+
<testsuite name="URI Test Suite">
19+
<directory>tests</directory>
20+
</testsuite>
21+
</testsuites>
22+
23+
<coverage cacheDirectory=".phpunit.cache/code-coverage"
24+
processUncoveredFiles="true">
25+
<include>
26+
<directory suffix=".php">src</directory>
27+
</include>
28+
</coverage>
29+
</phpunit>

src/Concerns/Arrayable.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Niladam\Uri\Concerns;
4+
5+
interface Arrayable
6+
{
7+
/**
8+
* Convert the object to an array.
9+
*
10+
* @return array
11+
*/
12+
public function toArray(): array;
13+
}

src/Concerns/Htmlable.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Niladam\Uri\Concerns;
4+
5+
interface Htmlable
6+
{
7+
/**
8+
* Convert the object to its HTML string representation.
9+
*
10+
* @return string
11+
*/
12+
public function toHtml(): string;
13+
}

src/Concerns/Stringable.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Niladam\Uri\Concerns;
4+
5+
/**
6+
* Interface Stringable
7+
* Defines a contract for objects that can be converted to a string.
8+
*/
9+
interface Stringable
10+
{
11+
/**
12+
* Convert the object to its string representation.
13+
*
14+
* @return string
15+
*/
16+
public function __toString(): string;
17+
}

src/Traits/Conditionable.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Niladam\Uri\Traits;
4+
5+
/**
6+
* Trait Conditionable
7+
* Provides methods for conditionally executing callbacks.
8+
*/
9+
trait Conditionable
10+
{
11+
public function when(bool $condition, callable $callback): self
12+
{
13+
if ($condition) {
14+
$callback($this);
15+
}
16+
17+
return $this;
18+
}
19+
20+
public function unless(bool $condition, callable $callback): self
21+
{
22+
if (!$condition) {
23+
$callback($this);
24+
}
25+
26+
return $this;
27+
}
28+
}

src/Traits/Dumpable.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Niladam\Uri\Traits;
6+
7+
trait Dumpable
8+
{
9+
public function dump()
10+
{
11+
dump($this);
12+
13+
return $this;
14+
}
15+
16+
public function dd(): void
17+
{
18+
$this->dump();
19+
20+
exit(1);
21+
}
22+
}

0 commit comments

Comments
 (0)