Skip to content

Commit

Permalink
Let's go!
Browse files Browse the repository at this point in the history
  • Loading branch information
niladam committed Dec 21, 2024
0 parents commit efd4f74
Show file tree
Hide file tree
Showing 18 changed files with 976 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Please see the documentation for all configuration options:
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:

- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
labels:
- "dependencies"
31 changes: 31 additions & 0 deletions .github/update-changelog.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: "Update Changelog"

on:
release:
types: [released]

permissions:
contents: write

jobs:
update:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: main

- name: Update Changelog
uses: stefanzweifel/changelog-updater-action@v1
with:
latest-version: ${{ github.event.release.name }}
release-notes: ${{ github.event.release.body }}

- name: Commit updated CHANGELOG
uses: stefanzweifel/git-auto-commit-action@v5
with:
branch: main
commit_message: Update CHANGELOG
file_pattern: CHANGELOG.md
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.DS_Store
.idea
.phpunit.result.cache
.vscode
build
composer.lock
coverage
docs
node_modules
phpunit.xml
phpstan.neon
testbench.yaml
vendor
33 changes: 33 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

use PhpCsFixer\Config;
use PhpCsFixer\Finder;
use PhpCsFixer\Runner\Parallel\ParallelConfigFactory;

$finder = Finder::create()
->in(__DIR__) // Adjust to the directories you want to scan
->exclude('vendor') // Exclude vendor directory
->name('*.php')
->notName('*.blade.php');

return (new Config())
->setParallelConfig(ParallelConfigFactory::detect())
->setFinder($finder)
->setRiskyAllowed(true) // Allow risky rules if needed
->setRules([
'@PSR12' => true,
'array_syntax' => ['syntax' => 'short'],
'binary_operator_spaces' => ['default' => 'align_single_space_minimal'],
'blank_line_after_namespace' => true,
'blank_line_after_opening_tag' => true,
'concat_space' => ['spacing' => 'one'],
'method_argument_space' => ['on_multiline' => 'ensure_fully_multiline'],
'no_unused_imports' => true,
'ordered_imports' => ['sort_algorithm' => 'alpha'],
'single_import_per_statement' => true,
'single_quote' => true,
'trailing_comma_in_multiline' => true,
'simplified_if_return' => true,
]);
Empty file added CHANGELOG.md
Empty file.
123 changes: 123 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# URI

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.

## Inspiration

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.

## Features

- Parse and manipulate URIs
- Handle query strings with ease, including merging, replacing, and removing keys
- Build URIs fluently
- Decode URI strings

## Installation

Install via Composer:

```bash
composer require niladam/uri
```

## Usage

### Creating and Parsing a URI

```php
use Niladam\Uri\Uri;

$uri = Uri::of('https://user:password@example.com:8080/some/subfolder?view=full#section');

// Access URI components
$scheme = $uri->scheme(); // "https"
$user = $uri->user(); // "user"
$password = $uri->password(); // "password"
$host = $uri->host(); // "example.com"
$port = $uri->port(); // 8080
$path = $uri->path(); // "some/subfolder"
$query = $uri->query()->all(); // ['view' => 'full']
$fragment = $uri->fragment(); // "section"

// String representation of the URI
echo (string)$uri; // "https://user:password@example.com:8080/some/subfolder?view=full#section"
```

### Building a URI Fluently

```php
$uri = Uri::of()
->withScheme('https')
->withHost('example.com')
->withUser('john', 'password')
->withPort(1234)
->withPath('/account/profile')
->withFragment('overview')
->replaceQuery(['view' => 'detailed']);

echo (string)$uri; // "https://john:password@example.com:1234/account/profile?view=detailed#overview"
```

### Working with Query Strings

#### Parsing Queries

```php
$uri = Uri::of('https://example.com?name=Taylor&tags=php&tags=laravel&flagged');

// Access all query parameters
$query = $uri->query()->all();
// [
// 'name' => 'Taylor',
// 'tags' => ['php', 'laravel'],
// 'flagged' => ''
// ]
```

#### Modifying Queries

```php
// Merging Queries
$uri = $uri->withQuery(['tags' => 'framework', 'new' => 'value']);
// Query: "tags=php&tags=laravel&tags=framework&new=value"

// Replacing Queries
$uri = $uri->replaceQuery(['key' => 'value']);
// Query: "key=value"

// Removing Query Keys
$uri = $uri->withoutQuery(['tags', 'new']);
// Query: "key=value"

// Pushing Values onto a Query Key
$uri = $uri->pushOntoQuery('tags', 'newTag');
// Query: "tags=php&tags=laravel&tags=newTag"
```

### Decoding a URI

```php
$decodedUri = $uri->decode();
// Converts encoded URI components to their decoded equivalents
```

## Testing

Run the tests using PHPUnit:

```bash
vendor/bin/phpunit
```

## Contributing

1. Fork the repository.
2. Create a feature branch.
3. Make your changes.
4. Submit a pull request.

## License

This package is open-source and licensed under the MIT License.

36 changes: 36 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "niladam/uri",
"description": "Manipulate and work with your URIs",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Madalin Tache",
"email": "niladam@gmail.com",
"role": "Developer"
}
],
"require": {
"php": ">=7.4",
"symfony/var-dumper": "5.4",
"league/uri": "^6.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.65",
"phpunit/phpunit": "9.6.22"
},
"autoload": {
"psr-4": {
"Niladam\\Uri\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Niladam\\Uri\\Tests\\": "tests/"
}
},
"scripts": {
"fix": "php-cs-fixer fix --config=.php-cs-fixer.php",
"test": "vendor/bin/phpunit --colors"
}
}
29 changes: 29 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheResultFile=".phpunit.cache/test-results"
colors="true"
executionOrder="depends,defects"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
convertDeprecationsToExceptions="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
failOnRisky="true"
failOnWarning="true"
verbose="true">
<testsuites>
<testsuite name="URI Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>

<coverage cacheDirectory=".phpunit.cache/code-coverage"
processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
</phpunit>
13 changes: 13 additions & 0 deletions src/Concerns/Arrayable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Niladam\Uri\Concerns;

interface Arrayable
{
/**
* Convert the object to an array.
*
* @return array
*/
public function toArray(): array;
}
13 changes: 13 additions & 0 deletions src/Concerns/Htmlable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Niladam\Uri\Concerns;

interface Htmlable
{
/**
* Convert the object to its HTML string representation.
*
* @return string
*/
public function toHtml(): string;
}
17 changes: 17 additions & 0 deletions src/Concerns/Stringable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Niladam\Uri\Concerns;

/**
* Interface Stringable
* Defines a contract for objects that can be converted to a string.
*/
interface Stringable
{
/**
* Convert the object to its string representation.
*
* @return string
*/
public function __toString(): string;
}
28 changes: 28 additions & 0 deletions src/Traits/Conditionable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Niladam\Uri\Traits;

/**
* Trait Conditionable
* Provides methods for conditionally executing callbacks.
*/
trait Conditionable
{
public function when(bool $condition, callable $callback): self
{
if ($condition) {
$callback($this);
}

return $this;
}

public function unless(bool $condition, callable $callback): self
{
if (!$condition) {
$callback($this);
}

return $this;
}
}
22 changes: 22 additions & 0 deletions src/Traits/Dumpable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Niladam\Uri\Traits;

trait Dumpable
{
public function dump()
{
dump($this);

return $this;
}

public function dd(): void
{
$this->dump();

exit(1);
}
}
Loading

0 comments on commit efd4f74

Please sign in to comment.