masfernandez/value-object

0.2.1 2022-10-01 16:20 UTC

This package is auto-updated.

Last update: 2024-04-29 04:48:13 UTC


README

Version Contributors Forks Stargazers Issues MIT License


value-object

Collection of ValueObject ready to use on DDD projects with Symfony validator integrated

PHP

Report Bug · Request Feature


Build Status Coverage Status


Table of Contents
  1. About The Project
  2. Getting Started
  3. Roadmap
  4. Contributing
  5. License
  6. Contact
  7. Acknowledgements

About The Project

I have created this collection of ValueObjects to use in the projects I am working on.

ValueObjects types

  • String
  • Nullable string
  • Uuid
  • Nullable uuid
  • Mixed
  • Int
  • Nullable int
  • Float
  • Nullable float
  • DateTime Y-m-d\TH:i:sP
  • DateTime milliseconds Y-m-d\TH:i:s.uP
  • Coordinate (x float, y float)

Getting Started

Prerequisites

N/A

Installation

composer require masfernandez/value-object

Examples

Value object modeling a book title. It enforces a string primitive type, not blank by default on StringValueObject.

<?php

declare(strict_types=1);

namespace My\Awesome\Ddd\Project;

use Masfernandez\ValueObject\StringValueObject;

final class BookTitle extends StringValueObject
{
}
$bookTitle = new BookTitle('Implementing Domain-Driven Design');
echo $bookTitle->value();
echo $bookTitle;
# output

$ Implementing Domain-Driven Design
$ Implementing Domain-Driven Design

Wrong book title: can not be blank

$bookTitle = new BookTitle('');

'This value is too short. It should have 1 character or more.'

Value object modeling a user email. It enforces a string primitive type, not blank by default on StringValueObject, and add some additional constraints: email format and a max length of 255

<?php

declare(strict_types=1);

namespace My\Awesome\Ddd\Project;

use Masfernandez\ValueObject\StringValueObject;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints;

final class UserEmail extends StringValueObject
{
    /**
     * @return Constraint[]
     */
    protected static function setConstraints(): array
    {
        return array_merge(
            parent::setConstraints(),
            [
                new Constraints\Email(),
                new Constraints\Length(['max' => 255]),
            ]
        );
    }
}
$userEmail = new UserEmail('mangel.sanfer@gmail.com');
echo $userEmail->value();
echo $userEmail;
# output

$ mangel.sanfer@gmail.com
$ mangel.sanfer@gmail.com

Roadmap

See the open issues for a list of proposed features (and known issues).

Contributing

Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

Distributed under the MIT License. See LICENSE.txt for more information.

Contact

Miguel Ángel Sánchez Fernández - mangel.sanfer@gmail.com

(linkedin hiden profile - require login)

LinkedIn

Project Link: https://github.com/masfernandez/value-object

Acknowledgements

Stats

68747470733a2f2f76696577732e77686174696c656172656e65642e746f6461792f76696577732f6769746875622f6d61736665726e616e64657a2f76696577732e737667