<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Class AbstractEntity
* @package App\Entity
*/
abstract class AbstractEntity implements \ArrayAccess
{
/**
* @ORM\Column(type="datetime", nullable=true)
*/
protected $created;
/**
* @ORM\ManyToOne(targetEntity=User::class)
*/
protected $createdUser;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
protected $changed;
/**
* @ORM\ManyToOne(targetEntity=User::class)
*/
protected $changedUser;
/**
* @ORM\Column(type="string", length=50, nullable=true)
*/
protected $ip;
public function getCreated(): ?\DateTimeInterface
{
return $this->created;
}
public function setCreated(\DateTimeInterface $created): self
{
$this->created = $created;
return $this;
}
public function getCreatedUser(): ?User
{
return $this->createdUser;
}
public function setCreatedUser(?User $createdUser): self
{
$this->createdUser = $createdUser;
return $this;
}
public function getChanged(): ?\DateTimeInterface
{
return $this->changed;
}
public function setChanged(\DateTimeInterface $changed): self
{
$this->changed = $changed;
return $this;
}
public function getChangedUser(): ?User
{
return $this->changedUser;
}
public function setChangedUser(?User $changedUser): self
{
$this->changedUser = $changedUser;
return $this;
}
public function getIp(): ?string
{
return $this->ip;
}
public function setIp(string $ip): self
{
$this->ip = $ip;
return $this;
}
/**
* Load client ip address
* @return mixed|string
*/
protected function getClientIp(){
if (!empty($_SERVER['HTTP_CLIENT_IP']))
{
$ipAddress = $_SERVER['HTTP_CLIENT_IP'];
}
//whether ip is from proxy
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$ipAddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
//whether ip is from remote address
else
{
$ipAddress = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'datafixtures';
}
if(is_null($ipAddress)){
return 'Nenacteno';
}
return $ipAddress;
}
/**
* @ORM\PrePersist
* @ORM\PreUpdate
*/
public function onPrePersist()
{
if(is_null($this->created)){
$this->created = new \DateTime("now");
}
$this->changed = new \DateTime("now");
$this->setIp($this->getClientIp());
}
public function offsetExists($offset)
{
$method = 'get'.ucfirst($offset);
return method_exists($this, $method);
}
/**
* @param mixed $offset
* @return false|mixed
*/
public function offsetGet($offset)
{
$method = 'get'.ucfirst($offset);
if(method_exists($this, $method)){
return $this->$method();
}
return false;
}
/**
* @param mixed $offset
* @param mixed $value
* @return false|void
*/
public function offsetSet($offset, $value)
{
$method = 'set'.ucfirst($offset);
if(method_exists($this, $method)){
return $this->$method($value);
}
return false;
}
public function offsetUnset($offset)
{
throw new \Exception('Method offsetUnset does not exists');
}
}