{"id":19386987,"url":"https://github.com/bermudaphp/byte","last_synced_at":"2026-03-16T19:34:37.839Z","repository":{"id":62493376,"uuid":"465092526","full_name":"bermudaphp/byte","owner":"bermudaphp","description":"Bytes object wrapper","archived":false,"fork":false,"pushed_at":"2024-05-20T20:55:56.000Z","size":39,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-02-21T07:36:00.064Z","etag":null,"topics":["byte","bytes","php8"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bermudaphp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-03-01T23:43:04.000Z","updated_at":"2024-08-03T10:48:31.000Z","dependencies_parsed_at":"2024-05-20T21:58:32.987Z","dependency_job_id":"6f86274d-47fe-4db4-be0b-a14300f8c88b","html_url":"https://github.com/bermudaphp/byte","commit_stats":{"total_commits":38,"total_committers":1,"mean_commits":38.0,"dds":0.0,"last_synced_commit":"278d119be6acc0330cae6c0eae4ff64af4745d31"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bermudaphp%2Fbyte","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bermudaphp%2Fbyte/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bermudaphp%2Fbyte/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bermudaphp%2Fbyte/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bermudaphp","download_url":"https://codeload.github.com/bermudaphp/byte/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243401480,"owners_count":20285051,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["byte","bytes","php8"],"created_at":"2024-11-10T10:08:02.287Z","updated_at":"2025-12-25T19:40:34.227Z","avatar_url":"https://github.com/bermudaphp.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Byte and BitRate Classes\n[![PHP Version](https://img.shields.io/badge/php-%3E%3D8.4-blue.svg)](https://www.php.net/)\n[![License](https://img.shields.io/badge/license-MIT-green.svg)](https://opensource.org/licenses/MIT)\n[![Ask DeepWiki](https://deepwiki.com/badge.svg)](https://deepwiki.com/bermudaphp/byte)\n\n*Read this in other languages: [Russian](README.ru.md)*\n\n## Overview\n\nThe library provides PHP classes for working with data sizes (Byte) and data transfer rates (BitRate) with comprehensive functionality for conversion, comparison, arithmetic operations, and human-readable formatting.\n\n## Features\n\n- **Unit Conversion**: Easily convert between different data units (B, kB, MB, GB, TB, PB, EB, ZB, YB)\n- **Transfer Rate Handling**: Work with bit rates in various units (bps, kbps, Mbps, Gbps, etc.)\n- **Arithmetic Operations**: Perform addition, subtraction, multiplication, division, and modulo operations\n- **Comparison**: Compare values with support for both individual and array-based comparisons\n- **Formatting**: Format values as human-readable strings with multi-language support\n- **Range Operations**: Create ranges of values, find min/max, calculate averages\n- **Transfer Time Calculation**: Estimate data transfer times based on bandwidth\n- **Internationalization**: Format time durations in multiple languages\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require bermudaphp/byte\n```\n\n## Usage\n\n### Byte Class\n\n#### Creating Byte Instances\n\nThere are multiple ways to create a `Byte` instance:\n\n```php\nuse Bermuda\\Stdlib\\Byte;\n\n// From numeric value (bytes)\n$bytes = new Byte(1024);\n\n// From string\n$bytes = new Byte('1024 kB');\n\n// Using static factory methods\n$bytes = Byte::new(1024);\n$bytes = Byte::kb(1024); // 1024 kilobytes\n$bytes = Byte::mb(50);   // 50 megabytes\n$bytes = Byte::gb(2);    // 2 gigabytes\n\n// From human-readable string\n$bytes = Byte::fromHumanReadable('2.5 GB');\n\n// From bits\n$bytes = Byte::fromBits(8192); // 1024 bytes\n```\n\n#### Converting to Different Units\n\n```php\n$bytes = new Byte(1536);\n\n// Convert to human-readable string\necho $bytes-\u003etoString(); // \"1.5 kB\"\n\n// Convert to specific units\necho $bytes-\u003etoKb();  // \"1.5 kB\"\necho $bytes-\u003etoMb();  // \"0.0015 MB\"\necho $bytes-\u003etoGb();  // \"0.000001 GB\"\n\n// Customize format\necho $bytes-\u003eto('kb', 3, '_'); // \"1.500_kB\"\n\n// Get raw value in specific unit\n$kbValue = $bytes-\u003egetValue('kb'); // 1.5\n```\n\n#### Comparison Operations\n\nThe class supports both single-value and multi-value comparisons with two modes:\n- `MODE_ALL`: Returns true only if the condition is true for all values\n- `MODE_ANY`: Returns true if the condition is true for at least one value\n\n```php\n$bytes = Byte::kb(1024); // 1 MB\n\n// Compare with a single value\n$bytes-\u003eequalTo('1 MB');        // true\n$bytes-\u003egreaterThan('900 kB');  // true\n$bytes-\u003elessThan('1.1 MB');     // true\n\n// Compare with multiple values\n$bytes-\u003egreaterThan(['900 kB', '1.1 MB'], Byte::MODE_ANY); // true\n$bytes-\u003egreaterThan(['900 kB', '1.1 MB'], Byte::MODE_ALL); // false\n\n// Other comparison methods\n$bytes-\u003elessThanOrEqual('1 MB');                    // true\n$bytes-\u003egreaterThanOrEqual(['1 MB', '1024 kB']);    // true\n$bytes-\u003ebetween('900 kB', '1.1 MB');                // true\n$bytes-\u003einRanges([['500 kB', '800 kB'], ['1 MB', '1.5 MB']]); // true\n```\n\n#### Arithmetic Operations\n\n```php\n$bytes = Byte::mb(1);\n\n// Addition\n$newBytes = $bytes-\u003eincrement('500 kB'); // 1.5 MB\n\n// Subtraction\n$newBytes = $bytes-\u003edecrement('512 kB'); // ~0.5 MB\n\n// Division\n$newBytes = $bytes-\u003edivide(2); // 512 KB\n\n// Multiplication\n$newBytes = $bytes-\u003emultiply(3); // 3 MB\n\n// Modulo\n$newBytes = $bytes-\u003emodulo('512 kB'); // 0\n\n// Absolute value\n$newBytes = (new Byte(-1024))-\u003eabs(); // 1024 bytes\n\n// Min/Max\n$newBytes = $bytes-\u003emax('1.5 MB'); // 1.5 MB\n$newBytes = $bytes-\u003emin(['2 MB', '500 kB']); // 500 kB\n```\n\n#### Static Operations on Collections\n\n```php\n// Create a range\n$range = Byte::range('1 MB', '5 MB', '1 MB'); // [1 MB, 2 MB, 3 MB, 4 MB, 5 MB]\n\n// Calculate sum\n$sum = Byte::sum(['1 MB', '2 MB', '500 kB']); // 3.5 MB\n\n// Calculate average\n$avg = Byte::average(['1 MB', '2 MB', '3 MB']); // 2 MB\n\n// Find maximum/minimum\n$max = Byte::maximum(['1 MB', '500 kB', '2 GB']); // 2 GB\n$min = Byte::minimum(['1 MB', '500 kB', '2 GB']); // 500 kB\n```\n\n### BitRate Class\n\n#### Creating BitRate Instances\n\n```php\nuse Bermuda\\Stdlib\\BitRate;\n\n// From numeric value (bits per second)\n$rate = new BitRate(1_000_000); // 1 Mbps\n\n// Using static factory methods for bit-based rates\n$rate = BitRate::bps(1000);     // 1000 bits per second\n$rate = BitRate::kbps(1000);    // 1000 kilobits per second\n$rate = BitRate::mbps(10);      // 10 megabits per second\n$rate = BitRate::gbps(1);       // 1 gigabit per second\n\n// Using static factory methods for byte-based rates\n$rate = BitRate::bytesPerSec(125_000);  // 125 KB/s (equivalent to 1 Mbps)\n$rate = BitRate::kBps(1);               // 1 kilobyte per second (8 kbps)\n$rate = BitRate::mBps(1);               // 1 megabyte per second (8 Mbps)\n$rate = BitRate::gBps(1);               // 1 gigabyte per second (8 Gbps)\n\n// From any unit string\n$rate = BitRate::from(10, 'Mbps');      // 10 Mbps\n$rate = BitRate::from(1.5, 'GBps');     // 1.5 GB/s\n\n// From human-readable string\n$rate = BitRate::fromHumanReadable('10 Mbps');\n```\n\n#### Converting Between Units\n\n```php\n$rate = BitRate::mbps(100);  // 100 Mbps\n\n// Get value in bits or bytes\n$bitsPerSec = $rate-\u003etoBits();    // 100,000,000 bps\n$bytesPerSec = $rate-\u003etoBytes();  // 12,500,000 B/s\n\n// Convert to human-readable formats\necho $rate-\u003etoString();           // \"100 Mbps\"\necho $rate-\u003etoString('byte');     // \"12.5 MBps\"\n\n// Convert to specific units\necho $rate-\u003etoMbps();             // \"100 Mbps\"\necho $rate-\u003etoGbps();             // \"0.1 Gbps\"\necho $rate-\u003etoMBps();             // \"12.5 MBps\"\necho $rate-\u003etoKBps();             // \"12500 kBps\"\n\n// Customize output format\necho $rate-\u003eto('Mbps', 3, '_');   // \"100.000_Mbps\"\n```\n\n#### Comparison Operations\n\n```php\n$rate = BitRate::mbps(100);  // 100 Mbps\n\n// Compare with another bit rate\n$rate-\u003eequalTo(BitRate::kbps(100000));           // true\n$rate-\u003eequalTo('100 Mbps');                      // true\n$rate-\u003eequalTo('12.5 MBps');                     // true (bytes equivalent)\n\n// Greater/less than comparisons\n$rate-\u003egreaterThan(BitRate::mbps(50));           // true\n$rate-\u003elessThan(BitRate::gbps(1));               // true\n\n// Compare with arrays\n$rate-\u003egreaterThan(['10 Mbps', '150 Mbps'], BitRate::MODE_ANY);  // true\n```\n\n#### Arithmetic Operations\n\n```php\n$rate = BitRate::mbps(100);\n\n// Addition\n$newRate = $rate-\u003eincrement(BitRate::mbps(50));    // 150 Mbps\n\n// Subtraction\n$newRate = $rate-\u003edecrement(BitRate::mbps(30));    // 70 Mbps\n\n// Multiplication\n$newRate = $rate-\u003emultiply(2);                     // 200 Mbps\n\n// Division\n$newRate = $rate-\u003edivide(4);                       // 25 Mbps\n\n// Throttling (special case of multiplication)\n$throttledRate = $rate-\u003ethrottle(0.8);             // 80 Mbps (80% of original)\n```\n\n#### Transfer Calculations\n\n```php\n$rate = BitRate::mbps(100);      // 100 Mbps download speed\n$fileSize = Byte::gb(1);         // 1 GB file\n\n// Calculate transfer time\n$seconds = $rate-\u003ecalculateTransferTime($fileSize);  // 80 seconds\n\n// Get formatted transfer time\n$time = $rate-\u003egetFormattedTransferTime($fileSize);  // \"1 minute, 20 seconds\"\n\n// Calculate transfer amount for a given time\n$downloadedSize = $rate-\u003ecalculateTransferAmount(60);  // 750 MB in 60 seconds\n\n// Estimate file size for streaming\n$streamingRate = BitRate::mbps(5);                    // 5 Mbps video stream\n$videoDuration = 3600;                                // 1 hour in seconds\n$videoSize = $streamingRate-\u003eestimateFileSize($videoDuration);  // ~2.25 GB\n```\n\n#### Static Operations on Collections\n\n```php\n$rates = [\n    BitRate::mbps(10),\n    BitRate::mbps(50),\n    BitRate::mbps(100)\n];\n\n// Calculate average\n$avgRate = BitRate::average($rates);  // 53.33 Mbps\n\n// Find maximum/minimum\n$maxRate = BitRate::maximum($rates);  // 100 Mbps\n$minRate = BitRate::minimum($rates);  // 10 Mbps\n\n// Calculate sum\n$totalRate = BitRate::sum($rates);    // 160 Mbps\n\n// Create a range\n$rangeRates = BitRate::range(\n    BitRate::mbps(10), \n    BitRate::mbps(50), \n    BitRate::mbps(10)\n);  // [10 Mbps, 20 Mbps, 30 Mbps, 40 Mbps, 50 Mbps]\n```\n\n### BitFormatter for Internationalization\n\nThe `BitFormatter` class provides formatting functionality with multi-language support:\n\n```php\nuse Bermuda\\Stdlib\\BitFormatter;\nuse Bermuda\\Stdlib\\Byte;\nuse Bermuda\\Stdlib\\BitRate;\n\n// Load translation files\nBitFormatter::loadLanguage('/path/to/translations/en.php');\nBitFormatter::loadLanguage('/path/to/translations/fr.php');\nBitFormatter::loadLanguage('/path/to/translations/ru.php');\n\n// Or load all translations from a directory\nBitFormatter::loadLanguagesFromDirectory('/path/to/translations');\n\n/* The library comes with built-in translations for multiple languages. You can load all available translations at once using the `loadDefaults()` method*/\nBitFormatter::loadDefaults()\n\n// Format time in different languages\n$fileSize = Byte::gb(2);\n$downloadSpeed = BitRate::mbps(25);\n$seconds = BitFormatter::calculateTransferTime($fileSize, $downloadSpeed);\n\necho BitFormatter::formatTime($seconds, 'en');  // \"10 minutes, 40 seconds\"\necho BitFormatter::formatTime($seconds, 'fr');  // \"10 minutes et 40 secondes\"\necho BitFormatter::formatTime($seconds, 'ru');  // \"10 минут и 40 секунд\"\n\n// Set default language\nBitFormatter::setDefaultLanguage('fr');\necho BitFormatter::formatTime($seconds);        // \"10 minutes et 40 secondes\"\n\n// Direct formatting with BitRate and Byte classes\necho $downloadSpeed-\u003egetFormattedTransferTime($fileSize, 'en');  // \"10 minutes, 40 seconds\"\necho $fileSize-\u003egetFormattedTransferTime($downloadSpeed, 'ru');  // \"10 минут и 40 секунд\"\n\n// Format data values\necho BitFormatter::humanizeBytes(1536);  // \"1.5 kB\"\necho BitFormatter::humanizeBitRate(1_000_000, 'bit');  // \"1 Mbps\"\necho BitFormatter::humanizeBitRate(1_000_000, 'byte');  // \"125 kBps\"\n```\n\n## Error Handling\n\nThe classes throw exceptions in the following situations:\n\n- `\\InvalidArgumentException`: When parsing invalid string formats or using unsupported units\n- `\\LogicException`: When attempting to decrement by a value greater than the current value\n- `\\DivisionByZeroError`: When attempting to divide by zero\n\n## License\n\nThis package is open-sourced software licensed under the [MIT license](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbermudaphp%2Fbyte","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbermudaphp%2Fbyte","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbermudaphp%2Fbyte/lists"}