PHPackages                             ap-lib/caster - 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. ap-lib/caster

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

ap-lib/caster
=============

AP\\Caster is a high-performance PHP library designed to facilitate flexible and efficient data casting operations. It supports adaptive scalar casting, enum casting, and customizable casting strategies to ensure data integrity and type safety in your applications

039PHP

Since Jul 8Pushed 10mo ago1 watchersCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

AP\\Caster
==========

[](#apcaster)

[![MIT License](https://camo.githubusercontent.com/7013272bd27ece47364536a221edb554cd69683b68a46fc0ee96881174c4214c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)](LICENSE)

AP\\Caster is a high-performance PHP library designed to facilitate flexible and efficient data casting operations. It supports adaptive scalar casting, enum casting, and customizable casting strategies to ensure data integrity and type safety in your applications

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

[](#installation)

```
composer require ap-lib/caster
```

Features
--------

[](#features)

- **Adaptive Scalar Casting**: Automatically converts between scalar types (e.g., string to int) when appropriate.
- **Enum Casting**: Seamlessly casts values to PHP enums, ensuring valid enum instances.
- **Customizable Casting Strategies**: Extend or modify casting behavior by implementing custom casters.
- **Error Handling**: Provides detailed error information when casting operations fail.

Requirements
------------

[](#requirements)

- PHP 8.3 or higher

Core Components
---------------

[](#core-components)

AP\\Caster is built around a single interface: [`CasterInterface`](src/CasterInterface.php), which defines the contract for all casters.

By default, all casting-related errors extend [`AP\ErrorNode\Error`](https://github.com/ap-lib/error-node/blob/main/src/Error.php), ensuring consistent error handling across different casters.

Getting started
---------------

[](#getting-started)

Here's a quick example demonstrating how to use AP\\Caster

### Initialize the PrimaryCaster with desired casters

[](#initialize-the-primarycaster-with-desired-casters)

```
use AP\Caster\PrimaryCaster;
use AP\Caster\AdaptiveScalarCaster;
use AP\Caster\EnumCaster;

//
$caster = new PrimaryCaster([
    new AdaptiveScalarCaster(),
    new EnumCaster(),
]);
```

### Example 1: matching data type

[](#example-1-matching-data-type)

```
$data = "hello world";
$result = $caster->cast("string", $data);

// $result = true
// $data = "hello world"
```

### Example 2: adaptive scalar casting

[](#example-2-adaptive-scalar-casting)

```
//
$data = "1200";
$result = $caster->cast("int", $data);

// $result = true
// $data = 1200
```

### Example 2: enum casting

[](#example-2-enum-casting)

```
enum OneTwoThree: string
{
    case One   = 1;
    case Two   = 2;
    case Three = 3;
}

// Example 3:
$data = 3;
$result = $caster->cast(OneTwoThreeExampleEnumInt::class, $data);

// $result = true
// $data = OneTwoThreeExampleEnumInt::Three
```

### Example 4: handling casting errors

[](#example-4-handling-casting-errors)

```
$data = ["hello" => "world"];
$result = $caster->cast("int", $data);

// IMPORTANT, a caster will never modify the original data if casting was unsuccessful
// $data = ["hello" => "world"]
/*
    result = [
      \AP\Caster\Error\UnexpectedType::__set_state([
         'message' => 'Unexpected date type, expected `int`, actual `array`',
         'path' => [],
         'expected' => 'int',
         'actual' => 'array',
      ]),
    ]
*/

if (is_array($result)) {
    // Handle errors
    foreach ($result as $error) {
        echo $error->message . PHP_EOL;
    }
}

// Output: Unexpected data type, expected `int`, actual `array`
```

###  Health Score

17

—

LowBetter than 6% of packages

Maintenance39

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity14

Early-stage or recently created project

 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/1215fe5ecc9ba0ab1c730d3c992125cf6ebf460562e66be71ebae127789d465a?d=identicon)[AntonPanfilov](/maintainers/AntonPanfilov)

---

Top Contributors

[![anton-panfilov](https://avatars.githubusercontent.com/u/1083546?v=4)](https://github.com/anton-panfilov "anton-panfilov (9 commits)")

### Embed Badge

![Health badge](/badges/ap-lib-caster/health.svg)

```
[![Health](https://phpackages.com/badges/ap-lib-caster/health.svg)](https://phpackages.com/packages/ap-lib-caster)
```

###  Alternatives

[magepow/categories

Shows categories in the form of a grid or list, on category pages, home page or any other page.

4018.8k](/packages/magepow-categories)[mmikkel/child-me

Easily create child elements

2048.6k1](/packages/mmikkel-child-me)

PHPackages © 2026

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