PHPackages                             viral-agency/master-caster - 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. viral-agency/master-caster

ActiveLibrary

viral-agency/master-caster
==========================

Easily cast JSON API responses into custom objects models

v1.5.0(6mo ago)040Apache-2.0PHPPHP &gt;=8.0

Since Apr 4Pushed 6mo ago1 watchersCompare

[ Source](https://github.com/ViralAgency/MasterCaster)[ Packagist](https://packagist.org/packages/viral-agency/master-caster)[ RSS](/packages/viral-agency-master-caster/feed)WikiDiscussions master Synced 1mo ago

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

PHP MasterCaster
================

[](#php-mastercaster)

[![Latest Stable Version](https://camo.githubusercontent.com/921eeb95d2a31bb34a1008913b1b7e09948d99836541ef7f1855f9daab7ef126/68747470733a2f2f706f7365722e707567782e6f72672f766972616c2d6167656e63792f6d61737465722d6361737465722f762f737461626c65)](https://packagist.org/packages/viral-agency/master-caster)[![Total Downloads](https://camo.githubusercontent.com/331496bf61f7b3676b2e6675811db11009363ec4822262b3eb0661da80ef2a29/68747470733a2f2f706f7365722e707567782e6f72672f766972616c2d6167656e63792f6d61737465722d6361737465722f646f776e6c6f616473)](https://packagist.org/packages/viral-agency/master-caster)[![License](https://camo.githubusercontent.com/43faca9a6893d9e3473b0a5448f7cb9749b85cd9953aa2ccd719f2e9026b2da4/68747470733a2f2f706f7365722e707567782e6f72672f766972616c2d6167656e63792f6d61737465722d6361737465722f6c6963656e7365)](https://packagist.org/packages/viral-agency/master-caster)

Unlike other strongly typed languages (such as Java), PHP typically converts API requests from/to JSON using arrays. The problem with this approach is that it is impossible to know upfront the number, names, or types of properties being converted. This leads to unpredictable scenarios and makes the code harder to maintain. As a result, developers are often forced into complex technical workarounds, which, over time, become unmanageable. This is especially true when dealing with deeply nested JSON properties, where developers are forced to write iterative loops, resulting in messy and poorly maintainable code. MasterCaster bridges the gap between strong typing and PHP's flexibility. It allows you to define a model where the properties of objects are specified in advance, based on the third-party API documentation. This enables JSON responses to be dynamically and recursively converted into specific objects. At the same time, it lets you define custom methods within the classes, taking full advantage of OOP principles such as inheritance, encapsulation, and polymorphism.

In short, it’s a small revolution in PHP.

Requirements
------------

[](#requirements)

- PHP &gt;= 8.0
- Composer (you can install from [here](https://getcomposer.org/download/))
- A well-written third-party API documentation (in order to fit the requirements out-of-the-box, the JSON representation of the response have to meet the [Google JSON style guide](https://google.github.io/styleguide/jsoncstyleguide.xml), in particularly way the [array naming convention](https://google.github.io/styleguide/jsoncstyleguide.xml?showone=Singular_vs_Plural_Property_Names#Singular_vs_Plural_Property_Names))

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

[](#installation)

To install the package, run the following command using Composer:

```
composer require viral-agency/master-caster
```

Alternatively, you can add the package to your `composer.json` file:

```
"require": {
"viral-agency/master-caster": "^1.0.0"
}
```

Then, run:

```
composer install viral-agency/master-caster
```

Usage
-----

[](#usage)

You can easily to use MasterCaster by defining a model from the third party API documentation:

### Sample third-party API documentation

[](#sample-third-party-api-documentation)

Here a sample third party API documentation that fits the [Google JSON style guide](https://google.github.io/styleguide/jsoncstyleguide.xml)

```
{
  "sampleInt": 5,
  "sampleString": "sample_string",
  "sampleObject": {
    "sampleString": "sample_string",
    "sampleInt": 2
  },
  "sampleObjects": [
    {
      "sampleString": "sample_string",
      "sampleInt": 2
    },
    {
      "sampleString": "sample_string",
      "sampleInt": 2
    }
  ]
}
```

### Define the models

[](#define-the-models)

Define each elements of the JSON representation by coping directly from the documentation, in order to be sure that's correct.

#### Sample Response Class

[](#sample-response-class)

```
