Helpful Code

<?php

// create_function は使わない。無形関数使う
$logger = function($product){
	echo $product
};
<?php

namespace App\Packages\MfsAdmin\RefinanceContract\Pre\Domains\ValueObjects;

use ArrayAccess;
use ArrayIterator;
use Countable;
use InvalidArgumentException;
use IteratorAggregate;

class RecommendRankCodes implements ArrayAccess, IteratorAggregate, Countable
{
    /**
     * @var RecommendRankCode[]
     */
    private array $codes;

    /**
     * @param RecommendRankCode[]
     */
    public function __construct(
        array $codes
    ) {
        foreach ($codes as $code) {
            if (!$code instanceof RecommendRankCode) {
                throw new InvalidArgumentException("parameter is invalid..." );
            }
        }

        $this->codes = array_values($codes);
    }

    /**
     * @param mixed $offset
     * @return bool
     */
    public function offsetExists($offset): bool
    {
        return isset($this->codes[$offset]);
    }

    /**
     * @param mixed $offset
     * @return RecommendRankCode|null
     */
    public function offsetGet($offset): RecommendRankCode|null
    {
        return $this->offsetExists($offset) ? $this->codes[$offset] : null;
    }

    /**
     * @param mixed $offset
     * @param mixed $value
     */
    public function offsetSet($offset, $value): void
    {
        if (is_null($offset)) {
            $this->codes[] = $value;
        } else {
            $this->codes[$offset] = $value;
        }
    }

    /**
     * @param mixed $offset
     */
    public function offsetUnset($offset): void
    {
        unset($this->codes[$offset]);
    }

    public function getIterator(): ArrayIterator
    {
        return new ArrayIterator($this->codes);
    }

    /**
     * @return int
     */
    public function count(): int
    {
        return count($this->codes);
    }

    /**
     * @param RecommendRankCode $code
     * @return RecommendRankCodes
     */
    public function add(RecommendRankCode $code): RecommendRankCodes
    {
        return new self(array_merge($this->codes, $code));
    }
}