<?php 

namespace NPS\Entity;

use NPS\Exception\NonExistingPropertyException;

/**
 * Adds ability for an entity to be constructed out of an array of properties
 * @author Lukas Korinek
 */
trait TCreate
{
    /**
     * Constructs an object with a predefined set of properties
     * Properties arrays contains keys of camel cased property names and their respective values
     * @param array $properties
     * @return self
     */
    public static function create(array $properties)
    {
        $self = new self();

        foreach($properties as $prop => $val)
        {
            if(!property_exists($self, $prop))
            {
                throw new NonExistingPropertyException(get_called_class(), $prop);
            }

            $self->{$prop} = $val;
        }

        return $self;
    }
}

