An open API service indexing awesome lists of open source software.

https://github.com/php-ffi/scalar-utils

PHP FFI Scalar Utilities
https://github.com/php-ffi/scalar-utils

Last synced: 11 months ago
JSON representation

PHP FFI Scalar Utilities

Awesome Lists containing this project

README

          

# FFI Scalar Utils


PHP 8.1+
Latest Stable Version
Latest Unstable Version
Total Downloads
License MIT




A set of util methods to interact with PHP and C scalar types.

## Requirements

- PHP >= 7.4
- ext-ffi

## Installation

Library is available as composer repository and can be installed using the
following command in a root of your project.

```sh
$ composer require ffi/scalar-utils
```

## Usage

### PHP To C Marshalling

```php
use FFI\Scalar\Type;

Type::int8(42); // object CData { cdata: 42 }
Type::int8Array([1, 2, 3]); // object CData { cdata: [1, 2, 3] }
Type::uint8(42); // object CData { cdata: 42 }
Type::uint8Array([1, 2, 3]); // object CData { cdata: [1, 2, 3] }

Type::int16(42); // object CData { cdata: 42 }
Type::int16Array([1, 2, 3]); // object CData { cdata: [1, 2, 3] }
Type::uint16(42); // object CData { cdata: 42 }
Type::uint16Array([1, 2, 3]); // object CData { cdata: [1, 2, 3] }

Type::int32(42); // object CData { cdata: 42 }
Type::int32Array([1, 2, 3]); // object CData { cdata: [1, 2, 3] }
Type::uint32(42); // object CData { cdata: 42 }
Type::uint32Array([1, 2, 3]); // object CData { cdata: [1, 2, 3] }

Type::int64(42); // object CData { cdata: 42 }
Type::int64Array([1, 2, 3]); // object CData { cdata: [1, 2, 3] }
Type::uint64(42); // object CData { cdata: 42 }
Type::uint64Array([1, 2, 3]); // object CData { cdata: [1, 2, 3] }

Type::short(42); // object CData { cdata: 42 }
Type::shortArray([1, 2, 3]); // object CData { cdata: [1, 2, 3] }
Type::ushort(42); // object CData { cdata: 42 }
Type::ushortArray([1, 2, 3]); // object CData { cdata: [1, 2, 3] }

Type::int(42); // object CData { cdata: 42 }
Type::intArray([1, 2, 3]); // object CData { cdata: [1, 2, 3] }
Type::uint(42); // object CData { cdata: 42 }
Type::uintArray([1, 2, 3]); // object CData { cdata: [1, 2, 3] }

Type::long(42); // object CData { cdata: 42 }
Type::longArray([1, 2, 3]); // object CData { cdata: [1, 2, 3] }
Type::ulong(42); // object CData { cdata: 42 }
Type::ulongArray([1, 2, 3]); // object CData { cdata: [1, 2, 3] }

Type::float(42); // object CData { cdata: 42.0 }
Type::floatArray([1, 2, 3]); // object CData { cdata: [1.0, 2.0, 3.0] }

Type::double(42); // object CData { cdata: 42.0 }
Type::doubleArray([1, 2, 3]); // object CData { cdata: [1.0, 2.0, 3.0] }

Type::longDouble(42); // object CData { cdata: 42.0 }
Type::longDoubleArray([1, 2, 3]); // object CData { cdata: [1.0, 2.0, 3.0] }

Type::bool(true); // object CData { cdata: true }
Type::boolArray([true, false, true]); // object CData { cdata: [true, false, true] }

Type::char('c'); // object CData { cdata: 'c' }
Type::charArray(['a', 'b', 'c']); // object CData { cdata: ['a', 'b', 'c'] }

Type::string('hi'); // object CData { cdata: ['h', 'i', '\0'] }
Type::stringArray(['a', 'b']); // object CData { cdata: [['a' '\0'], ['b', '\0']] }

Type::wideString('hi'); // object CData { cdata: ['h', 'i', '\0\0'] }
Type::wideStringArray(['a', 'b']); // object CData { cdata: [['a' '\0\0'], ['b', '\0\0']] }

// Direct API
Type::create($ffi->type('example'), $value); // object CData { cdata: ... }
Type::array($ffi->type('example'), [$value]); // object CData { cdata: [ ... ] }
```

### C To PHP Marshalling

```php
use FFI\Scalar\Type;

Type::toString($cdata); // string(x) "..."
Type::toWideString($cdata); // string(x) "..."
Type::toInt($cdata); // int(x)
Type::toFloat($cdata); // float(x)
Type::toBool($cdata); // bool(x)
Type::toArray($cdata); // array(...)
```