Skip to content

Commit d6fda48

Browse files
committed
initial
0 parents  commit d6fda48

35 files changed

Lines changed: 1865 additions & 0 deletions

.github/workflows/test_master.yml

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
10+
11+
jobs:
12+
test:
13+
name: Test
14+
runs-on: ubuntu-latest
15+
strategy:
16+
matrix:
17+
php: ['7.4', '8.0', '8.1', '8.2']
18+
19+
steps:
20+
- name: Set up PHP
21+
uses: shivammathur/setup-php@v2
22+
with:
23+
php-version: ${{ matrix.php }}
24+
coverage: xdebug
25+
tools: composer:v2
26+
27+
- name: Checkout code
28+
uses: actions/checkout@v2
29+
with:
30+
fetch-depth: 0
31+
32+
- name: PHP Version Check
33+
run: php -v
34+
35+
- name: Validate Composer JSON
36+
run: composer validate
37+
38+
- name: Run Composer
39+
run: composer install --no-interaction
40+
41+
- name: Unit tests
42+
run: |
43+
composer test-init
44+
composer test
45+
46+
- name: PHP Code Sniffer
47+
run: composer codesniffer
48+
49+
- name: PHPStan analysis
50+
run: composer stan
51+
52+
code-coverage:
53+
name: Code coverage
54+
runs-on: ubuntu-latest
55+
strategy:
56+
matrix:
57+
php: ['7.4']
58+
59+
steps:
60+
- name: Set up PHP
61+
uses: shivammathur/setup-php@v2
62+
with:
63+
php-version: ${{ matrix.php }}
64+
coverage: xdebug
65+
tools: composer:v2
66+
67+
- name: Checkout code
68+
uses: actions/checkout@v2
69+
with:
70+
fetch-depth: 0
71+
72+
- name: Run Composer
73+
run: composer install --no-interaction
74+
75+
- name: Unit tests
76+
run: |
77+
composer test-init
78+
composer test-coverage-xml
79+
mkdir -p ./build/logs
80+
cp ./tests/_output/coverage.xml ./build/logs/clover.xml
81+
- name: Code Coverage (Coveralls)
82+
env:
83+
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
84+
run: php vendor/bin/php-coveralls -v

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
vendor
2+
.idea
3+
tests/_output
4+
tests/_support/_generated
5+
composer.lock
6+
build

.scrutinizer.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
build:
2+
image: default-bionic
3+
environment:
4+
php:
5+
version: 8.1.2
6+
ini:
7+
xdebug.mode: coverage
8+
nodes:
9+
analysis:
10+
project_setup:
11+
override:
12+
- 'true'
13+
tests:
14+
override:
15+
- php-scrutinizer-run
16+
-
17+
command: phpcs-run
18+
coverage:
19+
tests:
20+
override:
21+
- command: composer test-init && composer test-coverage-xml
22+
coverage:
23+
file: ./tests/_output/coverage.xml
24+
format: clover
25+
tools:
26+
php_code_coverage: true
27+
filter:
28+
excluded_paths:
29+
- 'tests/*'
30+
checks:
31+
php: true
32+
coding_style:
33+
php:
34+
spaces:
35+
around_operators:
36+
additive: false
37+
multiplicative: false

LICENSE

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) 2022 Smoren
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 all
13+
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 THE
21+
SOFTWARE.

