1
0
mirror of https://github.com/danog/PrimeModule.git synced 2024-12-02 09:37:54 +01:00
PrimeModule/tests/Struct.php

85 lines
1.9 KiB
PHP
Raw Normal View History

2017-02-25 13:06:41 +01:00
<?php
namespace danog\PHP;
/**
* PHPStruct
* PHP implementation of Python's struct module.
* This library was created to help me develop a [client for the mtproto protocol](https://github.com/danog/MadelineProto).
* The functions and the formats are exactly the ones used in python's struct (https://docs.python.org/3/library/struct.html)
* For now custom byte size may not work properly on certain machines for the f and d formats.
*
* @author Daniil Gentili <daniil@daniil.it>
* @license MIT license
*/
// Struct class (for static access)
class Struct
{
private static $struct = null;
/**
* constructor.
*
* Istantiates the PHPStruct class in a static variable
*
* @param $format Format string
*/
2017-02-25 13:07:04 +01:00
public static function constructor()
{
2017-02-25 13:06:41 +01:00
if (self::$struct == null) {
self::$struct = new \danog\PHP\StructTools();
}
}
2017-02-25 13:07:04 +01:00
2017-02-25 13:06:41 +01:00
/**
* pack.
*
* Packs data into bytes
*
* @param $format Format string
2017-02-25 11:40:07 +01:00
* @param $data Parameters to encode
2017-02-25 13:06:41 +01:00
*
* @return Encoded data
*/
2017-02-25 11:40:07 +01:00
public static function pack($format, $data)
2017-02-25 13:06:41 +01:00
{
self::constructor();
2017-02-25 13:07:04 +01:00
2017-02-25 11:40:07 +01:00
return self::$struct->pack($format, $data);
2017-02-25 13:06:41 +01:00
}
/**
* unpack.
*
* Unpacks data into an array
*
* @param $format Format string
* @param $data Data to decode
*
* @return Decoded data
*/
public static function unpack($format, $data)
{
self::constructor();
2017-02-25 13:07:04 +01:00
2017-02-25 13:06:41 +01:00
return self::$struct->unpack($format, $data);
}
/**
* calcsize.
*
* Return the size of the struct (and hence of the string) corresponding to the given format.
2017-12-22 12:06:46 +01:00
*
2017-02-25 13:06:41 +01:00
*
* @param $format Format string
*
* @return int with size of the struct.
*/
public static function calcsize($format)
{
self::constructor();
return self::$struct->calcsize($format);
}
}