PHPackages                             makeabledk/php-value-objects - 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. makeabledk/php-value-objects

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

makeabledk/php-value-objects
============================

v0.16.1(3w ago)11.7k1CC-BY-SA-4.0PHPPHP ^8.1CI passing

Since Jun 27Pushed 3w ago1 watchersCompare

[ Source](https://github.com/makeabledk/php-value-objects)[ Packagist](https://packagist.org/packages/makeabledk/php-value-objects)[ RSS](/packages/makeabledk-php-value-objects/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (10)Dependencies (6)Versions (16)Used By (0)

Laravel Value Objects
=====================

[](#laravel-value-objects)

[![Latest Version on Packagist](https://camo.githubusercontent.com/6556589a043d80c500064495d090b7d2b14b56651b1faa62c1e9eef511c426c6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d616b6561626c65646b2f7068702d76616c75652d6f626a656374732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/makeabledk/php-value-objects)[![Build Status](https://camo.githubusercontent.com/b9b8009996ecd5cb40648f3cb4c86a600ad9d7f3bec9c6c06c262087bc388fec/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f6d616b6561626c65646b2f7068702d76616c75652d6f626a656374732f52756e25323074657374733f6c6162656c3d5465737473)](https://github.com/makeabledk/php-value-objects/actions)[![StyleCI](https://camo.githubusercontent.com/70cafc2e7006f38f606b9d7d09afad8622522953525a778dcd2402954918877f/68747470733a2f2f7374796c6563692e696f2f7265706f732f39353535323736312f736869656c643f6272616e63683d6d6173746572)](https://styleci.io/repos/95552761)

This package provides a set of handy value objects:

- Duration
- Period
- Whitelist

Makeable is web- and mobile app agency located in Aarhus, Denmark.

Install
-------

[](#install)

You can install this package via composer:

```
composer require makeabledk/php-value-objects
```

Usage
-----

[](#usage)

### Duration

[](#duration)

Duration provides an easy way to interact with and manipulate durations of time.

#### Example usages

[](#example-usages)

Create a duration and display formatted

```
Duration::create(1,30)->toFormat(); // 01:30:00
```

You can also specify a custom format. Valid placeholders are: h,hh,m,mm,s,ss

```
Duration::$format = 'h:mm';
Duration::create(1,30)->toFormat(); // 1:30
```

Perform simple add/subtract calculations

```
Duration::create(1,30)->add(Duration::create(1,30))->toFormat(); // 03:00:00
Duration::create(1,30)->subtract(Duration::create(0,20))->toFormat(); // 01:10:00
```

If you are using Laravel and have a Events@getDurationAttribute() accessor that converts to Duration::class, you can even do this:

```
$events = Events::all();
$eventsTotalDuration = Duration::sum($events, 'duration');
```

Easily export as an array, and re-instantiate if needed. Great for serving client API\*.

```
$exported = Duration::create(1,30)->toArray(); // ['seconds' => 5400, 'minutes' => 90, 'hours' => 1.5, 'formatted' => '01:30:00']
$imported = Duration::fromArray($exported);
```

\*Note it implements illuminate/support Arrayable contract, so it automatically casts to an array for eloquent models.

Period
------

[](#period)

The Period object is great when you need to query data within a given period.

#### Example usages

[](#example-usages-1)

Creating a period. Note that both start and end is optional.

```
$today = new Period(Carbon::today(), Carbon::tomorrow());
$future = new Period(Carbon::now());
$past = new Period(null, Carbon::now());
```

Manipulate on the fly

```
$thisWeek = new Period(
    Carbon::today()->previous(Carbon::MONDAY)
    Carbon::today()->next(Carbon::SUNDAY)
);
$thisWeek->earliest(Carbon::today())->getStart(); // carbon of today
$thisWeek->latest(Carbon::tomorrow())->getEnd(); // carbon of tomorrow
```

Easily export as an array, and re-instantiate if needed. Great for serving client API\*.

```
$exported = (new Period(Carbon::today(), Carbon::tomorrow()))->toArray(); // ['start' => '2017-06-27 00:00:00', 'end' => '2017-06-28 00:00:00']
$imported = Duration::fromArray($exported);
```

\*Note it implements illuminate/support Arrayable contract, so it automatically casts to an array for eloquent models.

Whitelist
---------

[](#whitelist)

Whitelist is an abstract class that you can extend to specify a certain sets of whitelisted values.

It is great to quickly whip up a Status Object that ensures you are always working with a valid status.

### Example usages

[](#example-usages-2)

Creating an OrderStatus class

```
class OrderStatus extends Whitelist
{
    const VALUES = ['pending', 'accepted', 'cancelled'];
}
```

Now you would only be able to instantiate OrderStatus with a valid status:

```
$accepted = new OrderStatus('accepted');
$invalid = new OrderStatus('foobar'); // throws exception
```

You can customize the exception thrown. For instance you could swap it for the default Symfony/Laravel '422 UnprocessableEntityExceptions'.

```
OrderStatus::$exceptionClass = \Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException::class;
```

Now you have error handling out of the box for forms and wildcard controller methods (ie. '/orders/{status}') !

Change log
----------

[](#change-log)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Testing
-------

[](#testing)

You can run the tests with:

```
composer test
```

Contributing
------------

[](#contributing)

We are happy to receive pull requests for additional functionality. Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Credits
-------

[](#credits)

- [Rasmus Christoffer Nielsen](https://github.com/rasmuscnielsen)
- [All Contributors](../../contributors)

License
-------

[](#license)

Attribution-ShareAlike 4.0 International. Please see [License File](LICENSE.md) for more information.

###  Health Score

53

—

FairBetter than 97% of packages

Maintenance95

Actively maintained with recent releases

Popularity22

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity70

Established project with proven stability

 Bus Factor1

Top contributor holds 96.8% 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 ~268 days

Recently: every ~543 days

Total

13

Last Release

24d ago

PHP version history (4 changes)v0.9.0PHP &gt;=7.0.0

v0.11.0PHP &gt;=7.1.3

0.13.0PHP &gt;=7.2

v0.15.0PHP ^8.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/22741894?v=4)[Makeable ApS](/maintainers/makeabledk)[@makeabledk](https://github.com/makeabledk)

---

Top Contributors

[![rasmuscnielsen](https://avatars.githubusercontent.com/u/8465957?v=4)](https://github.com/rasmuscnielsen "rasmuscnielsen (30 commits)")[![ottothebot](https://avatars.githubusercontent.com/u/261808813?v=4)](https://github.com/ottothebot "ottothebot (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/makeabledk-php-value-objects/health.svg)

```
[![Health](https://phpackages.com/badges/makeabledk-php-value-objects/health.svg)](https://phpackages.com/packages/makeabledk-php-value-objects)
```

###  Alternatives

[flarum/core

Delightfully simple forum software.

211.3M1.9k](/packages/flarum-core)[erlandmuchasaj/laravel-gzip

Gzip your responses.

40129.3k2](/packages/erlandmuchasaj-laravel-gzip)[interaction-design-foundation/laravel-geoip

Support for multiple Geographical Location services.

17221.0k3](/packages/interaction-design-foundation-laravel-geoip)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

245.2k](/packages/aedart-athenaeum)

PHPackages © 2026

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