squirrelphp/entities

View on GitHub
src/PopulatePropertiesWithIterableTrait.php

Summary

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

namespace Squirrel\Entities;

use Squirrel\Debug\Debug;

/**
 * @psalm-immutable
 */
trait PopulatePropertiesWithIterableTrait
{
    /**
     * Initialize the object with an array - not used by repository, as the repository uses reflection to
     * set entity values, but a constructor can be helpful for testing or other special/explicit usages
     */
    public function __construct(iterable $data = [])
    {
        foreach ($data as $key => $value) {
            if (!\property_exists($this, $key)) {
                throw new \InvalidArgumentException(
                    'Property "' . $key . '" does not exist in entity class when attempting to construct with: ' .
                    Debug::sanitizeData($data),
                );
            }

            $this->$key = $value;
        }
    }
}