Skip to content

Commit

Permalink
Allow to change properties on factories
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Aug 23, 2018
1 parent 3c8b8af commit ae6ebda
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
43 changes: 42 additions & 1 deletion src/Carbon/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,55 @@ class Factory

protected $settings = [];

public function __construct(array $settings = [], $className = null)
public function __construct(array $settings = [], string $className = null)
{
if ($className) {
$this->className = $className;
}
$this->settings = $settings;
}

public function getClassName()
{
return $this->className;
}

public function setClassName(string $className)
{
$this->className = $className;

return $this;
}

public function className(string $className = null)
{
return $className === null ? $this->getClassName() : $this->setClassName($className);
}

public function getSettings()
{
return $this->settings;
}

public function setSettings(array $settings)
{
$this->settings = $settings;

return $this;
}

public function settings(array $settings = null)
{
return $settings === null ? $this->getSettings() : $this->setSettings($settings);
}

public function mergeSettings(array $settings)
{
$this->settings = array_merge($this->settings, $settings);

return $this;
}

public function __call($name, $arguments)
{
return $this->className::$name(...$arguments)->settings($this->settings);
Expand Down
2 changes: 1 addition & 1 deletion tests/Carbon/StringsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public function testIsoFormat()
$this->assertSame('2016', $d->isoFormat('G'));

$d = Carbon::parse('2017-01-01 22:25:24.182937');
$this->assertSame('1 18 182 1829 18293 182937', $d->isoFormat('S SS SSS SSSS SSSSS SSSSSS'));
$this->assertSame('1 18 182 1829 18293 182937 1829370 18293700 182937000', $d->isoFormat('S SS SSS SSSS SSSSS SSSSSS SSSSSSS SSSSSSSS SSSSSSSSS'));

$this->assertSame('02017 +002017', $d->isoFormat('YYYYY YYYYYY'));
$this->assertSame(-117, Carbon::create(-117, 1, 1)->year);
Expand Down
32 changes: 32 additions & 0 deletions tests/Factory/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,36 @@ public function testFactory()
$this->assertInstanceOf(CarbonImmutable::class, $factory->parse('2018-01-01'));
$this->assertSame('01/01/2018', $factory->parse('2018-01-01')->format('d/m/Y'));
}

public function testFactoryModification()
{
$factory = new Factory();

$this->assertSame(Carbon::class, $factory->className());
$this->assertSame($factory, $factory->className(MyCarbon::class));
$this->assertSame(MyCarbon::class, $factory->className());

$this->assertSame([], $factory->settings());
$this->assertSame($factory, $factory->settings([
'locale' => 'fr',
]));
$this->assertSame([
'locale' => 'fr',
], $factory->settings());

$this->assertSame($factory, $factory->mergeSettings([
'timezone' => 'Europe/Paris',
]));
$this->assertSame([
'locale' => 'fr',
'timezone' => 'Europe/Paris',
], $factory->settings());

$this->assertSame($factory, $factory->settings([
'timezone' => 'Europe/Paris',
]));
$this->assertSame([
'timezone' => 'Europe/Paris',
], $factory->settings());
}
}

0 comments on commit ae6ebda

Please sign in to comment.