README.md

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# Iterator-based squences
2+
3+
![Packagist PHP Version Support](https://img.shields.io/packagist/php-v/smoren/sequence)
4+
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/Smoren/sequence-php/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/Smoren/sequence-php/?branch=master)
5+
[![Coverage Status](https://coveralls.io/repos/github/Smoren/sequence-php/badge.svg?branch=master)](https://coveralls.io/github/Smoren/sequence-php?branch=master)
6+
![Build and test](https://github.com/Smoren/sequence-php/actions/workflows/test_master.yml/badge.svg)
7+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
8+
9+
Python-like sequences with iterators for PHP
10+
11+
### How to install to your project
12+
```
13+
composer require smoren/sequence
14+
```
15+
16+
### Unit testing
17+
```
18+
composer install
19+
composer test-init
20+
composer test
21+
```
22+
23+
### Usage
24+
25+
#### Range
26+
27+
```php
28+
use Smoren\Sequence\Structs\IntRange;
29+
use Smoren\Sequence\Structs\FloatRange;
30+
31+
/* Simple int range */
32+
$range = new IntRange(1, 3, 2); // (from, size, step)
33+
var_dump($range->isInfinite()); // false
34+
35+
foreach($range as $value) {
36+
echo "{$value} ";
37+
}
38+
// out: 1 3 5
39+
40+
var_dump($range[0]); // 1
41+
var_dump($range[1]); // 3
42+
var_dump($range[2]); // 5
43+
44+
try {
45+
$range[3];
46+
} catch(OutOfRangeException $e) {
47+
}
48+
49+
var_dump($range[-1]); // 5
50+
var_dump($range[-2]); // 3
51+
var_dump($range[-3]); // 1
52+
53+
try {
54+
$range[-4];
55+
} catch(OutOfRangeException $e) {
56+
echo "cannot get value from index out of range\n";
57+
}
58+
59+
/* Infinite int range */
60+
$range = new IntRange(1, null, 2);
61+
var_dump($range->isInfinite()); // true
62+
63+
foreach($range as $i => $value) {
64+
echo "{$value} ";
65+
if($i > 100) break;
66+
}
67+
// out: 1 3 5 7 9 11 13...
68+
69+
/* Float range */
70+
$range = new FloatRange(1.1, 3, 2.1);
71+
var_dump($range->isInfinite()); // false
72+
73+
foreach($range as $value) {
74+
echo "{$value} ";
75+
}
76+
// out: 1.1 3.2 5.3
77+
```
78+
79+
#### IndexedArray
80+
81+
```php
82+
use Smoren\Sequence\Structs\IndexedArray;
83+
use Smoren\Sequence\Exceptions\OutOfRangeException;
84+
85+
$array = new IndexedArray([10, 20, 30]);
86+
87+
$array[0] = 11;
88+
$array[-1] = 33;
89+
$array[1] = 22;
90+
var_dump(count($array)); // 3
91+
print_r($array->toArray()); // [11, 22, 33]
92+
93+
unset($array[1]);
94+
print_r($array->toArray()); // [11, 33]
95+
96+
$array[] = 111;
97+
print_r($array->toArray()); // [11, 33, 111]
98+
99+
try {
100+
$array[3];
101+
} catch(OutOfRangeException $e) {
102+
echo "cannot get value from index out of range\n";
103+
}
104+
105+
try {
106+
$array[3] = 1;
107+
} catch(OutOfRangeException $e) {
108+
echo "cannot set value from index out of range\n";
109+
}
110+
111+
try {
112+
unset($array[3]);
113+
} catch(OutOfRangeException $e) {
114+
echo "cannot unset value from index out of range\n";
115+
}
116+
```

codeception.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
actor: Tester
2+
bootstrap: _bootstrap.php
3+
paths:
4+
tests: tests
5+
log: tests/_output
6+
output: tests/_output
7+
data: tests/_data
8+
helpers: tests/_support
9+
settings:
10+
memory_limit: 1024M
11+
colors: true
12+
coverage:
13+
#c3_url: http://localhost:8080/index-test.php/
14+
enabled: true
15+
show_uncovered: false
16+
include:
17+
- src/*
18+
exclude:
19+
- vendor/*
20+
- tests/*

composer.json

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"name": "smoren/sequence",
3+
"description": "Iterator-based sequences",
4+
"keywords": ["sequence", "range", "exponential", "iterator", "python-like"],
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Smoren",
9+
"email": "ofigate@gmail.com"
10+
}
11+
],
12+
"require": {
13+
"php": ">=7.4.0"
14+
},
15+
"require-dev": {
16+
"codeception/codeception": "^4.2.1",
17+
"codeception/module-asserts": "^2.0",
18+
"php-coveralls/php-coveralls": "^2.0",
19+
"squizlabs/php_codesniffer": "3.*",
20+
"phpstan/phpstan": "^1.8"
21+
},
22+
"autoload": {
23+
"psr-4": {
24+
"Smoren\\Sequence\\": "src",
25+
"Smoren\\Sequence\\Tests\\Unit\\": "tests/unit"
26+
}
27+
},
28+
"config": {
29+
"fxp-asset": {
30+
"enabled": false
31+
}
32+
},
33+
"repositories": [
34+
{
35+
"type": "composer",
36+
"url": "https://asset-packagist.org"
37+
}
38+
],
39+
"scripts": {
40+
"test-init": ["./vendor/bin/codecept build"],
41+
"test-all": ["composer test-coverage", "composer codesniffer", "composer stan"],
42+
"test": ["./vendor/bin/codecept run unit tests/unit"],
43+
"test-coverage": ["./vendor/bin/codecept run unit tests/unit --coverage"],
44+
"test-coverage-html": ["./vendor/bin/codecept run unit tests/unit --coverage-html"],
45+
"test-coverage-xml": ["./vendor/bin/codecept run unit tests/unit --coverage-xml"],
46+
"codesniffer": ["./vendor/bin/phpcs --ignore=vendor,tests --standard=tests/coding_standard.xml -s ."],
47+
"stan": ["./vendor/bin/phpstan analyse -l 9 src"]
48+
}
49+
}

phpcs.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0"?>
2+
<ruleset>
3+
<file>./</file>
4+
<exclude-pattern>./vendor/*</exclude-pattern>
5+
<rule ref="PSR1" />
6+
</ruleset>

src/Exceptions/BaseException.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Smoren\Sequence\Exceptions;
6+
7+
use Exception;
8+
9+
abstract class BaseException extends Exception
10+
{
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Smoren\Sequence\Exceptions;
6+
7+
class OutOfRangeException extends BaseException
8+
{
9+
}

0 commit comments

Comments
 (0)