diff --git a/README.md b/README.md index ce69ce3..afe72bd 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,79 @@ Binn ==== +[![Build Status](https://travis-ci.org/et-nik/binn-php.svg?branch=master)](https://travis-ci.org/et-nik/binn-php) +[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/et-nik/binn-php/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/et-nik/binn-php/?branch=master) +[![Code Coverage](https://scrutinizer-ci.com/g/et-nik/binn-php/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/et-nik/binn-php/?branch=master) + PHP Class for serialize to binary string. Original Binn Library for C++ - https://github.com/liteserver/binn Binn Specification: https://github.com/liteserver/binn/blob/master/spec.md +## Installation + +```bash +composer require knik/binn +``` + ## Examples +### Binn + +Sequential arrays: + +```php +use Knik\Binn\Binn; + +$binn = new Binn; + +// List +$array = [123, -456, 789]; +$binnString = $binn->serialize($array); +$unserialized = $binn->unserialize($binnString); // Equal with $array +``` + +Numeric keys array: +```php +$binn = new Binn; + +// Map +$array = [1 => "add", 2 => [-12345, 6789]]; +$binnString = $binn->serialize($array); +$unserialized = $binn->unserialize($binnString); // Equal with $array +``` + +String keys array: +```php +$binn = new Binn; + +// Object +$array = ["hello" => "world"]; +$binnString = $binn->serialize($array); +$unserialized = $binn->unserialize($binnString); // Equal with $array +``` + +Mixed arrays: + +```php +$binn = new Binn; +$array = [ ["id" => 1, "name" => "John"], ["id" => 2, "name" => "Eric"] ] + +// A list of objects +$binnString = $binn->serialize($array); +$unserialized = $binn->unserialize($binnString); // Equal with $array +``` + +### Binn List + +Serialize/unserialize sequential arrays + ### Simple example ```php +use Knik\Binn\BinnList; + $array = [4, -8875, 'text']; $binn = new BinnList(); diff --git a/composer.json b/composer.json index c0b2ec9..2ea2b47 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ } ], "require": { - "php": ">=5.4" + "php": ">=5.6.3" }, "require-dev": { "mockery/mockery": "0.9.*", diff --git a/coverage.xml b/coverage.xml index 5febdda..ae643cb 100644 --- a/coverage.xml +++ b/coverage.xml @@ -1,140 +1,719 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/BinnAbstract.php b/src/BinnAbstract.php index 991df47..607bd0a 100644 --- a/src/BinnAbstract.php +++ b/src/BinnAbstract.php @@ -136,9 +136,9 @@ abstract class BinnAbstract * ] */ protected $containersClasses = [ - 0xE0 => \Knik\Binn\BinnList::class, - 0xE1 => \Knik\Binn\BinnMap::class, - 0xE2 => \Knik\Binn\BinnObject::class, + self::BINN_LIST => \Knik\Binn\BinnList::class, + self::BINN_MAP => \Knik\Binn\BinnMap::class, + self::BINN_OBJECT => \Knik\Binn\BinnObject::class, ]; /** @@ -150,22 +150,24 @@ public function setContainersClasses($containersClasses) } /** + * Get 4 bytes packed size. Add cut bit. * * @param int $intVal - * - * @return string HEX string + * @return string */ protected function getInt32Binsize($intVal = 0) { - $intVal = ($intVal | (1 << 31)); // Add byte + $intVal = ($intVal | (1 << 31)); // Add bit return $this->pack(self::BINN_UINT32, $intVal); } /** - * @param null $value + * Detect value type + * + * @param mixed $value * @return int */ - protected function detectType($value = null) + protected function detectType($value) { if (is_bool($value)) { return $value ? self::BINN_TRUE : self::BINN_FALSE; @@ -231,12 +233,21 @@ protected function detectInt($value) } } + /** + * Get storage type + * + * @param $type + * @return int + */ protected function storageType($type) { return $type & ($type ^ self::BINN_TYPE_MASK); } /** + * Array associativity check + * True if array is associative, False if array is sequential + * * @param array $arr * @return bool */ @@ -248,6 +259,9 @@ protected static function isArrayAssoc($arr) } /** + * Array objectivity check + * True if array is objective, False if array is sequential or have only number keys + * * @param $arr * @return bool */ @@ -263,8 +277,8 @@ protected static function isArrayObject($arr) } /** - * - * @return int + * Calculate result binary Binn string size + * @return int */ protected function calculateSize() { @@ -314,6 +328,7 @@ public function getBinnArr() } /** + * Get binn size * @return int */ public function binnSize() @@ -322,6 +337,12 @@ public function binnSize() } /** + * Memory saving + * If it possible: + * Converting int64 to int32/int16/int8 + * Converting uint64 to uint32/uint16/uint8 + * Converting positive int to uint + * * @param int $type * @param mixed $val * @@ -383,6 +404,11 @@ protected function compressInt($type, $val) return $newType; } + /** + * Clear all binn data + * + * @return $this + */ public function binnFree() { // $this->binnType = self::BINN_STORAGE_NOBYTES; @@ -403,7 +429,11 @@ public function binnFree() } /** - * + * Unpack value + * + * @param $varType + * @param $value + * @return bool|null */ protected function unpack($varType, $value) { @@ -439,7 +469,11 @@ protected function unpack($varType, $value) } /** - * + * Pack value + * + * @param $varType + * @param mixed $value + * @return null|string */ protected function pack($varType, $value = null) { @@ -477,6 +511,8 @@ protected function pack($varType, $value = null) } /** + * Pack varType + * * @param $type * @return string */ @@ -486,7 +522,9 @@ protected function packType($type) } /** - * @param $type + * Pack size info + * + * @param $size * @return string */ protected function packSize($size) @@ -495,7 +533,15 @@ protected function packSize($size) ? $this->pack(self::BINN_UINT8, $size) : $this->getInt32Binsize($size); } - + + /** + * Get size info + * data and meta (type info, size info, null bytes) + * + * @param $type + * @param string $value + * @return array + */ protected function getTypeSize($type, $value = '') { $size = ['meta' => 0, 'data' => 0]; diff --git a/tests/BinnTest.php b/tests/BinnTest.php index 3a71b4f..1b92a56 100644 --- a/tests/BinnTest.php +++ b/tests/BinnTest.php @@ -89,4 +89,16 @@ public function testSerializeTypes() $this->assertEquals($array, $unserialized, '', 0.000001); } + + public function testObject() + { + $array = ['name' => 'knik/binn', 'description' => 'Serialize to binary string.', 'keywords' => ["serialize", "bin"]]; + $object = (object) $array; + + $binn1 = new Binn; + $arraySerialized = $binn1->serialize($array); + $objectSerialized = $binn1->serialize($object); + + $this->assertEquals($arraySerialized, $objectSerialized); + } } \ No newline at end of file