PHPackages                             danack/datatype - 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. danack/datatype

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

danack/datatype
===============

A library that makes using data types easier

0.9.1(1mo ago)0117[7 issues](https://github.com/Danack/DataType/issues)MITPHPPHP &gt;=8.1CI passing

Since Jul 23Pushed 1mo ago2 watchersCompare

[ Source](https://github.com/Danack/DataType)[ Packagist](https://packagist.org/packages/danack/datatype)[ RSS](/packages/danack-datatype/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (25)Versions (16)Used By (0)

DataType
========

[](#datatype)

A library for validating input and creating types.

[![Actions Status](https://github.com/Danack/DataType/workflows/Tests/badge.svg)](https://github.com/Danack/DataType/actions)

Installation
============

[](#installation)

`composer require danack/datatype`

Example usage
=============

[](#example-usage)

The full documentation is in [DOCS.md](DOCS.md), but here is an example usage.

In your controller, you would have some code to create the type. e.g. for Symfony you would have something like:

```
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;

class SearchController
{
    public function index(Request $request, SearchRepo $searchRepo): JsonResponse
    {
        $searchParams = SearchParams::createFromRequest($request);

        $data = $searchRepo->search($searchParams);

        return $this->json($data);
    }
}
```

In your code you would have a data type that represents a particular concept, e.g. the search parameter used in the API:

```
class SearchParameters implements DataType
{
    use CreateFromRequest;
    use CreateFromVarMap;
    use GetInputTypesFromAttributes;

    public function __construct(
        #[SearchTerm('search')]
        public string $phrase,

        #[MaxItems('limit')]
        public int $limit,

        #[ArticleSearchOrdering('order')]
        public Ordering $ordering,
    ) {
    }
}
```

And each of the elements in the 'search parameters' would have their own rule based based details:

```
