Skip to content

Commit 2ce87a5

Browse files
committed
initial commit
0 parents  commit 2ce87a5

File tree

9 files changed

+240
-0
lines changed

9 files changed

+240
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
build/
2+
composer.lock
3+
docs
4+
vendor/

.styleci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
preset: psr2

.travis.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
language: php
2+
3+
php:
4+
- '7.1'
5+
- '7.2'
6+
- '7.3'
7+
8+
before_script:
9+
- travis_retry composer self-update
10+
- travis_retry composer update --no-interaction --prefer-source
11+
12+
script:
13+
- vendor/bin/phpunit

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# The MIT License (MIT)
2+
3+
Copyright (c) 2016 kr <[email protected]>
4+
5+
> Permission is hereby granted, free of charge, to any person obtaining a copy
6+
> of this software and associated documentation files (the "Software"), to deal
7+
> in the Software without restriction, including without limitation the rights
8+
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
> copies of the Software, and to permit persons to whom the Software is
10+
> furnished to do so, subject to the following conditions:
11+
>
12+
> The above copyright notice and this permission notice shall be included in
13+
> all copies or substantial portions of the Software.
14+
>
15+
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
> THE SOFTWARE.

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Parse ids to an array
2+
3+
[![Software License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](LICENSE.md)
4+
[![Latest Stable Version](https://img.shields.io/badge/Version-stable-blue.svg?format=flat-square)](https://packagist.org/packages/ottosmops/ids)
5+
[![Build Status](https://travis-ci.com/ottosmops/ids.svg?branch=master)](https://travis-ci.com/ottosmops/ids)
6+
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/248db8b3-4969-48c5-9a61-9c7346832ff0/mini.png)](https://insight.sensiolabs.com/projects/248db8b3-4969-48c5-9a61-9c7346832ff0)
7+
[![Packagist Downloads](https://img.shields.io/packagist/dt/ottosmops/ids.svg?style=flat-square)](https://packagist.org/packages/ottosmops/ids)
8+
9+
10+
## Idea
11+
For PHP CLI or Api, if you want to have an option for one or more ids (integers), this very small package is a helper, which parses a string to an array of ids.
12+
13+
| Input | Output |
14+
|:----:|:----:|
15+
|1|[1]|
16+
|1,2|[1,2]|
17+
|1-3 | [1,2,3] |
18+
| 1-5,7,8 | [1,2,3,4,5,7,8] |
19+
20+
## Usage
21+
The usage is very simple:
22+
23+
```php
24+
$ids = '1-3';
25+
$array = Ids::parse($ids); // [1,2,3]
26+
```
27+
28+
## License
29+
30+
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
31+

composer.json

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"name": "ottosmops/ids",
3+
"description": "parse an ids-string to an array",
4+
"keywords": [
5+
"ottosmops",
6+
"CLI",
7+
"Api",
8+
"Helper"
9+
],
10+
"type": "library",
11+
"license": "MIT",
12+
"authors": [
13+
{
14+
"name": "ak",
15+
"email": "[email protected]",
16+
"homepage": "https://wwwk-r.ch"
17+
}
18+
],
19+
"homepage": "https://github.com/ottosmops/ottosmops/ids",
20+
"minimum-stability": "stable",
21+
"require": {
22+
"php" : ">=7.1"
23+
},
24+
"autoload": {
25+
"psr-4": {
26+
"Ottosmops\\Ids\\": "src"
27+
}
28+
},
29+
"autoload-dev": {
30+
"psr-4": {
31+
"Ottosmops\\Ids\\Test\\": "tests/"
32+
}
33+
},
34+
"scripts": {
35+
"test": "phpunit",
36+
"check-style": "phpcs -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src tests",
37+
"fix-style": "phpcbf -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src tests"
38+
},
39+
"require-dev": {
40+
"phpunit/phpunit": "^7.5"
41+
}
42+
43+
}

phpunit.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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/7.0/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
forceCoversAnnotation="true"
6+
beStrictAboutCoversAnnotation="true"
7+
beStrictAboutOutputDuringTests="true"
8+
beStrictAboutTodoAnnotatedTests="true"
9+
verbose="true"
10+
colors="true">
11+
<testsuite name="default">
12+
<directory suffix="Test.php">tests</directory>
13+
</testsuite>
14+
15+
<filter>
16+
<whitelist processUncoveredFilesFromWhitelist="true">
17+
<directory suffix=".php">src</directory>
18+
</whitelist>
19+
</filter>
20+
</phpunit>

src/Ids.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Ottosmops\Ids;
4+
5+
class Ids
6+
{
7+
static public function parse(string $ids) : array
8+
{
9+
if (!static::isValid($ids)) {
10+
throw new \InvalidArgumentException('Is not a valid ids-string: ' . $ids);
11+
}
12+
13+
if (strpos($ids, '-') && strpos($ids, ',')) {
14+
$array = explode(',', $ids);
15+
$return = [];
16+
foreach($array as $v) {
17+
if (strpos($v, '-')) {
18+
$return = array_merge($return, static::createRange($v));
19+
} else {
20+
$return[] = $v;
21+
}
22+
}
23+
return $return;
24+
}
25+
26+
if (strpos($ids, '-')) {
27+
return static::createRange($ids);
28+
}
29+
30+
if (strpos($ids, ',')) {
31+
return explode(',', $ids);
32+
}
33+
34+
return [$ids];
35+
}
36+
37+
static public function isValid(string $string): bool
38+
{
39+
return preg_match('/^[0-9].*/', $string) &&
40+
preg_match('/.*[0-9]$/', $string) &&
41+
!preg_match('/(--|,,|,-|-,|[a-zA-Z&%$§!*_:.;\/])/', $string) &&
42+
preg_match('/[1-9][1-9,-]*/', $string);
43+
}
44+
45+
static private function createRange(string $string): array
46+
{
47+
list($start, $end) = explode('-', $string);
48+
return range(trim($start), trim($end));
49+
}
50+
}

tests/ParseIdsTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Ottosmops\Ids\Test;
4+
5+
use Ottosmops\Ids\Ids;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class ParseIdsTest extends TestCase
9+
{
10+
/** test */
11+
public function test_it_parses_a_single_integer()
12+
{
13+
$expected = [3];
14+
$actual = Ids::parse("3");
15+
$this->assertEquals($expected, $actual);
16+
}
17+
18+
public function test_it_parses_a_two_integers()
19+
{
20+
$expected = [3,4];
21+
$actual = Ids::parse("3, 4");
22+
$this->assertEquals($expected, $actual);
23+
}
24+
25+
public function test_it_parses_a_range()
26+
{
27+
$expected = [3,4,5];
28+
$actual = Ids::parse("3-5");
29+
$this->assertEquals($expected, $actual);
30+
}
31+
32+
public function test_it_something_complicated()
33+
{
34+
$expected = [3,4,5,7,9,10,11];
35+
$actual = Ids::parse("3-5, 7, 9-11");
36+
$this->assertEquals($expected, $actual);
37+
}
38+
39+
public function test_it_thows_a_invalid_argument_exception()
40+
{
41+
$this->expectException(\InvalidArgumentException::class);
42+
Ids::parse("5--3");
43+
}
44+
45+
public function test_it_thows_a_invalid_argument_exception2()
46+
{
47+
$this->expectException(\InvalidArgumentException::class);
48+
Ids::parse("hallo");
49+
}
50+
51+
public function test_it_thows_a_invalid_argument_exception3()
52+
{
53+
$this->expectException(\InvalidArgumentException::class);
54+
Ids::parse("3&5");
55+
}
56+
57+
}

0 commit comments

Comments
 (0)