widmogrod/php-functional

View on GitHub
src/Primitive/Sum.php

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
<?php

declare(strict_types=1);

namespace Widmogrod\Primitive;

use FunctionalPHP\FantasyLand;

class Sum extends Num implements
    FantasyLand\Monoid,
    FantasyLand\Pointed
{
    /**
     * @inheritdoc
     */
    public static function mempty()
    {
        return self::of(0);
    }

    /**
     * @inheritdoc
     */
    public function concat(FantasyLand\Semigroup $value): FantasyLand\Semigroup
    {
        if ($value instanceof self) {
            return self::of($this->extract() + $value->extract());
        }

        throw new TypeMismatchError($value, self::class);
    }
}