Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
Smoren committed Dec 24, 2022
0 parents commit d6fda48
Show file tree
Hide file tree
Showing 35 changed files with 1,865 additions and 0 deletions.
84 changes: 84 additions & 0 deletions .github/workflows/test_master.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
name: CI

on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
test:
name: Test
runs-on: ubuntu-latest
strategy:
matrix:
php: ['7.4', '8.0', '8.1', '8.2']

steps:
- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: xdebug
tools: composer:v2

- name: Checkout code
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: PHP Version Check
run: php -v

- name: Validate Composer JSON
run: composer validate

- name: Run Composer
run: composer install --no-interaction

- name: Unit tests
run: |
composer test-init
composer test
- name: PHP Code Sniffer
run: composer codesniffer

- name: PHPStan analysis
run: composer stan

code-coverage:
name: Code coverage
runs-on: ubuntu-latest
strategy:
matrix:
php: ['7.4']

steps:
- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: xdebug
tools: composer:v2

- name: Checkout code
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Run Composer
run: composer install --no-interaction

- name: Unit tests
run: |
composer test-init
composer test-coverage-xml
mkdir -p ./build/logs
cp ./tests/_output/coverage.xml ./build/logs/clover.xml
- name: Code Coverage (Coveralls)
env:
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: php vendor/bin/php-coveralls -v
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
vendor
.idea
tests/_output
tests/_support/_generated
composer.lock
build
37 changes: 37 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
build:
image: default-bionic
environment:
php:
version: 8.1.2
ini:
xdebug.mode: coverage
nodes:
analysis:
project_setup:
override:
- 'true'
tests:
override:
- php-scrutinizer-run
-
command: phpcs-run
coverage:
tests:
override:
- command: composer test-init && composer test-coverage-xml
coverage:
file: ./tests/_output/coverage.xml
format: clover
tools:
php_code_coverage: true
filter:
excluded_paths:
- 'tests/*'
checks:
php: true
coding_style:
php:
spaces:
around_operators:
additive: false
multiplicative: false
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2022 Smoren

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
116 changes: 116 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# Iterator-based squences

![Packagist PHP Version Support](https://img.shields.io/packagist/php-v/smoren/sequence)
[![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)
[![Coverage Status](https://coveralls.io/repos/github/Smoren/sequence-php/badge.svg?branch=master)](https://coveralls.io/github/Smoren/sequence-php?branch=master)
![Build and test](https://github.com/Smoren/sequence-php/actions/workflows/test_master.yml/badge.svg)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

Python-like sequences with iterators for PHP

### How to install to your project
```
composer require smoren/sequence
```

### Unit testing
```
composer install
composer test-init
composer test
```

### Usage

#### Range

```php
use Smoren\Sequence\Structs\IntRange;
use Smoren\Sequence\Structs\FloatRange;

/* Simple int range */
$range = new IntRange(1, 3, 2); // (from, size, step)
var_dump($range->isInfinite()); // false

foreach($range as $value) {
echo "{$value} ";
}
// out: 1 3 5

var_dump($range[0]); // 1
var_dump($range[1]); // 3
var_dump($range[2]); // 5

try {
$range[3];
} catch(OutOfRangeException $e) {
}

var_dump($range[-1]); // 5
var_dump($range[-2]); // 3
var_dump($range[-3]); // 1

try {
$range[-4];
} catch(OutOfRangeException $e) {
echo "cannot get value from index out of range\n";
}

/* Infinite int range */
$range = new IntRange(1, null, 2);
var_dump($range->isInfinite()); // true

foreach($range as $i => $value) {
echo "{$value} ";
if($i > 100) break;
}
// out: 1 3 5 7 9 11 13...

/* Float range */
$range = new FloatRange(1.1, 3, 2.1);
var_dump($range->isInfinite()); // false

foreach($range as $value) {
echo "{$value} ";
}
// out: 1.1 3.2 5.3
```

#### IndexedArray

```php
use Smoren\Sequence\Structs\IndexedArray;
use Smoren\Sequence\Exceptions\OutOfRangeException;

$array = new IndexedArray([10, 20, 30]);

$array[0] = 11;
$array[-1] = 33;
$array[1] = 22;
var_dump(count($array)); // 3
print_r($array->toArray()); // [11, 22, 33]

unset($array[1]);
print_r($array->toArray()); // [11, 33]

$array[] = 111;
print_r($array->toArray()); // [11, 33, 111]

try {
$array[3];
} catch(OutOfRangeException $e) {
echo "cannot get value from index out of range\n";
}

try {
$array[3] = 1;
} catch(OutOfRangeException $e) {
echo "cannot set value from index out of range\n";
}

try {
unset($array[3]);
} catch(OutOfRangeException $e) {
echo "cannot unset value from index out of range\n";
}
```
20 changes: 20 additions & 0 deletions codeception.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
actor: Tester
bootstrap: _bootstrap.php
paths:
tests: tests
log: tests/_output
output: tests/_output
data: tests/_data
helpers: tests/_support
settings:
memory_limit: 1024M
colors: true
coverage:
#c3_url: http://localhost:8080/index-test.php/
enabled: true
show_uncovered: false
include:
- src/*
exclude:
- vendor/*
- tests/*
49 changes: 49 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name": "smoren/sequence",
"description": "Iterator-based sequences",
"keywords": ["sequence", "range", "exponential", "iterator", "python-like"],
"license": "MIT",
"authors": [
{
"name": "Smoren",
"email": "ofigate@gmail.com"
}
],
"require": {
"php": ">=7.4.0"
},
"require-dev": {
"codeception/codeception": "^4.2.1",
"codeception/module-asserts": "^2.0",
"php-coveralls/php-coveralls": "^2.0",
"squizlabs/php_codesniffer": "3.*",
"phpstan/phpstan": "^1.8"
},
"autoload": {
"psr-4": {
"Smoren\\Sequence\\": "src",
"Smoren\\Sequence\\Tests\\Unit\\": "tests/unit"
}
},
"config": {
"fxp-asset": {
"enabled": false
}
},
"repositories": [
{
"type": "composer",
"url": "https://asset-packagist.org"
}
],
"scripts": {
"test-init": ["./vendor/bin/codecept build"],
"test-all": ["composer test-coverage", "composer codesniffer", "composer stan"],
"test": ["./vendor/bin/codecept run unit tests/unit"],
"test-coverage": ["./vendor/bin/codecept run unit tests/unit --coverage"],
"test-coverage-html": ["./vendor/bin/codecept run unit tests/unit --coverage-html"],
"test-coverage-xml": ["./vendor/bin/codecept run unit tests/unit --coverage-xml"],
"codesniffer": ["./vendor/bin/phpcs --ignore=vendor,tests --standard=tests/coding_standard.xml -s ."],
"stan": ["./vendor/bin/phpstan analyse -l 9 src"]
}
}
6 changes: 6 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0"?>
<ruleset>
<file>./</file>
<exclude-pattern>./vendor/*</exclude-pattern>
<rule ref="PSR1" />
</ruleset>
11 changes: 11 additions & 0 deletions src/Exceptions/BaseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Smoren\Sequence\Exceptions;

use Exception;

abstract class BaseException extends Exception
{
}
9 changes: 9 additions & 0 deletions src/Exceptions/OutOfRangeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Smoren\Sequence\Exceptions;

class OutOfRangeException extends BaseException
{
}
9 changes: 9 additions & 0 deletions src/Exceptions/ReadOnlyException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Smoren\Sequence\Exceptions;

class ReadOnlyException extends BaseException
{
}

0 comments on commit d6fda48

Please sign in to comment.