PHPackages                             asokol1981/abstract-mold - 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. asokol1981/abstract-mold

ActiveLibrary

asokol1981/abstract-mold
========================

A base abstract class for centralizing all changes to an entity using user-provided data (mold pattern).

2.0.3(10mo ago)018MITPHPPHP &gt;=8.1CI passing

Since Jul 8Pushed 10mo agoCompare

[ Source](https://github.com/asokol1981/abstract-mold)[ Packagist](https://packagist.org/packages/asokol1981/abstract-mold)[ RSS](/packages/asokol1981-abstract-mold/feed)WikiDiscussions main Synced 1mo ago

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

AbstractMold
============

[](#abstractmold)

[![tests](https://github.com/asokol1981/abstract-mold/workflows/tests/badge.svg)](https://github.com/asokol1981/abstract-mold/actions) [![codecov](https://camo.githubusercontent.com/acdb33cc67a94a8322f978b75531314605ecd596433e01211bb8fd89a1ce3a07/68747470733a2f2f636f6465636f762e696f2f67682f61736f6b6f6c313938312f61627374726163742d6d6f6c642f6272616e63682f6d61696e2f67726170682f62616467652e737667)](https://codecov.io/gh/asokol1981/abstract-mold) [![downloads](https://camo.githubusercontent.com/85b48216de7135b7fdb04e270250c337e1c923621d16db0c08bdadb7330c684c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f61736f6b6f6c313938312f61627374726163742d6d6f6c642e737667)](https://packagist.org/packages/asokol1981/abstract-mold)

💎 **AbstractMold** is a set of abstract classes and shared utilities for centralized management of entity data changes, with strict control and single-point validation.

---

✨ Main idea
-----------

[](#-main-idea)

A **Mold** acts as a single entry point for handling any incoming user data (creation, updates, or partial patches). All changes go through the mold to ensure:

- Whitelisted allowed fields only.
- Centralized validation and normalization logic.
- Predictable and consistent data preparation.

---

🧩 Two mold types
----------------

[](#-two-mold-types)

### 🟢 AbstractMutableMold

[](#-abstractmutablemold)

Allows step-by-step incremental data changes and gradual accumulation of modifications.

- Perfect for form scenarios or multi-step wizards.
- You can modify fields one by one or in batches.
- Tracks which fields were explicitly changed.

---

### 🔵 AbstractImmutableMold

[](#-abstractimmutablemold)

Accepts **base data** and a **patch** all at once in the constructor.

- Ideal for APIs or services where you receive all data as a single payload.
- Does not allow further modification after creation (immutable approach).

---

🚀 Installation
--------------

[](#-installation)

```
composer require asokol1981/abstract-mold
```

---

🚀 Usage examples
----------------

[](#-usage-examples)

### 🟢 AbstractMutableMold usage

[](#-abstractmutablemold-usage)

```
use ASokol1981\AbstractMold\AbstractMutableMold;

final class UserMutableMold extends AbstractMutableMold
{
    protected function publicFields(): array
    {
        return ['name', 'email', 'age'];
    }

    protected function validatedData(): array
    {
        $age = $this->getRawData('age');

        return [
            'name' => (string) $this->getRawData('name', ''),
            'email' => strtolower((string) $this->getRawData('email', '')),
            'age' => $age !== null ? (int) $age : null,
        ];
    }
}

// Usage

$mold = new UserMutableMold(['name' => 'John', 'email' => 'John@EXAMPLE.COM']);
$mold->change('name', 'Johnny');
$mold->changes(['email' => 'Johnny@EXAMPLE.COM', 'age' => '25']);

$data = $mold->validated();
// ['name' => 'Johnny', 'email' => 'johnny@example.com', 'age' => 25]

$changes = $mold->changesValidated();
// ['name' => 'Johnny', 'email' => 'johnny@example.com', 'age' => 25]
```

---

### 🔵 AbstractImmutableMold usage

[](#-abstractimmutablemold-usage)

```
use ASokol1981\AbstractMold\AbstractImmutableMold;

final class UserImmutableMold extends AbstractImmutableMold
{
    protected function publicFields(): array
    {
        return ['name', 'email', 'age'];
    }

    protected function validatedData(): array
    {
        $age = $this->getRawData('age');

        return [
            'name' => (string) $this->getRawData('name', ''),
            'email' => strtolower((string) $this->getRawData('email', '')),
            'age' => $age !== null ? (int) $age : null,
        ];
    }
}

// Usage

$mold = new UserImmutableMold(
    ['name' => 'John', 'email' => 'John@EXAMPLE.COM'],
    ['name' => 'Johnny', 'age' => '25']
);

$data = $mold->validated();
// ['name' => 'Johnny', 'email' => 'john@example.com', 'age' => 25]

$changes = $mold->changesValidated();
// ['name' => 'Johnny', 'age' => 25]
```

---

✅ Key advantages
----------------

[](#-key-advantages)

- 🚦 Strict allowed fields control.
- 💾 Centralized validation logic.
- 🧩 Flexible design for different scenarios (mutable vs immutable).
- ⚡️ Built-in validated data cache for performance.
- 🛡 Safe and predictable API.

---

🤝 License
---------

[](#-license)

MIT © Aleksei Sokolov

---

Created in collaboration with ChatGPT (OpenAI), July 2025.

###  Health Score

32

—

LowBetter than 71% of packages

Maintenance58

Moderate activity, may be stable

Popularity6

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~0 days

Total

5

Last Release

303d ago

Major Versions

v1.0.0 → v2.0.02025-07-10

### Community

Maintainers

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

---

Top Contributors

[![asokol1981](https://avatars.githubusercontent.com/u/11194728?v=4)](https://github.com/asokol1981 "asokol1981 (8 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/asokol1981-abstract-mold/health.svg)

```
[![Health](https://phpackages.com/badges/asokol1981-abstract-mold/health.svg)](https://phpackages.com/packages/asokol1981-abstract-mold)
```

PHPackages © 2026

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