PHPackages                             olekhy/view-model-service - 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. [Templating &amp; Views](/categories/templating)
4. /
5. olekhy/view-model-service

ActiveLibrary[Templating &amp; Views](/categories/templating)

olekhy/view-model-service
=========================

Helper for view modeling

v0.1.0(12y ago)05951MITPHPPHP &gt;=5.3.3

Since May 11Pushed 11y ago1 watchersCompare

[ Source](https://github.com/olekhy/view-model-service)[ Packagist](https://packagist.org/packages/olekhy/view-model-service)[ Docs](https://github.com/olekhy/view-model-service)[ RSS](/packages/olekhy-view-model-service/feed)WikiDiscussions master Synced today

READMEChangelog (1)Dependencies (4)Versions (5)Used By (0)

View Model service
==================

[](#view-model-service)

[![Build Status](https://camo.githubusercontent.com/3c056ff6216b8a30f93788914c46a8bf6f64b13382de2e910cc09118493eda50/68747470733a2f2f7472617669732d63692e6f72672f6f6c656b68792f766965772d6d6f64656c2d736572766963652e737667)](https://travis-ci.org/olekhy/view-model-service)

[![Scrutinizer Code Quality](https://camo.githubusercontent.com/cd5de166834b885665a95abcd3e81fec77a8c1979fb2c28920e53e5dffd49913/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6f6c656b68792f766965772d6d6f64656c2d736572766963652f6261646765732f7175616c6974792d73636f72652e706e673f733d33356139316130646237366432623466336336333863343861323665323566366632303135333336)](https://scrutinizer-ci.com/g/olekhy/view-model-service/)

[![Coverage Status](https://camo.githubusercontent.com/2774f883d2cc27639eea59c5e43d21f2addb505db7e4186b60a8336ac5c53510/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6f6c656b68792f766965772d6d6f64656c2d736572766963652f62616467652e706e673f6272616e63683d646576656c6f70)](https://coveralls.io/r/olekhy/view-model-service?branch=master)

[![Coverage Status](https://camo.githubusercontent.com/2774f883d2cc27639eea59c5e43d21f2addb505db7e4186b60a8336ac5c53510/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6f6c656b68792f766965772d6d6f64656c2d736572766963652f62616467652e706e673f6272616e63683d646576656c6f70)](https://coveralls.io/r/olekhy/view-model-service?branch=develop)

The module named "ViewModelService" could be used for:

- Inserting data obtained from database or user's input in the instance of the View Model via appropriate mapper.
- Latter inserting (applying) data to the View Model and building an instance at the time when View Model was called in template.
- Convenience at coding in IDE from the autocomplete for the View Models in the view template.
- Flexible and secure approach of the logic how data will be mapped to the View Model (Using of mappers).
- Lazy loading data in view model via Repository without using of controller.

\#Explaining of the process and root idea

Firstly we will have in codebase of project many View Models classes which oneself a simples presentation of variables that we obtained from database or another sources and will be using in our view templates. All of the View Model classes needed to be named by convention **ConcreteNameViewModel** and must implement an interface **ViewModelInterface**.

Parallel to the view models we create View Model Mapper classes, the purpose of the mapper class is the mapping data to the View Model instance. Naming convention for the Mapper class is similar to the View Model naming and and looks like this **ConcreteNameViewMapper**. All mappers implement an interface **ViewMapperInterface** and must realizing the map() method which contains logic that describes how will be mapped data to the View Model.

Process of applying of data is internally segregated into two logical independent processes, first is the recording in the repository a recipe which describes how can the View Model created and second is creation of a real View Model instance. The purpose of this segregation is performance of execution of the request as simple as we can say the View Model Instance will be created only at calling also on requirement in the template.

Next we explain how we could work with the View Model Service

As for example we are at Request in UserController::actionShowProfile()

```
use ViewModelService\ViewModelRepo;

class UserController
{

	function actionShowProfile()
	{
		$self = $this;

		ViewModelRepo::getRepo()->addUserProfile(function() use($self)
		{
			$userId = $self->getRequest()->getParam('userId');
			return $self->getUserService()->findUserProfile($userId);
		);});
	}

	public function getUserService()
	{
		return new UserService();
	}
}
```

now in View Template

```
	echo ViewModelRepo::getRepo()->getUserProfile()->firstname;
	echo '';
	echo ViewModelRepo::getRepo()->getUserProfile()->lastname;
```

Important is that the both UserProfileViewModel and UserProfileViewMapper classes exists in the code scope.

Follow you can see some other usage cases:

```
// output immediately as json
echo json_encode(ViewModelRepo::getRepo()->addConcreteViewModel($callable)->getConcreteMode());

// creation of the view model with an specific identifier
// this is necessary for creation more than one view model instance that contains different data
ViewModelRepo::getRepo()->addConcreteViewModel(array('status_first' => 1, 'status_second' => 2, 'level' => 3), 'id_concrete_1');

ViewModelRepo::getRepo()->addConcreteViewModel(new stdClass, 'id_concrete_2');

// get data in template
echo ViewModelRepo::getRepo()->getConcreteViewModel();
// ...
echo ViewModelRepo::getRepo()->getConcreteViewModel('id_concrete_1')->status_first;
// ...
echo ViewModelRepo::getRepo()->getConcreteViewModel('id_concrete_2');
```

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

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

Unknown

Total

1

Last Release

4384d ago

### Community

Maintainers

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

---

Top Contributors

[![olekhy](https://avatars.githubusercontent.com/u/422148?v=4)](https://github.com/olekhy "olekhy (74 commits)")[![scrutinizer-auto-fixer](https://avatars.githubusercontent.com/u/6253494?v=4)](https://github.com/scrutinizer-auto-fixer "scrutinizer-auto-fixer (1 commits)")

---

Tags

modelview

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/olekhy-view-model-service/health.svg)

```
[![Health](https://phpackages.com/badges/olekhy-view-model-service/health.svg)](https://phpackages.com/packages/olekhy-view-model-service)
```

###  Alternatives

[backpack/generators

Generate files for laravel projects

3122.6M18](/packages/backpack-generators)[laminas/laminas-view

Fast and type safe HTML templating library with a flexible plugin system supporting multistep template composition

7526.3M230](/packages/laminas-laminas-view)[shoot/shoot

Shoot aims to make providing data to your templates more manageable

40229.9k2](/packages/shoot-shoot)[deefour/presenter

Presenters/Decorators for PHP Objects

122.5k](/packages/deefour-presenter)

PHPackages © 2026

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