<?php

namespace App\Models\Objects;

class Contact
{
	private ?int $id;
    private string $firstName;
    private string $lastName;
    private string $phone;
    private string $email;
    private ?string $imgSmall;
    private ?string $imgLarge;
    private string $imgPath;
    private ?string $webPage;
    private ?string $role;

    public function setImgPath(string $path): void
    {
        $this->imgPath = $path;
    }

    public function __construct(array $data = [])
    {
        if($data) {
            $this->setAttributes($data);
        }
    }

	public function getFullName(): string
    {
		return sprintf("%s %s", $this->firstName, $this->lastName);
	}

    public function toArray(): array
    {
        return [
            'id' => $this->id,
            'login' => $this->login,
            'firstName' => $this->firstName,
            'lastName' => $this->lastName,
            'phone' => $this->phone,
            'email' => $this->email,
            'imgSmall' => $this->imgSmall,
            'imgLarge' => $this->imgLarge,
            'role' =>  $this->role,
        ];
    }

	public function setAttributes(array $attributes = []):void
    {
        $this->id = $attributes['id'] ?? null;
        $this->firstName = $attributes['first_name'];
        $this->lastName = $attributes['last_name'];
        $this->phone = $attributes['phone'];
        $this->email = $attributes['email'];
        $this->webPage = $attributes['web_page'] ?? null;
        $this->role = $attributes['role'] ?? null;
        $this->imgSmall = $this->setImagePath($attributes['img_small']) ?? null;
        $this->imgLarge = $this->setImagePath($attributes['img_large']) ?? null;
	}

    private function setImagePath(string $path = null): string
    {
        return $this->imgPath ?
            $this->cleanURL($this->imgPath."/".$path) : $path;
    }

    private function cleanURL(string $url): string
    {
        return preg_replace('#(?<!:)/+#', '/', $url);
    }

    public function setImgSmall(string $imgSmall): void
    {
        $this->imgSmall = $imgSmall;
    }

    public  function setImgLarge(string $imgLarge): void
    {
        $this->imgLarge = $imgLarge;
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getFirstName(): string
    {
        return $this->firstName;
    }

    public function getLastName(): string
    {
        return $this->lastName;
    }

    public function getPhone(): string
    {
        return $this->phone;
    }

    public function getEmail(): string
    {
        return $this->email;
    }

    public function getImgSmall(): ?string
    {
        return $this->imgSmall;
    }

    public function getImgLarge(): ?string
    {
        return $this->imgLarge;
    }

    public function getImgPath(): string
    {
        return $this->imgPath;
    }

    public function getWebpage(): ?string
    {
        return $this->webPage;
    }

    public function getRole(): ?string
    {
        return $this->role;
    }
}
?>
