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

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

apie/value-objects
==================

Apie value objects

095PHP

Since Feb 10Pushed 5y ago1 watchersCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

value-objects
=============

[](#value-objects)

[![Scrutinizer Code Quality](https://camo.githubusercontent.com/486c9338b5258518b41b93ca8072dfc8a53c6eb12c15874d61484b5d53d8c024/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f617069652d6c69622f76616c75652d6f626a656374732f6261646765732f7175616c6974792d73636f72652e706e673f623d6d61696e)](https://scrutinizer-ci.com/g/apie-lib/value-objects/?branch=main)[![Build Status](https://camo.githubusercontent.com/bb347df1892bd98fb662f06e802f7dae7f78c628059390addab63171992e27bd/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f617069652d6c69622f76616c75652d6f626a656374732f6261646765732f6275696c642e706e673f623d6d61696e)](https://scrutinizer-ci.com/g/apie-lib/value-objects/build-status/main)

This package is part of the [Apie](https://github.com/apie-lib) library. The code is maintained in a monorepo, so PR's need to be sent to the [monorepo](https://github.com/apie-lib/apie-lib-monorepo/pulls)

Documentation
-------------

[](#documentation)

This package is used in the entire Apie library to make value objects, but they can also be used outside Apie. Apie value objects implement ValueObjectInterface. ValueObjectInterface has 2 methods to implement. A static fromNative method to create a value object from a primitive value and a toNative method to convert a value object back to a primitive value.

Next to this interface we have two traits to make simple value objects for strings and enums.

Available classes/interfaces.
-----------------------------

[](#available-classesinterfaces)

### ValueObjectInterface

[](#valueobjectinterface)

Interface to implement a value object for Apie. fromNative converts primitive to a value object and toNative converts a value object to a primitive.

### ValueObjectCompareInterface

[](#valueobjectcompareinterface)

Interface to add a method to compare for equality where $object-&gt;toNative() === $object2-&gt;toNative() is false, but the objects value objects are the same.

### StringTrait

[](#stringtrait)

Used to make value objects that are represented as a string. It implements all methods on ValueObjectInterface, but you require to make 2 methods to make it work: one for validating the input and one for sanitizing the input.

```
class StringTraitExample implements ValueObjectInterface
{
    use StringTrait;

    protected function validValue(string $value): bool
    {
        return !empty($value);
    }

    protected function sanitizeValue(string $value): string
    {
        return trim($value);
    }
}
```

And can be used like this:

```
$instance = new StringTraitExample(' example ');
$instance->toNative(); // returns 'example'
$instance2 = StringTraitExample::fromNative(' example '); // just calls the constructor.

StringTraitExample::fromNative(''); // throws exception
```

### StringEnumTrait

[](#stringenumtrait)

Used to make Enum value objects.

Class example:

```
