LeanMapper\Entity::__construct PHP Method

__construct() public method

public __construct ( Row | Traversabl\Traversable | array | null $arg = null )
$arg Row | Traversabl\Traversable | array | null
    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);
            }
        }
    }

Usage Example

Example #1
0
 /**
  * @param Row|null $arg
  * @param EdgesLoader $edgesLoader
  */
 public function __construct($arg = null, EdgesLoader $edgesLoader)
 {
     parent::__construct($arg);
     $this->edgesLoader = $edgesLoader;
 }
All Usage Examples Of LeanMapper\Entity::__construct