Type and shape system for arrays. Help write clearer code when implementing configs for your PocketMine-MP plugin or composer project.

Overview

ConfigStruct

Type and shape system for arrays. Help write clearer code when implementing configs for your PocketMine-MP plugin or composer project.

It also generates more human-readable errors when something is wrong with the data. Encouraging and guiding the user (especially some PocketMine-MP server owners) to read the error and fix their mess.

https://github.com/Sandertv/Marshal is an alternative that supports lower versions of PHP. However, it is not as bloat feature-rich as this library.

The section that everyone is looking for

Installation

Via Composer:

composer require endermanbugzjfc/configstruct

Via PocketMine-MP virion infection: https://poggit.pmmp.io/v.dl/Endermanbugzjfc/ConfigStruct/ConfigStruct/%5E2.0.0

Preview

Parse errors

2 errors in /Users/Shoghi/Documents/shog chips.yml
    1 errors in element "a"
        1 errors in index "0"
            1 errors in element "c"
                Element is array while it should be string
    1 errors in element "b"
        Element is null while it should be bool

Notice there is a trailing line break.

Developer guide

Parsing data

use Endermanbugzjfc\ConfigStruct\Parse;
$context = Parse::object($object, $data);
$context->copyToObject($object, $dataFilePath);

$dataFilePath will be displayed in error messages if there is any.

The errors will be wrapped and thrown with a ParseErrorsWrapper when calling copyToObject(). Although it is recommended to catch it, you can yet ignore it. Because the errors can still be displayed well in a PHP uncaught error message.

You may use Parse::objectByReflection() if you don't have an object but instead, its ReflectionClass instance. And use $context->copyToNewObject() to copy the parsed data to a new object.

Customising error message

Changing the root header label

/Users/Shoghi/Documents/shog chips.yml is the root header label in the following errors tree:

2 errors in /Users/Shoghi/Documents/shog chips.yml
    1 errors in element "a"
        1 errors in index "0"
            1 errors in element "c"
                Element is array while it should be string
    1 errors in element "b"
        Element is null while it should be bool

You can change it in the first argument of ParseErrorsWrapper::regenerateErrorMessage():

$parseErrorsWrapper->regenerateErrorMessage('C:\Windows\System32\ntoskrnl.exe');

Changing the indentation

You can change it in the second argument of ParseErrorsWrapper::regenerateErrorMessage()

Filtering errors

You can hide certain errors from the errors tree by filtering them out. Apply an error filter with the third argument of ParseErrorsWrapper::regenerateErrorMessage():

$parseErrorsWrapper->regenerateErrorMessage(
    $parseErrorsWrapper->getRootHeaderLabel(),
    $parseErrorsWrapper->getIndentation(),
    fn (array $keys, BaseParseError $parseError) : bool => !$parseError instanceof TypeMismatchError
);

This filters out all the TypeMismatchError. Although $parseError->getErrorsTree() will still have them, they will not be shown in the error message.

Print the updated error message

Simply throw the parse errors wrapper again. Or you may choose to echo $parseErrorsWrapper->getMessage(). By default, the error message has a trailing line break (\n). You can get an error message without the trailing line break (and other whitespaces) by calling $parseErrorsWrapper->getMessageRtrim() instead.

You might also like...
Make your PHP arrays sweet'n'safe

