PHPackages                             ryantxr/jsonmodel - 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. ryantxr/jsonmodel

ActiveLibrary

ryantxr/jsonmodel
=================

A class that uses JSON as a model.

1.0.0(1y ago)08MITPHPPHP ^7.4 || ^8.0

Since Jul 3Pushed 1y ago1 watchersCompare

[ Source](https://github.com/ryantxr/jsonmodel)[ Packagist](https://packagist.org/packages/ryantxr/jsonmodel)[ RSS](/packages/ryantxr-jsonmodel/feed)WikiDiscussions master Synced 1mo ago

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

Ryantxr\\JsonModel\\Model
=========================

[](#ryantxrjsonmodelmodel)

Overview
--------

[](#overview)

The Model class provides a JSON wrapper for parsing, manipulating, and exporting JSON data using dot notation and array syntax. It supports nested properties and arrays, with validation for key access to ensure only numeric indices are allowed within square brackets. Installation

To use this class, ensure you have PHP 7.4 or any version of PHP 8. You can include this class in your project via Composer. Composer Configuration

Install via Composer:
---------------------

[](#install-via-composer)

```
composer require ryantxr/jsonmodel

```

Usage
-----

[](#usage)

Class Instantiation

You can instantiate the Model class with an optional JSON string:

```
use Ryantxr\JsonModel\Model;

$jsonInput = '
{
    "foo" : {
        "bar" : {
            "baz": [
                {"a": 100},
                {"b": 200},
                {"c": 300}
            ],
            "fiz" : {
                "x": "xyzzy",
                "y": "yyzzy",
                "z": "zyzzy"
            },
            "buzz": [
                91, 92, 93, 94, 95
            ]
        }
    }
}';

$model = new Model($jsonInput);

$model = new \Ryantxr\Json\Model($jsonInput);
```

Methods
-------

[](#methods)

### get(string $var): mixed

[](#getstring-var-mixed)

Retrieves the value of a property using dot notation and array syntax.

#### Example:

[](#example)

```
$value = $model->get('foo.bar.baz[0].a'); // Returns 100

```

Throws:

```
InvalidArgumentException if the key format is invalid.

```

### set(string $var, $value): void

[](#setstring-var-value-void)

Sets the value of a property using dot notation and array syntax.

#### Example:

[](#example-1)

```
$model->set('foo.bar.buzz[1]', '1001');

```

Throws:

```
InvalidArgumentException if the key format is invalid.

```

### isset(string $var): bool

[](#issetstring-var-bool)

Checks if a variable exists using dot notation and array syntax.

#### Example:

[](#example-2)

```
$exists = $model->isset('foo.bar.baz'); // Returns true

```

### export(): string

[](#export-string)

Exports the current data as a JSON string.

#### Example:

[](#example-3)

```
$jsonOutput = $model->export();

```

### parse(string $json): void

[](#parsestring-json-void)

Parses and stores the JSON object.

#### Example:

[](#example-4)

```
$model->parse($jsonInput);

```

### parseKeys(string $keyString): array

[](#parsekeysstring-keystring-array)

Parses the keys and determines their types. Validates keys to ensure only numeric indices are allowed within square brackets.

Throws:

```
InvalidArgumentException if a key format is invalid.

```

Example Usage

```
use Ryantxr\JsonModel\Model;

$jsonInput = '
{
    "foo" : {
        "bar" : {
            "baz": [
                {"a": 100},
                {"b": 200},
                {"c": 300}
            ],
            "fiz" : {
                "x": "xyzzy",
                "y": "yyzzy",
                "z": "zyzzy"
            },
            "buzz": [
                91, 92, 93, 94, 95
            ]
        }
    }
}';

$model = new Model($jsonInput);

echo $model->get('foo.bar.baz[0].a'); // Output: 100
$model->set('foo.bar.buzz[1]', '1001');
echo $model->get('foo.bar.buzz[1]'); // Output: 1001

if ($model->isset('foo.bar')) {
    echo 'Property exists';
}

echo $model->export(); // Output: JSON string

```

Unit Testing
------------

[](#unit-testing)

The Model class can be tested using PHPUnit. Below is an example test class.

The Model class can be tested using PHPUnit. Below is an example test class. Example Test Class

```
