/** * @param Row|Traversable|array|null $arg * @throws InvalidArgumentException */ public function __construct($arg = null) { if ($arg instanceof Row) { if ($arg->isDetached()) { throw new InvalidArgumentException('It is not allowed to create entity ' . get_called_class() . ' from detached instance of LeanMapper\\Row.'); } $this->row = $arg; $this->mapper = $arg->getMapper(); } else { $this->row = Result::createDetachedInstance()->getRow(); foreach ($this->getCurrentReflection()->getEntityProperties() as $property) { if ($property->hasDefaultValue()) { $propertyName = $property->getName(); $this->set($propertyName, $property->getDefaultValue()); } } $this->initDefaults(); if ($arg !== null) { if (!is_array($arg) and !$arg instanceof Traversable) { $type = gettype($arg) !== 'object' ? gettype($arg) : 'instance of ' . get_class($arg); throw new InvalidArgumentException("Argument \$arg in " . get_called_class() . "::__construct must contain either null, array, instance of LeanMapper\\Row or instance of Traversable, {$type} given."); } $this->assign($arg); } } }