PHPackages                             triun/value-object - 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. triun/value-object

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

triun/value-object
==================

Value Object

v1.0.0(9y ago)0477MITPHPPHP &gt;=5.1.0

Since Mar 9Pushed 9y ago1 watchersCompare

[ Source](https://github.com/Triun/value-object)[ Packagist](https://packagist.org/packages/triun/value-object)[ Docs](https://github.com/Triun)[ RSS](/packages/triun-value-object/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (1)Dependencies (2)Versions (2)Used By (0)

Value Object
============

[](#value-object)

[![Latest Version on Packagist](https://camo.githubusercontent.com/b40fde81e1a05f334f9e26a393e1ce005ccc23ead0cdc7257d314b5e1531b522/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f747269756e2f76616c75652d6f626a6563742e737667)](https://packagist.org/packages/triun/value-object)[![Pre Release Version on Packagist](https://camo.githubusercontent.com/e6369c426dec36d306e32ea15e0cb367cd43682e9ed0d90f474113168bf3bc70/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f767072652f747269756e2f76616c75652d6f626a6563742e737667)](https://packagist.org/packages/triun/value-object)[![Latest Unstable Version](https://camo.githubusercontent.com/ec0593e532103ec5159f59ef89624ff6adb031426a8e1d73d083a155eb4a462e/68747470733a2f2f706f7365722e707567782e6f72672f747269756e2f76616c75652d6f626a6563742f762f756e737461626c65)](https://packagist.org/packages/triun/value-object)[![Build Status](https://camo.githubusercontent.com/13b015d18298016a343de1603b1d0e5c0379963b8fc97e2f78da51ec42337d6a/68747470733a2f2f7472617669732d63692e6f72672f547269756e2f76616c75652d6f626a6563742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/Triun/value-object)[![Total Downloads](https://camo.githubusercontent.com/52936079703785f4c8f3c2b789cf4121d8c89efa77cd827d804af839220ee9ff/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f747269756e2f76616c75652d6f626a6563742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/triun/value-object)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)

Value Object definition.

About
-----

[](#about)

PHP Class interface to define and standardise a Value Object format.

In computer science, a value object is a small object that represents a simple entity whose equality is not based on identity: i.e. two value objects are equal when they have the same value, not necessarily being the same object (Wikipedia).

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

[](#installation)

Require this package with composer using the following command:

```
composer require triun/value-object
```

Description
-----------

[](#description)

The package contains a ValueObject interface:

```
interface ValueObject
{
    /**
     * Returns a object taking PHP native value(s) as argument(s).
     *
     * @return \Triun\ValueObject\ValueObject
     */
    public static function fromNative();

    /**
     * Returns PHP native value(s)
     *
     * @return mixed
     */
    public function toNative();

    /**
     * Compare two ValueObjectInterface and tells whether they can be considered equal.
     *
     * @param \Triun\ValueObject\ValueObject $object
     *
     * @return mixed
     */
    public function equals(ValueObject $object);

    /**
     * Returns a string representation of the object.
     *
     * @return string
     */
    public function __toString();
}
```

And a Invalid Argument Exception:

```
class InvalidNativeArgumentException extends \InvalidArgumentException
{
    /**
     * InvalidNativeArgumentException constructor.
     *
     * @param string $value
     * @param array  $allowed_types
     */
    public function __construct($value, array $allowed_types)
    {
        $this->message = sprintf(
            'Argument "%s" is invalid. Allowed types for argument are "%s".',
            $value,
            implode(', ', $allowed_types)
        );
    }
}
```

Usage
-----

[](#usage)

Example of use for a single field ValueObject:

```
use Triun\ValueObject\ValueObject;
use Triun\ValueObject\Exceptions\InvalidNativeArgumentException;

class StringLiteral implements ValueObject
{
    /**
     * Native string
     *
     * @var string
     */
    protected $value;

    /**
     * Returns a StringLiteral object given a PHP native string as parameter.
     *
     * @internal param string $value
     *
     * @return StringLiteral
     */
    public static function fromNative()
    {
        $value = func_get_arg(0);

        return new static($value);
    }

    /**
     * Returns a StringLiteral object given a PHP native string as parameter.
     *
     * @param string $value
     */
    public function __construct($value)
    {
        if (false === \is_string($value)) {
            throw new InvalidNativeArgumentException($value, array('string'));
        }

        $this->value = $value;
    }

    /**
     * Returns the value of the string
     *
     * @return string
     */
    public function toNative()
    {
        return $this->value;
    }

    /**
     * Tells whether two string literals are equal by comparing their values
     *
     * @param  ValueObject $stringLiteral
     *
     * @return bool
     */
    public function equals(ValueObject $stringLiteral)
    {
        if (static::class !== get_class($stringLiteral)) {
            return false;
        }

        return $this->toNative() === $stringLiteral->toNative();
    }

    /**
     * Tells whether the StringLiteral is empty
     *
     * @return bool
     */
    public function isEmpty()
    {
        return \strlen($this->toNative()) == 0;
    }

    /**
     * Returns the string value itself
     *
     * @return string
     */
    public function __toString()
    {
        return $this->toNative();
    }
}
```

Links
-----

[](#links)

- Wikipedia: [Value Object](https://en.wikipedia.org/wiki/Value_object)
- Wikipedia: [Domain-Driven Design](https://en.wikipedia.org/wiki/Domain-driven_design)
- Culttt: [What is the difference between Entities and Value Objects?](http://culttt.com/2014/04/30/difference-entities-value-objects/)

Issues
------

[](#issues)

Bug reports and feature requests can be submitted on the [Github Issue Tracker](https://github.com/Triun/value-object/issues).

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

[](#contributing)

See [CONTRIBUTING.md](CONTRIBUTING.md) for information.

License
-------

[](#license)

The Laravel Model Base is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT)

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

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

Unknown

Total

1

Last Release

3355d ago

### Community

Maintainers

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

---

Tags

objectvalue

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/triun-value-object/health.svg)

```
[![Health](https://phpackages.com/badges/triun-value-object/health.svg)](https://phpackages.com/packages/triun-value-object)
```

###  Alternatives

[myclabs/deep-copy

Create deep copies (clones) of your objects

8.9k849.8M169](/packages/myclabs-deep-copy)[symfony/property-access

Provides functions to read and write from/to an object or array using a simple string notation

2.8k295.3M2.5k](/packages/symfony-property-access)[cuyz/valinor

Dependency free PHP library that helps to map any input into a strongly-typed structure.

1.5k9.2M108](/packages/cuyz-valinor)[yzen.dev/plain-to-class

Class-transformer to transform your dataset into a structured object

16293.9k6](/packages/yzendev-plain-to-class)[jasny/dotkey

Dot notation access for objects and arrays

14219.5k6](/packages/jasny-dotkey)[brysem/phpenums

Enums made simple in PHP.

10171.5k](/packages/brysem-phpenums)

PHPackages © 2026

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