Skip to content

Commit

Permalink
Merge pull request #1 from vdhicts/feature/initial-version
Browse files Browse the repository at this point in the history
Add initial version of the package
  • Loading branch information
dvdheiden authored Aug 5, 2021
2 parents 0b13515 + c594427 commit cae3cd2
Show file tree
Hide file tree
Showing 15 changed files with 405 additions and 1 deletion.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
; This file is for unifying the coding style for different editors and IDEs.
; More information at http://editorconfig.org

root = true

[*]
charset = utf-8
indent_size = 4
indent_style = space
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
28 changes: 28 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
name: Bug report
about: Create a report to help us improve
title: ""
labels: bug
assignees: vdhicts

---

## Describe the bug

A clear and concise description of what the bug is.

## Reproduction

Steps to reproduce the behavior.

## Expected behavior

A clear and concise description of what you expected to happen.

## Actual behavior

Describe the behavior as it is right now.

## Additional information

Anything else you want to provide.
16 changes: 16 additions & 0 deletions .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
name: Feature request
about: Suggest an idea for this project
title: ""
labels: feature
assignees: vdhicts

---

## Goal

A clear and concise description of what the problem is. Ex. I think this could be easier when...

## Additional information

Add any other context or screenshots about the feature request here.
8 changes: 8 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Changes

Provide a summary of your changes.

# Checks

- [ ] The changelog is updated (when applicable)
- [ ] The support Cluster API version is updated in the readme (when applicable)
29 changes: 29 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: http-query-builder

on: [push]

jobs:
cluster-api-tests:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v1

- name: Setup PHP
uses: shivammathur/setup-php@v1
with:
php-version: '7.4'
extensions: mbstring, intl

- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-suggest --no-progress

- name: Execute tests (Unit and Feature tests) via PHPUnit
run: |
vendor/bin/phpunit
- name: Execute static analysis
run: |
vendor/bin/psalm
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
composer.lock
/vendor/
/.idea/
/build/
.phpunit.result.cache
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to
[Semantic Versioning](https://semver.org/spec/v2.0.0.html). Please note this changelog affects this package and not the
cluster API. See the changelog of the [cluster API](https://cluster-api.cyberfusion.nl/redoc#section/Changelog) for
detailed information.

## [1.0.0]

### [Added]

- Add initial version of the package
61 changes: 60 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,61 @@
# http-query-builder
Replacement of http_build_query to allow the same parameter multiple times

Replacement of http_build_query to allow the same parameter multiple times.

## Requirements

This package requires PHP 7.4 or higher.

## Installation

This package can be used in any PHP project or with any framework.

You can install the package via composer:

`composer require vdhicts/http-query-builder`

## Usage

The problem with the build-in `http_build_query` method is that it doesn't accept the same parameter multiple times.
When you need to consume an API that uses those parameters (for example [FastAPI](https://fastapi.tiangolo.com/)
supports it), this package comes in handy.

### Getting started

```php
use Vdhicts\HttpQueryBuilder\Builder;

$builder = (new Builder())
->add('filter', 'a:1')
->add('filter', 'b:2');
echo $builder; // filter=a%3A1&filter=b%3A2
```

## Tests

Unit tests are available in the `tests` folder. Run with:

`composer test`

When you want a code coverage report which will be generated in the `build/report` folder. Run with:

`composer test-coverage`

## Contribution

Any contribution is welcome, but it should meet the PSR-2 standard and please create one pull request per feature/bug.
In exchange, you will be credited as contributor on this page.

## Security

If you discover any security related issues in this or other packages of Vdhicts, please email info@vdhicts.nl instead
of using the issue tracker.

## License

This package is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT).

## About Vdhicts

[Vdhicts](https://www.vdhicts.nl) is the name of my personal company for which I work as freelancer. Vdhicts develops
and implements IT solutions for businesses and educational institutions.
45 changes: 45 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "vdhicts/http-query-builder",
"description": "Replacement of http_build_query to allow the same parameter multiple times ",
"keywords": [
"http_build_query",
"query"
],
"homepage": "https://github.com/vdhicts/http-query-builder",
"license": "MIT",
"authors": [
{
"name": "Dick van der Heiden",
"email": "info@vdhicts.nl",
"homepage": "https://www.vdhicts.nl",
"role": "Developer"
}
],
"require": {
"php": "^7.4 || ^8.0"
},
"require-dev": {
"phpunit/phpunit": "^9.0",
"vimeo/psalm": "^4.4"
},
"autoload": {
"psr-4": {
"Vdhicts\\HttpQueryBuilder\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Vdhicts\\HttpQueryBuilder\\Tests\\": "tests"
}
},
"scripts": {
"test": "vendor/bin/phpunit --no-coverage",
"test-coverage": "vendor/bin/phpunit",
"analyse": "vendor/bin/psalm"
},
"config": {
"sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true
}
16 changes: 16 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" backupStaticAttributes="false" colors="true" verbose="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">src/</directory>
</include>
<report>
<html outputDirectory="./build/report/"/>
</report>
</coverage>
<testsuites>
<testsuite name="Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
15 changes: 15 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<psalm
errorLevel="5"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
</psalm>
37 changes: 37 additions & 0 deletions src/Builder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Vdhicts\HttpQueryBuilder;

class Builder
{
private array $parameters = [];

public function get(): array
{
return $this->parameters;
}

public function add(string $parameter, string $value): Builder
{
$this->parameters[] = new Parameter($parameter, $value);

return $this;
}

public function build(): string
{
$queryParameters = array_map(
function (Parameter $parameter) {
return $parameter->build();
},
$this->parameters
);

return implode('&', $queryParameters);
}

public function __toString()
{
return $this->build();
}
}
39 changes: 39 additions & 0 deletions src/Parameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Vdhicts\HttpQueryBuilder;

class Parameter
{
private string $key;
private string $value;

public function __construct(string $key, string $value)
{
$this->key = $key;
$this->value = $value;
}

public function getKey(): string
{
return $this->key;
}

public function getValue(): string
{
return $this->value;
}

public function build(): string
{
return sprintf(
'%s=%s',
urlencode($this->key),
urlencode($this->value)
);
}

public function __toString(): string
{
return $this->build();
}
}
35 changes: 35 additions & 0 deletions tests/BuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Vdhicts\HttpQueryBuilder\Tests;

use PHPUnit\Framework\TestCase;
use Vdhicts\HttpQueryBuilder\Builder;
use Vdhicts\HttpQueryBuilder\Parameter;

class BuilderTest extends TestCase
{
private function initBuilder(): Builder
{
return (new Builder())
->add('filter', 'a:1')
->add('filter', 'b:2')
->add('test', 1);
}

public function testBuilder()
{
$builder = $this->initBuilder();

$this->assertIsArray($builder->get());
$this->assertCount(3, $builder->get());
$this->assertInstanceOf(Parameter::class, $builder->get()[0]);
$this->assertSame('filter=a%3A1&filter=b%3A2&test=1', $builder->build());
}

public function testSerialization()
{
$builder = $this->initBuilder();

$this->assertSame('filter=a%3A1&filter=b%3A2&test=1', (string)$builder);
}
}
Loading

0 comments on commit cae3cd2

Please sign in to comment.