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

Overview
NOTE!

I've found enough issues in this library to not use it. 
It remains here for historical purposes but I
would not recommend anyone expect it to work reliably as-is.

matchmaker

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

$books = [
    [
        'type' => 'book',
        'title' => 'Geography book',
        'chapters' => [
            'eu' => ['title' => 'Europe', 'interesting' => true],
            'as' => ['title' => 'America', 'interesting' => false]
        ]
    ],
    [
        'type' => 'book',
        'title' => 'Foreign languages book',
        'chapters' => [
            'de' => ['title' => 'Deutsch']
        ]
    ]
];

$pattern = [
    '*' => [
        'type' => 'book',
        'title' => ':string contains(book)',
        'chapters' => [
            ':string length(2) {1,3}' => [
                'title' => ':string',
                'interesting?' => ':bool',
            ]
        ]
    ]
];

matchmaker\matches($books, $pattern); // true

It could be used to check scalar values, objects or arrays from different sources (JSON, XML, Post Data).

Matching rules

Matching rules are strings that start with ':'. You can use multiple matchers joined with space. Matcher could be any callable (name of function or closure). You can add your own rules or replace standard ones.

  • General

  • empty

  • nonempty

  • required

  • in(a, b, ...)

  • mixed

  • any

  • Types

  • array

  • bool

  • boolean

  • callable

  • double

  • float

  • int

  • integer

  • long

  • numeric

  • number

  • object

  • real

  • resource

  • scalar

  • string

  • Numbers

  • gt(n)

  • gte(n)

  • lt(n)

  • lte(n)

  • negative

  • positive

  • between(a, b)

  • Strings

  • alnum

  • alpha

  • cntrl

  • digit

  • graph

  • lower

  • print

  • punct

  • space

  • upper

  • xdigit

  • regexp(pattern)

  • email

  • url

  • ip

  • length(n)

  • min(n)

  • max(n)

  • contains(needle)

  • starts(s)

  • ends(s)

  • json

  • date

  • Arrays

  • count(n)

  • keys(key1, key2, ...)

  • Objects

  • instance(class)

  • property(name, value)

  • method(name, value)

More details you can find here

Quantifiers for keys

  • ! - one key required (default)
  • ? - optional key
      • any count of keys
  • {3} - strict count of keys
  • {1,5} - range

For matchers (i.e. ':string') default quantifier is *

Installation

  • Install matchmaker via composer for your project:
composer require ptrofimov/matchmaker:*

License

Copyright (c) 2014 Petr Trofimov

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

You might also like...
Allows generate class files parse from json and map json to php object, including multi-level and complex objects;

nixihz/php-object Allows generate class files parse from json and map json to php object, including multi-level and complex objects; Installation You

The VarExporter component allows exporting any serializable PHP data structure to plain PHP code.

The VarExporter component allows exporting any serializable PHP data structure to plain PHP code. While doing so, it preserves all the semantics associated with the serialization mechanism of PHP (__wakeup, __sleep, Serializable).

YL MVC Structure (PHP MVC) is a pattern made in PHP used to implement user interfaces, data, and controlling logic.

YL MVC Structure (PHP MVC) is a pattern made in PHP used to implement user interfaces, data, and controlling logic. It is built based on the combination of ideas from the Yii framework and Laravel framework (yl).

PHP library that helps to map any input into a strongly-typed value object structure.

Valinor • PHP object mapper with strong type support Valinor is a PHP library that helps to map any input into a strongly-typed value object structure

CMS based on Phalcon PHP Framework with modular structure

Yona CMS Yona CMS - open source content management system (CMS). Written in Phalcon PHP Framework (version 3.x supported) Has a convenient modular str

this is a PHP framework like Laravel based on MVC structure

PHP-MVC-framework v 1-0-0 this is a PHP framework like Laravel based on MVC structure The "App" folder is the main part of this framework Models, View

A layout field that forces translations to have the same structure.
A layout field that forces translations to have the same structure.

Kirby translatedlayout plugin A layout field that forces translations to have anidentical structure to the one of the default language. This is an exp

Developer-friendly framework heavily inspired by Laravel and based on Timber and Wpemerge solutions for WordPress themes development with Bedrock folder structure

Lightweight Brocooly (Brocket) Improved Controllers, Middleware and Routers, but slightly less powerful Container Open beta. Package in development Cr

Admidio is a free open source user management system for websites of organizations and groups. The system has a flexible role model so that it’s possible to reflect the structure and permissions of your organization.
Admidio is a free open source user management system for websites of organizations and groups. The system has a flexible role model so that it’s possible to reflect the structure and permissions of your organization.

Admidio Admidio is a free open source user management system for websites of organizations and groups. The system has a flexible role model so that it

Owner
cd rubin
cd rubin
🏆 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

PHP School 311 Dec 30, 2022
Venture allows you to create and manage complex, async workflows in your Laravel apps.

Venture is a package to help you build and manage complex workflows of interdependent jobs using Laravel's queueing system. Installation Note: Venture

Kai Sassnowski 680 Dec 14, 2022
Here is the top 100 PHP functions: it is the list of the most often used PHP native functions

Here is the top 100 PHP functions: it is the list of the most often used PHP native functions. If you are a PHP developer, you must know the Top 100 PHP Functions deeply.

Max Base 16 Dec 11, 2022
Magento-Functions - A Resource of Magento Functions

Magento-Functions A Resource of Magento Functions Table of Contents Category Product User Cart Checkout General Account [Working w/ URL's] (#urls) Cat

Bryan Littlefield 28 Apr 19, 2021
provides a nested object property based user interface for accessing this configuration data within application code

laminas-config This package is considered feature-complete, and is now in security-only maintenance mode, following a decision by the Technical Steeri

Laminas Project 43 Dec 26, 2022
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.

Wojciech Mleczek 13 Jul 26, 2021
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 =>

Artem Zakirullin 192 Nov 28, 2022
Dobren Dragojević 6 Jun 11, 2023
Simple libary for functional programing paradigm with arrays.

rodrigodornelles/php-array-lib simple libary for functional programing paradigm with arrays Features Test driven development style (TDD) PHP version c

null 10 Nov 28, 2022
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

Alley Interactive 2 Aug 4, 2022