PHPackages                             instapro/schema-converter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. instapro/schema-converter

ActiveLibrary

instapro/schema-converter
=========================

Convert PHP types into schemas and cast values based on types.

1.0.0(6mo ago)04.4k↓29.5%MITPHPPHP &gt;=8.4CI passing

Since Nov 17Pushed 6mo agoCompare

[ Source](https://github.com/instapro/schema-converter)[ Packagist](https://packagist.org/packages/instapro/schema-converter)[ RSS](/packages/instapro-schema-converter/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (4)Versions (2)Used By (0)

Schema Converter
================

[](#schema-converter)

[![CI](https://github.com/instapro/schema-converter/actions/workflows/ci.yml/badge.svg)](https://github.com/instapro/schema-converter/actions/workflows/ci.yml)[![Packagist Version](https://camo.githubusercontent.com/642c8de13c50f6f7a6dd74e6c900159e7c08aa4bb4f0306e39db2c223e28ced3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f696e73746170726f2f736368656d612d636f6e766572746572)](https://packagist.org/packages/instapro/schema-converter)[![PHP Version](https://camo.githubusercontent.com/358e514bbee120c4fcba43ac266323dee0aea5caa059e4b5bd8c52d908ba5d3e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f696e73746170726f2f736368656d612d636f6e766572746572)](https://packagist.org/packages/instapro/schema-converter)[![License](https://camo.githubusercontent.com/8f12da55799d702a40b3d0331c63a23951cb9b60e4edcfb7a08ffabcb340ecaf/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f696e73746170726f2f736368656d612d636f6e766572746572)](https://github.com/instapro/schema-converter/blob/main/LICENSE)[![Total Downloads](https://camo.githubusercontent.com/ebc3868f140b470bc07675901c321d67d705e9d7423d3d5052749f2ed307884a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f696e73746170726f2f736368656d612d636f6e766572746572)](https://packagist.org/packages/instapro/schema-converter)

Overview
--------

[](#overview)

This library provides functionality to convert PHP types into schemas and cast values based on types.

Installation
------------

[](#installation)

You can install the library via Composer:

```
composer require instapro/schema-converter
```

Usage
-----

[](#usage)

There are several schema converters that can be used to convert PHP types into schemas. Bellow an example using the most basic converter which can be used to convert primitive types:

```
$converter = new \Instapro\SchemaConverter\PrimitiveConverter();

echo json_encode($converter->toSchema('string')->toArray()); // {"type": "string"}
```

You can also cast values based on the schema:

```
$converter = new \Instapro\SchemaConverter\PrimitiveConverter();

var_dump($converter->castValue('string', 12.3)); // string(4) "12.3"
```

Supported converters
--------------------

[](#supported-converters)

### `PrimitiveConverter`

[](#primitiveconverter)

The primitive converter can be used to convert primitive types into schemas. Primitives can be `array`, `string`, `int`, `float`, `bool`, `mixed`, or `null`.

You can see examples of how to use the primitive converter in the previous section.

### `ObjectConverter`

[](#objectconverter)

The object converter can be used to convert objects into schemas. The converter will recursively convert the properties of the object into schemas, but it will require you to define a converter to deal with that recursively.

For the following example, we'll let the converter handle the following class:

```
final readonly class Person
{
    public function __construct(
        public string $name,
        public int $age,
    ) {}
}
```

You can use the converter to get a schema of any class:

```
$converter = new \Instapro\SchemaConverter\ObjectConverter();
$converter->bindConverter(new \Instapro\SchemaConverter\PrimitiveConverter());

echo json_encode($converter->toSchema(Person::class)->toArray(), JSON_PRETTY_PRINT);
```

The code above will output the following schema:

```
{
  "type": "object",
  "parameters": {
    "name": {
      "required": true,
      "type": "string"
    },
    "age": {
      "required": true,
      "type": "int"
    }
  }
}
```

You can also cast values based on a type, as long you use the same format as the schema:

```
$converter = new \Instapro\SchemaConverter\ObjectConverter();
$converter->bindConverter(new \Instapro\SchemaConverter\PrimitiveConverter());

$person = $converter->castValue(Person::class, [
    'name' => 'John Doe',
    'age' => 30,
]);

echo $person->name; // John Doe
```

**Disclosure**: Note that this converter has a `bindConverter` instead of a constructor argument. This was designed to help with recursive conversions.

### `EntityConverter`

[](#entityconverter)

The entity converter can be used to convert Doctrine entities into schemas.

```
$converter = new \Instapro\SchemaConverter\EntityConverter($entityManager);

echo json_encode($converter->toSchema(MyEntity::class)->toArray()); // {"type": "identifier"}
echo $converter->castValue(MyEntity::class, 123)->id; // 123
```

### `DateTimeConverter`

[](#datetimeconverter)

The `DateTimeConverter` can handle objects that implement `DateTimeIterface`.

```
$converter = new \Instapro\SchemaConverter\DateTimeConverter();

echo json_encode($converter->toSchema(\DateTime::class)->toArray()); // {"type": "datetime"}
echo PHP_EOL;

$dateTime = $converter->castValue(\DateTime::class, '2021-01-01T00:00:00Z');
echo $dateTime->format('Y-m-d'); // 2021-01-01
```

### `EnumConverter`

[](#enumconverter)

The `EnumConverter` can handle PHP enumerations.

```
$converter = new \Instapro\SchemaConverter\EnumConverter();

echo json_encode($converter->toSchema(MyEnum::class)->toArray()); // {"type": "enum", "values": ["VALUE1", "VALUE2"]}
echo PHP_EOL;

$enum = $converter->castValue(MyEnum::class, 'VALUE1');
echo $enum->getValue(); // VALUE1
```

### `EntityByKeyConverter`

[](#entitybykeyconverter)

This converter handles Doctrine entities by their key properties.

```
$entityManager = // get your EntityManager instance
$converter = new \Instapro\SchemaConverter\EntityByKeyConverter(
    $entityManager,
    'postal_code',
    MyPostalCodeEntity::class,
    'postalCode'
);

echo json_encode($converter->toSchema(MyPostalCodeEntity::class)->toArray()); // {"type": "postal_code"}
echo PHP_EOL;

$entity = $converter->castValue(MyPostalCodeEntity::class, '1017BS');
echo $entity->postalCode; // 1017BS
```

### `JsonArrayConverter`

[](#jsonarrayconverter)

This converter handles JSON arrays.

```
$converter = new \Instapro\SchemaConverter\JsonArrayConverter();

echo json_encode($converter->toSchema('array')->toArray()); // {"type": "json"}
echo PHP_EOL;

$array = $converter->castValue('array', '[1, 2, 3]');
print_r($array); // Array ( [0] => 1 [1] => 2 [2] => 3 )
```

### `CompositeConverter`

[](#compositeconverter)

There are several types of schemas that can be generated. For convenience, the library provides a `CompositeConverter` that can be used to convert multiple types of schemas.

```
$converter = new \Instapro\SchemaConverter\CompositeConverter(
    new \Instapro\SchemaConverter\PrimitiveConverter(),
    new \Instapro\SchemaConverter\DateTimeConverter(),
    new \Instapro\SchemaConverter\ObjectConverter(),
    new \Instapro\SchemaConverter\EntityConverter($entityManager);
);

echo json_encode($converter->toSchema(SomethingComplex::class)->toArray()); // {"type": "object", "parameters": {...}}

$somethingComplex = $converter->castValue(SomethingComplex::class, $complextData);
$somethingComplex->doSomething();
```

**Note**: The `CompositeConverter` will automatically call `setConverter()` for the `ObjectConverter` with itself.

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance69

Regular maintenance activity

Popularity24

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

182d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/154023?v=4)[Henrique Moody](/maintainers/henriquemoody)[@henriquemoody](https://github.com/henriquemoody)

---

Top Contributors

[![henriquemoody](https://avatars.githubusercontent.com/u/154023?v=4)](https://github.com/henriquemoody "henriquemoody (9 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/instapro-schema-converter/health.svg)

```
[![Health](https://phpackages.com/badges/instapro-schema-converter/health.svg)](https://phpackages.com/packages/instapro-schema-converter)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