Mess We face a few problems in our PHP projects Illogical type casting (PHP's native implementation is way too "smart") Pointless casts like array =

An API for implementing leaderboards into your games

simple-leaderboard-api An API for implementing leaderboards into your games How to use it You'll first want to visit http://indiealchemy.com/simple-le

Drupal Composer Scaffold - A flexible Composer project scaffold builder

This project provides a composer plugin for placing scaffold files (like index.php, update.php, …) from the drupal/core project into their desired location inside the web root. Only individual files may be scaffolded with this plugin.

🏆 Learn You PHP! - An introduction to PHP's core features: i/o, http, arrays, exceptions and so on.
🏆 Learn You PHP! - An introduction to PHP's core features: i/o, http, arrays, exceptions and so on.

Learn You PHP! The very first PHP School workshop. A revolutionary new way to learn PHP Bring your imagination to life in an open learning eco-system

Safely break down arrays or objects, and put them back together in new shapes.

traverse/reshape traverse() and reshape() are companion functions that safely break down arrays or objects and put them back together in new shapes. t

This is a simple plugin for PocketMine-MP4 that will add a promo code system.

PromoSystem Author Download TG Releases This is a simple plugin for PocketMine-MP that will add a promo code system. Capabilities Two types of promo c

Composer Registrar Composer Plugin for Magento 2

This module add a global registration.php that replace the default glob search performed for each request to discover the components not installed from composer.

This package was created to provide simple way to manipulate arrays in PHP

PHP Collections This package was created to provide simple way to manipulate arrays in PHP. The package was inspired by the Laravel Collections.

Comments
  • PHPStan unfriendly

    PHPStan unfriendly

    The current API is so PHPStan unfriendly—especially the errors tree, since it is recursive, which cannot be expressed in PHPStan-type.

    I want to solve this by making each recursion level an object. Generic-type will also be used.

    opened by Endermanbugzjfc 1
  • Parse errors wrapper setting class

    Parse errors wrapper setting class

    Currently (2.0.0-BETA2), you must run $parseErrorsWrapper->regenerateErrorMessage() in order to customise the error message. Using one function for all settings is a probably bad idea because it is hard to maintain backward compatibility when adding or removing certain settings.

    Concept

    Users can extend the setting class to set their own settings. I am not sure if this is an abuse of class inheritance.

    API Break 
    opened by Endermanbugzjfc 0
Releases(2.1.0)
Owner
EndermanbugZJFC
scammer :shipit: (proved by @Hydros01)
EndermanbugZJFC
Pocketmine Plugin implementing Stackable Spawners

Information This plugin adds a Stackable Spawner System to PMMP. When you place a Spawner, you can stack additional Spawners onto the same Spawner to

null 33 Aug 17, 2022
PHP functions that help you validate structure of complex nested PHP arrays.

PHP functions that help you validate structure of complex nested PHP arrays.

cd rubin 7 May 22, 2022
A composer plugin, to install differenty types of composer packages in custom directories outside the default composer default installation path which is in the vendor folder.

composer-custom-directory-installer A composer plugin, to install differenty types of composer packages in custom directories outside the default comp

Mina Nabil Sami 136 Dec 30, 2022
Composer registry manager that help to easily switch to the composer repository you want

CRM - Composer Registry Manager Composer Registry Manager can help you easily and quickly switch between different composer repositories. 简体中文 Install

Tao 500 Dec 29, 2022
A plugin manager for PocketMine-MP downloads plugin from PocketMine-MP official plugin repository

oh-my-pmmp A plugin manager for PocketMine-MP Getting Started Prerequisites Your server MUST RUN the latest version of PocketMine. Installation From P

thebigcrafter 6 Jan 4, 2023
Ied plugin composer - Inspired Plugin Composer: Create, publish and edit plugins from within Textpattern CMS.

ied_plugin_composer Create, publish and edit plugins from within Textpattern CMS. Creates a new page under the Extensions tab where you can edit and e

Stef Dawson 8 Oct 3, 2020
Opinionated version of Wikimedia composer-merge-plugin to work in pair with Bamarni composer-bin-plugin.

Composer Inheritance Plugin Opinionated version of Wikimedia composer-merge-plugin to work in pair with bamarni/composer-bin-plugin. Usage If you are

Théo FIDRY 25 Dec 2, 2022
Preload your sweet sweet code to opcache with a composer command, making your code faster to run.

Composer Preload Preload your sweet sweet code to opcache with a composer command, making your code run faster. Composer Preload is a composer plugin

Ayesh Karunaratne 197 Dec 6, 2022
The main scope of this extension is to help phpstan to detect the type of object after the Assert\Assertion validation.

PHPStan beberlei/assert extension PHPStan beberlei/assert Description The main scope of this extension is to help phpstan to detect the type of object

PHPStan 33 Jan 2, 2023
Composer plugin that wraps all composer vendor packages inside your own namespace. Intended for WordPress plugins.

Imposter Plugin Composer plugin that wraps all composer vendor packages inside your own namespace. Intended for WordPress plugins. Built with ♥ by Typ

Typist Tech 127 Dec 17, 2022