Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
d8vjork committed Oct 28, 2023
1 parent 3831c7f commit a34cebe
Show file tree
Hide file tree
Showing 17 changed files with 905 additions and 159 deletions.
22 changes: 4 additions & 18 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,9 @@ jobs:
fail-fast: true
matrix:
os: [ ubuntu-latest ]
php: [ 8.0, 8.1, 8.2 ]
laravel: [ 9.*, 10.* ]
dependency-version: [ prefer-stable ]
include:
- laravel: 9.*
testbench: 7.*
php: [ 8.1, 8.2, 8.3 ]

- laravel: 10.*
testbench: 8.*
exclude:
- laravel: 10.*
php: 8.0

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ${{ matrix.os }}
name: PHP${{ matrix.php }} - ${{ matrix.os }}

steps:
- name: Checkout code
Expand All @@ -33,7 +22,6 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, intl, exif
coverage: pcov

- name: Get composer cache directory
Expand All @@ -45,12 +33,10 @@ jobs:
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-${{ matrix.dependency-version }}-
restore-keys: ${{ runner.os }}-composer-

- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest
run: composer install --prefer-dist --no-interaction --no-suggest

- name: Execute tests
run: vendor/bin/phpunit -c phpunit.coverage.dist.xml
Expand Down
54 changes: 40 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,54 @@
title_goes_here [![required php version](https://img.shields.io/packagist/php-v/open-southeners/packagist_package_here)](https://www.php.net/supported-versions.php) [![codecov](https://codecov.io/gh/open-southeners/packagist_package_here/branch/main/graph/badge.svg?token=codecov_badge_token)](https://codecov.io/gh/open-southeners/packagist_package_here) [![Edit on VSCode online](https://img.shields.io/badge/vscode-edit%20online-blue?logo=visualstudiocode)](https://vscode.dev/github/open-southeners/packagist_package_here)
Byte unit converter [![required php version](https://img.shields.io/packagist/php-v/open-southeners/byte-unit-converter)](https://www.php.net/supported-versions.php) [![codecov](https://codecov.io/gh/open-southeners/byte-unit-converter/branch/main/graph/badge.svg?token=qcEglkQDg4)](https://codecov.io/gh/open-southeners/byte-unit-converter) [![Edit on VSCode online](https://img.shields.io/badge/vscode-edit%20online-blue?logo=visualstudiocode)](https://vscode.dev/github/open-southeners/byte-unit-converter)
===

and_description_here

**Search & replace:**

| Keys | Replacement |
| --------------------------------------------------------- | ------------------------------------------------------------- |
| `title_goes_here` | Fancy title from the repository (only README and stuff) |
| `and_description_here` | Short package description (only for README and composer.json) |
| `packagist_package_here` | Packagist (composer) published package name |
| `codecov_badge_token` | Codecov token for the coverage badge |
| `OpenSoutheners\PhpPackage`, `OpenSoutheners\\PhpPackage` | PSR-0 and PSR-4 complaint package namespace |
A utility written in PHP8.1+ to convert multiple-byte units with no dependencies.

## Getting started

```
composer require open-southeners/packagist_package_here
composer require open-southeners/byte-unit-converter
```

### Usage

**Before start using this package first familiarice with the concept of [multi-byte unit](https://en.wikipedia.org/wiki/Byte#Multiple-byte_units)**.

Example usage:

```php
<?php

use OpenSoutheners\ByteUnitConverter\BinaryByteUnit;
use OpenSoutheners\ByteUnitConverter\DecimalByteUnit;
use OpenSoutheners\ByteUnitConverter\ByteUnitConverter;

// Using decimal system
ByteUnitConverter::from(1, DecimalByteUnit::TB)->to(DecimalByteUnit::GB); // 1000
ByteUnitConverter::from(1, DecimalByteUnit::TB)->toGB(); // 1000
ByteUnitConverter::from(1, DecimalByteUnit::TB)->toGigabyte(); // 1000
ByteUnitConverter::conversion(1, DecimalByteUnit::TB, DecimalByteUnit::GB); // 1000

// Using binary system
ByteUnitConverter::from(1, BinaryByteUnit::TiB)->to(BinaryByteUnit::GiB); // 1024
ByteUnitConverter::from(1, BinaryByteUnit::TiB)->toGiB(); // 1024
ByteUnitConverter::from(1, BinaryByteUnit::TiB)->toGibibyte(); // 1024
ByteUnitConverter::conversion(1, BinaryByteUnit::TiB, BinaryByteUnit::GiB); // 1024
```

### Considerations

**We don't use round** or any method to remove extra decimals, **we consider this should be done by the developer using this library** so take this in mind.

Also take in mind the different units within the enums, each one is using different conversion base (check usage).

## Motivation

After learning the Swift programming language I found a very useful utility under darwin systems (Mac OS X) which is the [ByteCountFormatter](https://developer.apple.com/documentation/foundation/bytecountformatter) there is where I based this project, also needed this for a long-run project (also used in our other package: [machine-stats](https://github.com/open-southeners/machine-stats)) therefore here it is.

## Partners

[![skore logo](https://github.com/open-southeners/partners/raw/main/logos/skore_logo.png)](https://getskore.com)

## License

This package is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).
This package is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).
15 changes: 0 additions & 15 deletions SECURITY.md

This file was deleted.

31 changes: 15 additions & 16 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
{
"name": "open-southeners/packagist_package_here",
"description": "and_description_here",
"name": "open-southeners/byte-unit-converter",
"description": "A utility written in PHP8.1+ to convert multiple-byte units with no dependencies",
"license": "MIT",
"keywords": [
"open-southeners"
"open-southeners",
"bytes-conversion",
"frameworkless",
"dependencyless",
"conversion",
"utilities",
"tools",
"bytes"
],
"authors": [
{
Expand All @@ -19,33 +26,25 @@
}
],
"require": {
"php": "^8.0 || ^8.1 || ^8.2"
"php": "^8.1"
},
"require-dev": {
"nunomaduro/larastan": "^2.0",
"orchestra/testbench": "^7.0 || ^8.0",
"laravel/pint": "^1.13",
"phpstan/phpstan": "^1.0",
"phpunit/phpunit": "^9.0 || ^10.0"
"phpunit/phpunit": "^10.0"
},
"minimum-stability": "dev",
"autoload": {
"psr-4": {
"OpenSoutheners\\PhpPackage\\": "src"
"OpenSoutheners\\ByteUnitConverter\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"OpenSoutheners\\PhpPackage\\Tests\\": "tests"
"OpenSoutheners\\ByteUnitConverter\\Tests\\": "tests"
}
},
"config": {
"sort-packages": true
},
"extra": {
"laravel": {
"providers": [
"OpenSoutheners\\PhpPackage\\ServiceProvider"
]
}
}
}
5 changes: 1 addition & 4 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
includes:
- ./vendor/nunomaduro/larastan/extension.neon

parameters:

paths:
- src

# The level 8 is the highest level
level: 5
level: 8

checkMissingIterableValueType: false
21 changes: 21 additions & 0 deletions phpunit-coverage.dist.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" bootstrap="vendor/autoload.php" backupGlobals="false" colors="true" processIsolation="true" stopOnFailure="false" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<coverage>
<report>
<clover outputFile="clover.xml"/>
</report>
</coverage>
<testsuites>
<testsuite name="Package Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<php>
<ini name="memory_limit" value="2048M"/>
</php>
<source>
<include>
<directory suffix=".php">src</directory>
</include>
</source>
</phpunit>
30 changes: 0 additions & 30 deletions phpunit.coverage.dist.xml

This file was deleted.

13 changes: 1 addition & 12 deletions phpunit.dist.xml
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
bootstrap="vendor/autoload.php"
backupGlobals="false"
colors="true"
processIsolation="true"
stopOnFailure="false"
cacheDirectory=".phpunit.cache"
backupStaticProperties="false"
>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" bootstrap="vendor/autoload.php" backupGlobals="false" colors="true" processIsolation="true" stopOnFailure="false" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<testsuites>
<testsuite name="Package Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<php>
<ini name="memory_limit" value="2048M"/>
<env name="DB_CONNECTION" value="sqlite" force="true"/>
<env name="DB_DATABASE" value=":memory:" force="true"/>
</php>
</phpunit>
70 changes: 70 additions & 0 deletions src/BinaryByteUnit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace OpenSoutheners\ByteUnitConverter;

/**
* This enum serves as a type safety for byte units based on the binary system.
*
* @see https://en.wikipedia.org/wiki/Binary_prefix
*
* @author Rubén Robles <me@d8vjork.com>
*/
enum BinaryByteUnit: int implements ByteUnit
{
use ComparesByteUnits;

/**
* Quebibyte
*/
case QiB = 10;

/**
* Robibyte
*/
case RiB = 9;

/**
* Yobibyte
*/
case YiB = 8;

/**
* Zebibyte
*/
case ZiB = 7;

/**
* Exbibyte
*/
case EiB = 6;

/**
* Pebibyte
*/
case PiB = 5;

/**
* Tebibyte
*/
case TiB = 4;

/**
* Gibibyte
*/
case GiB = 3;

/**
* Mebibyte
*/
case MiB = 2;

/**
* Kibibyte
*/
case KiB = 1;

/**
* Byte
*/
case B = 0;
}
15 changes: 15 additions & 0 deletions src/ByteUnit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace OpenSoutheners\ByteUnitConverter;

/**
* Mock interface used only for type safety on byte unit enums.
*
* @author Rubén Robles <me@d8vjork.com>
*
* @property-read int $value
*/
interface ByteUnit extends \BackedEnum
{
//
}
Loading

0 comments on commit a34cebe

Please sign in to comment.