PHPackages                             mkorkmaz/model\_utils - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. mkorkmaz/model\_utils

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

mkorkmaz/model\_utils
=====================

A simple PHP class for validating variable types, fixing, sanitising and setting default values for a model definition encoded as an array.

1.1.11(9y ago)0260MITPHPPHP &gt;=5.5

Since Mar 28Pushed 9y ago1 watchersCompare

[ Source](https://github.com/mkorkmaz/model_utils)[ Packagist](https://packagist.org/packages/mkorkmaz/model_utils)[ Docs](https://github.com/mkorkmaz/model_utils)[ RSS](/packages/mkorkmaz-model-utils/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (10)Dependencies (4)Versions (15)Used By (0)

ModelUtils
==========

[](#modelutils)

[![Circle CI](https://camo.githubusercontent.com/2fcf4dd4b00140942df8019f879a72b762e24cb2c9906d25af4d0ba07850630c/68747470733a2f2f636972636c6563692e636f6d2f67682f6d6b6f726b6d617a2f6d6f64656c5f7574696c732e7376673f7374796c653d736869656c64)](https://circleci.com/gh/mkorkmaz/model_utils)[![Latest Stable Version](https://camo.githubusercontent.com/17770a236fc5b292a4642534929e99ee48d338b0cf0f51d9e5caaa8fe379c138/68747470733a2f2f706f7365722e707567782e6f72672f6d6b6f726b6d617a2f6d6f64656c5f7574696c732f762f737461626c65)](https://packagist.org/packages/mkorkmaz/model_utils) [![Total Downloads](https://camo.githubusercontent.com/2530366f42bc3c0f21f80c41ad86732acdfb910b6a313e11b133bb2a4f5e2c1f/68747470733a2f2f706f7365722e707567782e6f72672f6d6b6f726b6d617a2f6d6f64656c5f7574696c732f646f776e6c6f616473)](https://packagist.org/packages/mkorkmaz/model_utils) [![Latest Unstable Version](https://camo.githubusercontent.com/5a41240b62804d29022e1a5dbcb4a4f7b9926b17045255faa6140f637c0b6792/68747470733a2f2f706f7365722e707567782e6f72672f6d6b6f726b6d617a2f6d6f64656c5f7574696c732f762f756e737461626c65)](https://packagist.org/packages/mkorkmaz/model_utils) [![License](https://camo.githubusercontent.com/429e2a4dfe18e5dcd2c019c54fab8768c7643705f758c2e3b6bc05383eba7f7d/68747470733a2f2f706f7365722e707567782e6f72672f6d6b6f726b6d617a2f6d6f64656c5f7574696c732f6c6963656e7365)](https://packagist.org/packages/mkorkmaz/model_utils)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/cfb3d8d6e38847b09d918416c93f18fb9d9bc8f304d8e6fa860b244c4393da7d/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d6b6f726b6d617a2f6d6f64656c5f7574696c732f6261646765732f7175616c6974792d73636f72652e706e673f623d6d617374657226763d312e312e34)](https://scrutinizer-ci.com/g/mkorkmaz/model_utils/?branch=master)[![Codacy Badge](https://camo.githubusercontent.com/97e735731fd252896b2e6d981d664735d53ca3307091859b45a2e7e9d508274f/68747470733a2f2f6170692e636f646163792e636f6d2f70726f6a6563742f62616467652f47726164652f3936333565363433363834633430396462663163316266336333646263373937)](https://www.codacy.com/app/mehmet/model_utils?utm_source=github.com&utm_medium=referral&utm_content=mkorkmaz/model_utils&utm_campaign=Badge_Grade)

A simple PHP class for validating variable types, fixing, sanitising and setting default values for a model definition encoded as an array. Can be used before inserting or updating documents.

This class is experimental, the default behaviour won't be changed but probably has some bugs to be fixed and needs to be extended for different type of cases.

This class can be used as a part of a complete ORM/ODM or in some low level database operations scripts.

Model definition
----------------

[](#model-definition)

\###\_type: To define variable type. Possible values: boolean, integer, float, string, array ###\_input\_type: To define more specific input type. Possible values: bool, date, time, datetime, timestamp, mail, url, ip, mac\_address or regex ###\_input\_format: If \_input\_type is set to regex, value of document items will be tested against this expression. ###\_min\_length: To define minimum length if string, minimum value if integer or float ###\_max\_length: To \\define maximum length if string, maximum value if integer or float ###\_required: To define if required doc item. Not used in the class for now ###\_index: To define if document item must be indexed in database. Not used in the class but can be used executing database operations. ###\_default: To define default value for the document item. ###\_ref: To define a relation with other documents like foreign key. Not used in the class but can be used executing database operations. ###\_has\_many: To define a relation with other documents that defined as child documents. Not used in the class but can be used executing database operations.

ModelUtils::validateDoc
-----------------------

[](#modelutilsvalidatedoc)

You can use it to test the document. Can be used before inserting documents. Validation throws an exception if there is a conflict.

ModelUtils::fitDocToModel
-------------------------

[](#modelutilsfitdoctomodel)

Can be used for fixing and sanitising partial documents. Can be used before updating documents.

ModelUtils::setModelDefaults
----------------------------

[](#modelutilssetmodeldefaults)

Can be used for fitting partial document according to model definition. Can be be used before both inserting or updating documents.

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

[](#installation)

It's recommended that you use [Composer](https://getcomposer.org/) to install ModelUtils.

```
$ composer require --prefer-dist mkorkmaz/model_utils "*"
```

This will install ModelUtils and all required dependencies. ModelUtils requires PHP 5.4.0 or newer. crisu83/shortid package need to support short ids.

Usage
-----

[](#usage)

```
use ModelUtils\ModelUtils as ModelUtils;

$doc = ModelUtils::fitDocToModel($model, $doc);
$doc = ModelUtils::setModelDefaults($model, $doc);
ModelUtils::validateDoc($model, $doc);

```

See also [test.php](https://github.com/mkorkmaz/model_utils/blob/master/test/test.php)

Contribute
----------

[](#contribute)

- Open issue if found bugs or sent pull request.
- Feel free to ask if have any questions.

###  Health Score

30

—

LowBetter than 62% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 96.3% 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

Every ~9 days

Total

14

Last Release

3618d ago

PHP version history (3 changes)1.0.x-devPHP &gt;=5.5.0

1.1.1PHP &gt;=5.4

1.1.5PHP &gt;=5.5

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/586318?v=4)[Haydar KÜLEKCİ](/maintainers/hkulekci)[@hkulekci](https://github.com/hkulekci)

![](https://www.gravatar.com/avatar/dd83e5d52a2538fdcddc2d17b3579ce5277473c87f46a12fda2d1ce1276c4e83?d=identicon)[mkorkmaz](/maintainers/mkorkmaz)

---

Top Contributors

[![mkorkmaz](https://avatars.githubusercontent.com/u/585601?v=4)](https://github.com/mkorkmaz "mkorkmaz (77 commits)")[![hkulekci](https://avatars.githubusercontent.com/u/586318?v=4)](https://github.com/hkulekci "hkulekci (3 commits)")

---

Tags

utilitymodel

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mkorkmaz-model-utils/health.svg)

```
[![Health](https://phpackages.com/badges/mkorkmaz-model-utils/health.svg)](https://phpackages.com/packages/mkorkmaz-model-utils)
```

###  Alternatives

[nette/utils

🛠 Nette Utils: lightweight utilities for string &amp; array manipulation, image handling, safe JSON encoding/decoding, validation, slug or strong password generating etc.

2.1k417.9M1.7k](/packages/nette-utils)[civicrm/civicrm-core

Open source constituent relationship management for non-profits, NGOs and advocacy organizations.

749284.3k35](/packages/civicrm-civicrm-core)[verbb/formie

The most user-friendly forms plugin for Craft.

100387.6k57](/packages/verbb-formie)[phpcsstandards/phpcsutils

A suite of utility functions for use with PHP\_CodeSniffer

6337.7M89](/packages/phpcsstandards-phpcsutils)[getdkan/dkan

DKAN Open Data Catalog

387137.8k2](/packages/getdkan-dkan)[oat-sa/tao-core

TAO core extension

66140.1k108](/packages/oat-sa-tao-core)

PHPackages © 2026

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