CodefyPHP FrameworkCodefyPHP Framework
  • Home
  • Community
    • Forum
    • Github
    • YouTube
  • News
  • Home
  • Community
    • Forum
    • Github
    • YouTube
  • News
home/Knowledge Base/Aggregates/Value Objects
Popular Search:installation, codex, mail

Value Objects

45 views 0

Written by Joshua
August 14, 2024

In the previous section, we created our first value object, PostId. Before continuing on, we have a couple of other value objects to create: Title and Content. To keep it simple, we will have them extend a base value object, Qubus\ValueObjects\StringLiteral\StringLiteral and add a new fromString method.

Title

<?php

use Qubus\Exception\Data\TypeException;
use Qubus\ValueObjects\StringLiteral\StringLiteral;

final class Title extends StringLiteral
{
    /**
     * @throws TypeException
     */
    public static function fromString(string $title): self
    {
        return new self(value: $title);
    }
}

Content

<?php

use Qubus\Exception\Data\TypeException;
use Qubus\ValueObjects\StringLiteral\StringLiteral;

final class Content extends StringLiteral
{
    /**
     * @throws TypeException
     */
    public static function fromString(string $content): self
    {
        return new self(value: $content);
    }
}

StringLiteral has other methods to be aware of:

<?php

/**
 * Returns a String object given a PHP native string as parameter.
 *
 * @param  string $value
 * @return StringLiteral|ValueObject
 */
public static function fromNative(): ValueObject;

/**
 * Returns the value of the string.
 */
public function toNative(): string;

/**
 * Tells whether two strings are equal by comparing their values
 *
 * @param  ValueObject $string
 */
public function equals(ValueObject $stringLiteral): bool;

/**
 * Tells whether the String is empty
 */
public function isEmpty(): bool;

Both Title and Content inherit the above methods.

Forum

If you have any questions or issues, please feel free to post to the Documentation Forum.

SLA Support

If you are needing more hands on support, needing consultation, or help with setup, support me on Github at $60 or more. Once you've sponsored me, you will receive an email on the best way to contact me to start your support.

Edit on Github

Last Updated on August 14, 2024 by Joshua

Related Articles
  • Aggregate Repository
  • Event Store
  • Event Sourcing
  • Protecting Invariants
  • Records Events
  • Identifies Aggregate

Didn't find your answer? Check out the Forum

  Identifies Aggregate

Domain Event  

  • Copyright 2025 CodefyPHP.com. All Rights Reserved

Popular Search:installation, codex, mail