1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 17:44:04 +00:00

Tenant::with

This commit is contained in:
Samuel Štancl 2019-09-20 19:44:05 +02:00
parent 61cc0d9364
commit 93fc961b34
2 changed files with 28 additions and 2 deletions

View file

@ -6,6 +6,7 @@ namespace Stancl\Tenancy;
use ArrayAccess; use ArrayAccess;
use Illuminate\Foundation\Application; use Illuminate\Foundation\Application;
use Illuminate\Support\Str;
use Stancl\Tenancy\Contracts\StorageDriver; use Stancl\Tenancy\Contracts\StorageDriver;
use Stancl\Tenancy\Contracts\UniqueIdentifierGenerator; use Stancl\Tenancy\Contracts\UniqueIdentifierGenerator;
use Stancl\Tenancy\Exceptions\TenantStorageException; use Stancl\Tenancy\Exceptions\TenantStorageException;
@ -267,6 +268,13 @@ class Tenant implements ArrayAccess
return $this->put($key, $value); return $this->put($key, $value);
} }
public function with(string $key, $value): self
{
$this->data[$key] = $value;
return $this;
}
public function __get($key) public function __get($key)
{ {
return $this->get($key); return $this->get($key);
@ -280,8 +288,12 @@ class Tenant implements ArrayAccess
$this->data[$key] = $value; $this->data[$key] = $value;
} }
public function __call($name, $arguments) public function __call($method, $parameters)
{ {
// todo withId() if (Str::startsWith($method, 'with')) {
return $this->with(Str::snake(substr($method, 4)), $parameters[0]);
}
// todo throw some exception?
} }
} }

View file

@ -72,4 +72,18 @@ class TenantClassTest extends TestCase
$this->assertEqualsCanonicalizing(['completely.localhost', 'different.localhost', 'domains.localhost'], $tenant->domains); $this->assertEqualsCanonicalizing(['completely.localhost', 'different.localhost', 'domains.localhost'], $tenant->domains);
$this->assertEqualsCanonicalizing(['completely.localhost', 'different.localhost', 'domains.localhost'], tenancy()->find($id)->domains); $this->assertEqualsCanonicalizing(['completely.localhost', 'different.localhost', 'domains.localhost'], tenancy()->find($id)->domains);
} }
/** @test */
public function with_methods_work()
{
$id = 'foo' . $this->randomString();
$tenant = Tenant::new()->withDomains(['foo.localhost'])->with('id', $id);
$this->assertSame($id, $tenant->id);
$id2 = 'bar' . $this->randomString();
$tenant2 = Tenant::new()->withDomains(['bar.localhost'])->withId($id2)->withFooBar('xyz');
$this->assertSame($id2, $tenant2->data['id']);
$this->assertSame('xyz', $tenant2->foo_bar);
$this->assertArrayHasKey('foo_bar', $tenant2->data);
}
} }