mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 22:14:03 +00:00
Add Tenant object
This commit is contained in:
parent
72baa45126
commit
96e8dec5eb
1 changed files with 91 additions and 0 deletions
91
src/Tenant.php
Normal file
91
src/Tenant.php
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy;
|
||||
|
||||
/**
|
||||
* @final Class is subject to breaking changes in minor and patch versions.
|
||||
*/
|
||||
final class Tenant
|
||||
{
|
||||
// todo specify id in data
|
||||
|
||||
/**
|
||||
* Tenant data.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $data = [];
|
||||
|
||||
/**
|
||||
* List of domains that belong to the tenant.
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
public $domains;
|
||||
|
||||
/**
|
||||
* @var TenantManager
|
||||
*/
|
||||
private $manager;
|
||||
|
||||
/**
|
||||
* Does this tenant exist in the storage.
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
private $persisted = false;
|
||||
|
||||
public function __construct(TenantManager $tenantManager)
|
||||
{
|
||||
$this->manager = $tenantManager;
|
||||
}
|
||||
|
||||
public static function new(): self
|
||||
{
|
||||
return app(static::class);
|
||||
}
|
||||
|
||||
public static function fromStorage(array $data): self
|
||||
{
|
||||
return app(static::class)->withData($data)->persisted();
|
||||
}
|
||||
|
||||
public function persisted()
|
||||
{
|
||||
$this->persisted = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function withDomains($domains): self
|
||||
{
|
||||
$domains = (array) $domains;
|
||||
|
||||
$this->domains = $domains;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function withData($data): self
|
||||
{
|
||||
$this->data = $data;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function save()
|
||||
{
|
||||
if ($this->persisted) {
|
||||
$this->manager->addTenant($this);
|
||||
} else {
|
||||
$this->manager->updateTenant($this);
|
||||
}
|
||||
}
|
||||
|
||||
public function __get($name)
|
||||
{
|
||||
return $this->data[$name];
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue