{"id":20841631,"url":"https://github.com/spomky-labs/cbor-php","last_synced_at":"2025-04-05T08:08:32.873Z","repository":{"id":45166133,"uuid":"124222837","full_name":"Spomky-Labs/cbor-php","owner":"Spomky-Labs","description":"CBOR Encoder/Decoder for PHP","archived":false,"fork":false,"pushed_at":"2024-04-13T21:15:55.000Z","size":511,"stargazers_count":40,"open_issues_count":3,"forks_count":12,"subscribers_count":5,"default_branch":"3.1.x","last_synced_at":"2024-04-14T04:04:16.578Z","etag":null,"topics":["cbor","rfc7049"],"latest_commit_sha":null,"homepage":null,"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/Spomky-Labs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"Spomky","patreon":"FlorentMorselli"}},"created_at":"2018-03-07T10:58:50.000Z","updated_at":"2024-04-26T13:53:37.021Z","dependencies_parsed_at":"2024-01-29T18:59:44.123Z","dependency_job_id":"26bbadfc-733a-436b-bf08-10fd13ee7dca","html_url":"https://github.com/Spomky-Labs/cbor-php","commit_stats":{"total_commits":148,"total_committers":9,"mean_commits":"16.444444444444443","dds":0.472972972972973,"last_synced_commit":"9311da5042422da216e6c9ba31d525a45a4689b2"},"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Spomky-Labs%2Fcbor-php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Spomky-Labs%2Fcbor-php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Spomky-Labs%2Fcbor-php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Spomky-Labs%2Fcbor-php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Spomky-Labs","download_url":"https://codeload.github.com/Spomky-Labs/cbor-php/tar.gz/refs/heads/3.1.x","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247305935,"owners_count":20917208,"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":["cbor","rfc7049"],"created_at":"2024-11-18T01:21:03.263Z","updated_at":"2025-04-05T08:08:32.851Z","avatar_url":"https://github.com/Spomky-Labs.png","language":"PHP","funding_links":["https://github.com/sponsors/Spomky","https://patreon.com/FlorentMorselli","https://www.patreon.com/FlorentMorselli"],"categories":[],"sub_categories":[],"readme":"CBOR for PHP\n============\n\n![Build Status](https://github.com/Spomky-Labs/cbor-php/workflows/Unit%20and%20Functional%20Tests/badge.svg)\n![Build Status](https://github.com/Spomky-Labs/cbor-php/workflows/Mutation%20Testing/badge.svg)\n\n![Coding Standards](https://github.com/Spomky-Labs/cbor-php/workflows/Coding%20Standards/badge.svg)\n![Static Analyze](https://github.com/Spomky-Labs/cbor-php/workflows/Static%20Analyze/badge.svg)\n\n[![Latest Stable Version](https://poser.pugx.org/Spomky-Labs/cbor-php/v)](//packagist.org/packages/Spomky-Labs/cbor-php)\n[![Total Downloads](https://poser.pugx.org/Spomky-Labs/cbor-php/downloads)](//packagist.org/packages/Spomky-Labs/cbor-php)\n[![Latest Unstable Version](https://poser.pugx.org/Spomky-Labs/cbor-php/v/unstable)](//packagist.org/packages/Spomky-Labs/cbor-php)\n[![License](https://poser.pugx.org/Spomky-Labs/cbor-php/license)](//packagist.org/packages/Spomky-Labs/cbor-php)\n\n# Scope\n\nThis library will help you to decode and create objects using the Concise Binary Object Representation (CBOR - [RFC8949](https://tools.ietf.org/html/rfc8949)).\n\n# Installation\n\nInstall the library with Composer: `composer require spomky-labs/cbor-php`.\n\nThis project follows the [semantic versioning](http://semver.org/) strictly.\n\n# Support\n\nI bring solutions to your problems and answer your questions.\n\nIf you really love that project, and the work I have done or if you want I prioritize your issues, then you can help me out for a couple of :beers: or more!\n\n[Become a sponsor](https://github.com/sponsors/Spomky)\n\nOr\n\n[![Become a Patreon](https://c5.patreon.com/external/logo/become_a_patron_button.png)](https://www.patreon.com/FlorentMorselli)\n\n# Documentation\n\n## Object Creation\n\nThis library supports all Major Types defined in the RFC8949 and has capabilities to support any kind of Tags (Major Type 6) and Other Objects (Major Type 7).\n\nEach object have at least:\n\n* a static method `create`. This method will correctly instantiate the object.\n* can be converted into a binary string: `$object-\u003e__toString();` or `(string) $object`.\n\n### Positive Integer (Major Type 0)\n\n```php\n\u003c?php\n\nuse CBOR\\UnsignedIntegerObject;\n\n$object = UnsignedIntegerObject::create(10);\n$object = UnsignedIntegerObject::create(1000);\n$object = UnsignedIntegerObject::create(10000);\n$object = UnsignedIntegerObject::createFromHex('0AFFEBFF');\n\necho bin2hex((string)$object); // 1a0affebff\n```\n\n### Negative Integer (Major Type 1)\n\n```php\n\u003c?php\n\nuse CBOR\\NegativeIntegerObject;\n\n$object = NegativeIntegerObject::create(-10);\n$object = NegativeIntegerObject::create(-1000);\n$object = NegativeIntegerObject::create(-10000);\n```\n\n### Byte String / Indefinite Length Byte String (Major Type 2)\n\nByte String and Indefinite Length Byte String objects have the same major type but are handled by two different classes in this library.\n\n```php\n\u003c?php\n\nuse CBOR\\ByteStringObject; // Byte String\nuse CBOR\\IndefiniteLengthByteStringObject; // Indefinite Length Byte String\n\n// Create a Byte String with value \"Hello\"\n$object = ByteStringObject::create('Hello');\n\n// Create an Indefinite Length Byte String with value \"Hello\" (\"He\" + \"\" + \"ll\" + \"o\")\n$object = IndefiniteLengthByteStringObject::create()\n    -\u003eappend('He')\n    -\u003eappend('')\n    -\u003eappend('ll')\n    -\u003eappend('o')\n;\n```\n\n### Text String / Indefinite Length Text String (Major Type 3)\n\nText String and Indefinite Length Text String objects have the same major type but are handled by two different classes in this library.\n\n```php\n\u003c?php\n\nuse CBOR\\TextStringObject; // Text String\nuse CBOR\\IndefiniteLengthTextStringObject; // Indefinite Length Text String\n\n// Create a Text String with value \"(｡◕‿◕｡)⚡\"\n$object = TextStringObject::create('(｡◕‿◕｡)⚡');\n\n// Create an Indefinite Length Text String with value \"(｡◕‿◕｡)⚡\" (\"(｡◕\" + \"\" + \"‿◕\" + \"｡)⚡\")\n$object = IndefiniteLengthTextStringObject::create()\n    -\u003eappend('(｡◕')\n    -\u003eappend('')\n    -\u003eappend('‿◕')\n    -\u003eappend('｡)⚡')\n;\n```\n\n### List / Indefinite Length List (Major Type 4)\n\nList and Indefinite Length List objects have the same major type but are handled by two different classes in this library.\nItems in the List object can be any of CBOR Object type.\n\n```php\n\u003c?php\n\nuse CBOR\\ListObject; // List\nuse CBOR\\IndefiniteLengthListObject; // Infinite List\nuse CBOR\\TextStringObject;\nuse CBOR\\UnsignedIntegerObject;\n\n// Create a List with a single item\n$object = ListObject::create()\n    -\u003eadd(TextStringObject::create('(｡◕‿◕｡)⚡'))\n;\n\n// Create an Infinite List with several items\n$object = IndefiniteLengthListObject::create()\n    -\u003eadd(TextStringObject::create('(｡◕‿◕｡)⚡'))\n    -\u003eadd(UnsignedIntegerObject::create(25))\n;\n```\n\n### Map / Indefinite Length Map (Major Type 5)\n\nMap and Indefinite Length Map objects have the same major type but are handled by two different classes in this library.\nKeys and values in the Map object can be any of CBOR Object type.\n\n**However, be really careful with keys. Please follow the recommendation hereunder:**\n\n* Keys should not be duplicated\n* Keys should be of type Positive or Negative Integer, (Indefinite Length)Byte String or (Indefinite Length)Text String. Other types may cause errors.\n\n```php\n\u003c?php\n\nuse CBOR\\MapObject; // Map\nuse CBOR\\IndefiniteLengthMapObject; // Infinite Map\nuse CBOR\\ByteStringObject;\nuse CBOR\\TextStringObject;\nuse CBOR\\UnsignedIntegerObject;\nuse CBOR\\NegativeIntegerObject;\n\n// Create a Map with a single item\n$object = MapObject::create()\n    -\u003eadd(UnsignedIntegerObject::create(25), TextStringObject::create('(｡◕‿◕｡)⚡'))\n;\n\n// Create an Infinite Map with several items\n$object = IndefiniteLengthMapObject::create()\n    -\u003eappend(ByteStringObject::create('A'), NegativeIntegerObject::create(-652))\n    -\u003eappend(UnsignedIntegerObject::create(25), TextStringObject::create('(｡◕‿◕｡)⚡'))\n;\n```\n\n### Tags (Major Type 6)\n\nThis library can support any kind of tags.\nIt comes with some of the thew described in the specification:\n\n* Base 16 encoding\n* Base 64 encoding\n* Base 64 Url Safe encoding\n* Big Float\n* Decimal Fraction\n* Epoch\n* Timestamp\n* Positive Big Integer\n* Negative Big Integer\n\nYou can easily create your own tag by extending the abstract class `CBOR\\TagObject`.\nThis library provides a `CBOR\\Tag\\GenericTag` class that can be used for any other unknown/unsupported tags.\n\n```php\n\u003c?php\n\nuse CBOR\\Tag\\TimestampTag;\nuse CBOR\\UnsignedIntegerObject;\n\n// Create an unsigned object that represents the current timestamp\n$object = UnsignedIntegerObject::create(time()); // e.g. 1525873787\n\n//We tag the object with the Timestamp Tag\n$taggedObject = TimestampTag::create($object); // Returns a \\DateTimeImmutable object with timestamp at 1525873787\n```\n\n### Other Objects (Major Type 7)\n\nThis library can support any kind of \"other objects\".\nIt comes with some of the thew described in the specification:\n\n* False\n* True\n* Null\n* Undefined\n* Half Precision Float\n* Single Precision Float\n* Double Precision Float\n* Simple Value\n\nYou can easily create your own object by extending the abstract class `CBOR\\OtherObject`.\nThis library provides a `CBOR\\OtherObject\\GenericTag` class that can be used for any other unknown/unsupported objects.\n\n**Because PHP does not support an 'undefined' object, the normalization method will return `'undefined'`.**\n\n```php\n\u003c?php\n\nuse CBOR\\OtherObject\\FalseObject;\nuse CBOR\\OtherObject\\NullObject;\nuse CBOR\\OtherObject\\UndefinedObject;\n\n$object = FalseObject::create();\n$object = NullObject::create();\n$object = UndefinedObject::create();\n```\n\n## Example\n\n```php\n\u003c?php\n\nuse CBOR\\MapObject;\nuse CBOR\\OtherObject\\UndefinedObject;\nuse CBOR\\TextStringObject;\nuse CBOR\\ListObject;\nuse CBOR\\NegativeIntegerObject;\nuse CBOR\\UnsignedIntegerObject;\nuse CBOR\\OtherObject\\TrueObject;\nuse CBOR\\OtherObject\\FalseObject;\nuse CBOR\\OtherObject\\NullObject;\nuse CBOR\\Tag\\DecimalFractionTag;\nuse CBOR\\Tag\\TimestampTag;\n\n$object = MapObject::create()\n    -\u003eadd(\n        TextStringObject::create('(｡◕‿◕｡)⚡'),\n        ListObject::create([\n            TrueObject::create(),\n            FalseObject::create(),\n            UndefinedObject::create(),\n            DecimalFractionTag::createFromExponentAndMantissa(\n                NegativeIntegerObject::create(-2),\n                UnsignedIntegerObject::create(1234)\n            ),\n        ])\n    )\n    -\u003eadd(\n        UnsignedIntegerObject::create(2000),\n        NullObject::create()\n    )\n    -\u003eadd(\n        TextStringObject::create('date'),\n        TimestampTag::create(UnsignedIntegerObject::create(1577836800))\n    )\n;\n```\n\nThe encoded result will be `0xa37428efbda1e29795e280bfe29795efbda129e29aa183f5f4c482211904d21907d0f66464617465c11a5e0be100`.\n\n## Object Loading\n\nIf you want to load a CBOR encoded string, you just have to instantiate a `CBOR\\Decoder` class.\nThis class does not need any argument.\n\n```php\n\u003c?php\n\nuse CBOR\\Decoder;\n\n$decoder = Decoder::create();\n```\n\nIf needed, you can define custom sets of Tag and Other Object support managers.\n\n```php\n\u003c?php\n\nuse CBOR\\Decoder;\nuse CBOR\\OtherObject;\nuse CBOR\\Tag;\n\n$otherObjectManager = OtherObject\\OtherObjectManager::create()\n    -\u003eadd(OtherObject\\SimpleObject::class)\n    -\u003eadd(OtherObject\\FalseObject::class)\n    -\u003eadd(OtherObject\\TrueObject::class)\n    -\u003eadd(OtherObject\\NullObject::class)\n    -\u003eadd(OtherObject\\UndefinedObject::class)\n    -\u003eadd(OtherObject\\HalfPrecisionFloatObject::class)\n    -\u003eadd(OtherObject\\SinglePrecisionFloatObject::class)\n    -\u003eadd(OtherObject\\DoublePrecisionFloatObject::class)\n;\n\n$tagManager = Tag\\TagManager::create()\n    -\u003eadd(Tag\\DatetimeTag::class)\n    -\u003eadd(Tag\\TimestampTag::class)\n    -\u003eadd(Tag\\UnsignedBigIntegerTag::class)\n    -\u003eadd(Tag\\NegativeBigIntegerTag::class)\n    -\u003eadd(Tag\\DecimalFractionTag::class)\n    -\u003eadd(Tag\\BigFloatTag::class)\n    -\u003eadd(Tag\\Base64UrlEncodingTag::class)\n    -\u003eadd(Tag\\Base64EncodingTag::class)\n    -\u003eadd(Tag\\Base16EncodingTag::class)\n;\n\n$decoder = Decoder::create($tagManager, $otherObjectManager);\n```\n\nThen, the decoder will read the data you want to load.\nThe data has to be handled by an object that implements the `CBOR\\Stream` interface.\nThis library provides a `CBOR\\StringStream` class to stream the string.\n\n```php\n\u003c?php\n\nuse CBOR\\StringStream;\n\n// CBOR object (in hex for the example)\n$data = hex2bin('fb3fd5555555555555');\n\n// String Stream\n$stream = StringStream::create($data);\n\n// Load the data\n$object = $decoder-\u003edecode($stream); // Return a CBOR\\OtherObject\\DoublePrecisionFloatObject class with normalized value ~0.3333 (1/3)\n```\n\n# Contributing\n\nRequests for new features, bug fixed and all other ideas to make this project useful are welcome.\nThe best contribution you could provide is by fixing the [opened issues where help is wanted](https://github.com/Spomky-Labs/cbor-php/issues?q=is%3Aissue+is%3Aopen+label%3A%22help+wanted%22).\n\nPlease report all issues in [the main repository](https://github.com/Spomky-Labs/cbor-php/issues).\n\nPlease make sure to [follow these best practices](.github/CONTRIBUTING.md).\n\n# Licence\n\nThis project is release under [MIT licence](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspomky-labs%2Fcbor-php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspomky-labs%2Fcbor-php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspomky-labs%2Fcbor-php/lists"}