Skip to content

Commit

Permalink
Rename "model" to "object" (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed May 9, 2023
1 parent dd6403b commit 1067099
Show file tree
Hide file tree
Showing 30 changed files with 223 additions and 223 deletions.
2 changes: 1 addition & 1 deletion rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@

$rectorConfig->skip([
ClosureToArrowFunctionRector::class,
__DIR__ . '/tests/Support/Model/SimpleModel.php',
__DIR__ . '/tests/Support/Classes/SimpleClass.php',
]);
};
14 changes: 7 additions & 7 deletions src/Hydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,26 @@ public function __construct(
$this->parameterAttributesHandler = new ParameterAttributesHandler($container);
}

public function hydrate(object $model, array $data = [], array $map = [], bool $strict = false): void
public function hydrate(object $object, array $data = [], array $map = [], bool $strict = false): void
{
$this->populate(
$model,
$this->getHydrateData($model, $data, $map, $strict),
$object,
$this->getHydrateData($object, $data, $map, $strict),
);
}

public function create(string $class, array $data = [], array $map = [], bool $strict = false): object
{
[$excludeProperties, $constructorArguments] = $this->getConstructorArguments($class, $data, $map, $strict);

$model = $this->injector->make($class, $constructorArguments);
$object = $this->injector->make($class, $constructorArguments);

$this->populate(
$model,
$this->getHydrateData($model, $data, $map, $strict, $excludeProperties),
$object,
$this->getHydrateData($object, $data, $map, $strict, $excludeProperties),
);

return $model;
return $object;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/HydratorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface HydratorInterface
/**
* @psalm-param MapType $map
*/
public function hydrate(object $model, array $data = [], array $map = [], bool $strict = false): void;
public function hydrate(object $object, array $data = [], array $map = [], bool $strict = false): void;

/**
* @psalm-template T
Expand Down
14 changes: 7 additions & 7 deletions tests/Attribute/Data/MapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use Yiisoft\Hydrator\Hydrator;
use Yiisoft\Hydrator\Tests\Support\Attribute\FromPredefinedArray;
use Yiisoft\Hydrator\Tests\Support\Attribute\FromPredefinedArrayResolver;
use Yiisoft\Hydrator\Tests\Support\Model\FromPredefinedArrayModel;
use Yiisoft\Hydrator\Tests\Support\Model\MapModel;
use Yiisoft\Hydrator\Tests\Support\Classes\FromPredefinedArrayClass;
use Yiisoft\Hydrator\Tests\Support\Classes\MapClass;
use Yiisoft\Hydrator\UnexpectedAttributeException;
use Yiisoft\Test\Support\Container\SimpleContainer;

Expand All @@ -20,10 +20,10 @@ public function testBase(): void
{
$hydrator = new Hydrator(new SimpleContainer());

$model = $hydrator->create(MapModel::class);
$object = $hydrator->create(MapClass::class);

$this->assertSame('1', $model->a);
$this->assertSame('2', $model->b);
$this->assertSame('1', $object->a);
$this->assertSame('2', $object->b);
}

public function testUnexpectedAttributeException(): void
Expand All @@ -32,10 +32,10 @@ public function testUnexpectedAttributeException(): void
new SimpleContainer([FromPredefinedArrayResolver::class => new Map([])])
);

$model = new FromPredefinedArrayModel();
$object = new FromPredefinedArrayClass();

$this->expectException(UnexpectedAttributeException::class);
$this->expectExceptionMessage('Expected "' . Map::class . '", but "' . FromPredefinedArray::class . '" given.');
$hydrator->hydrate($model);
$hydrator->hydrate($object);
}
}
18 changes: 9 additions & 9 deletions tests/Attribute/Data/StrictTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@
use Yiisoft\Hydrator\Hydrator;
use Yiisoft\Hydrator\Tests\Support\Attribute\FromPredefinedArray;
use Yiisoft\Hydrator\Tests\Support\Attribute\FromPredefinedArrayResolver;
use Yiisoft\Hydrator\Tests\Support\Model\FromPredefinedArrayModel;
use Yiisoft\Hydrator\Tests\Support\Model\StrictModel;
use Yiisoft\Hydrator\Tests\Support\Classes\FromPredefinedArrayClass;
use Yiisoft\Hydrator\Tests\Support\Classes\StrictClass;
use Yiisoft\Hydrator\UnexpectedAttributeException;
use Yiisoft\Test\Support\Container\SimpleContainer;

final class StrictTest extends TestCase
{
public function testBase(): void
{
$service = new Hydrator(new SimpleContainer());
$hydrator = new Hydrator(new SimpleContainer());

$model = $service->create(StrictModel::class);
$object = $hydrator->create(StrictClass::class);

$this->assertSame('1', $model->a);
$this->assertSame('2', $model->b);
$this->assertSame('.', $model->c);
$this->assertSame('1', $object->a);
$this->assertSame('2', $object->b);
$this->assertSame('.', $object->c);
}

public function testUnexpectedAttributeException(): void
Expand All @@ -33,10 +33,10 @@ public function testUnexpectedAttributeException(): void
new SimpleContainer([FromPredefinedArrayResolver::class => new Strict()])
);

$model = new FromPredefinedArrayModel();
$object = new FromPredefinedArrayClass();

$this->expectException(UnexpectedAttributeException::class);
$this->expectExceptionMessage('Expected "' . Strict::class . '", but "' . FromPredefinedArray::class . '" given.');
$hydrator->hydrate($model);
$hydrator->hydrate($object);
}
}
54 changes: 27 additions & 27 deletions tests/Attribute/Parameter/DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@
use Yiisoft\Hydrator\Hydrator;
use Yiisoft\Hydrator\Tests\Support\Attribute\Counter;
use Yiisoft\Hydrator\Tests\Support\Attribute\CounterResolver;
use Yiisoft\Hydrator\Tests\Support\Model\CounterModel;
use Yiisoft\Hydrator\Tests\Support\Classes\CounterClass;
use Yiisoft\Hydrator\UnexpectedAttributeException;
use Yiisoft\Test\Support\Container\SimpleContainer;

final class DataTest extends TestCase
{
public function testBase(): void
{
$service = new Hydrator(new SimpleContainer());
$hydrator = new Hydrator(new SimpleContainer());

$model = new class () {
$object = new class () {
#[Data('a')]
public ?int $x = null;

#[Data('b')]
public ?int $y = null;
};

$service->hydrate(
$model,
$hydrator->hydrate(
$object,
data: [
'a' => 99,
'b' => 88,
Expand All @@ -37,41 +37,41 @@ public function testBase(): void
],
);

$this->assertSame(99, $model->x);
$this->assertSame(88, $model->y);
$this->assertSame(99, $object->x);
$this->assertSame(88, $object->y);
}

public function testWholeData(): void
{
$service = new Hydrator(new SimpleContainer());
$hydrator = new Hydrator(new SimpleContainer());

$model = new class () {
$object = new class () {
#[Data]
public array $data = [];
};

$service->hydrate(
$model,
$hydrator->hydrate(
$object,
data: ['a' => 1, 'b' => 2],
);

$this->assertSame(['a' => 1, 'b' => 2], $model->data);
$this->assertSame(['a' => 1, 'b' => 2], $object->data);
}

public function testPath(): void
{
$service = new Hydrator(new SimpleContainer());
$hydrator = new Hydrator(new SimpleContainer());

$model = new class () {
$object = new class () {
#[Data('nested.n')]
public ?int $y = null;

#[Data(['nested', 'nested2', 'n'])]
public ?int $z = null;
};

$service->hydrate(
$model,
$hydrator->hydrate(
$object,
data: [
'nested' => [
'n' => 2,
Expand All @@ -82,15 +82,15 @@ public function testPath(): void
],
);

$this->assertSame(2, $model->y);
$this->assertSame(3, $model->z);
$this->assertSame(2, $object->y);
$this->assertSame(3, $object->z);
}

public function testMapping(): void
{
$service = new Hydrator(new SimpleContainer());
$hydrator = new Hydrator(new SimpleContainer());

$model = new class () {
$object = new class () {
#[Data('a')]
public ?int $x = null;

Expand All @@ -101,8 +101,8 @@ public function testMapping(): void
public ?int $z = null;
};

$service->hydrate(
$model,
$hydrator->hydrate(
$object,
data: [
'value' => 1,
'nested' => [
Expand All @@ -119,9 +119,9 @@ public function testMapping(): void
]
);

$this->assertSame(1, $model->x);
$this->assertSame(2, $model->y);
$this->assertSame(3, $model->z);
$this->assertSame(1, $object->x);
$this->assertSame(2, $object->y);
$this->assertSame(3, $object->z);
}

public function testUnexpectedAttributeException(): void
Expand All @@ -130,10 +130,10 @@ public function testUnexpectedAttributeException(): void
new SimpleContainer([CounterResolver::class => new Data()])
);

$model = new CounterModel();
$object = new CounterClass();

$this->expectException(UnexpectedAttributeException::class);
$this->expectExceptionMessage('Expected "' . Data::class . '", but "' . Counter::class . '" given.');
$hydrator->hydrate($model);
$hydrator->hydrate($object);
}
}
6 changes: 3 additions & 3 deletions tests/Attribute/Parameter/DiResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Yiisoft\Hydrator\Hydrator;
use Yiisoft\Hydrator\Tests\Support\Attribute\Counter;
use Yiisoft\Hydrator\Tests\Support\Attribute\CounterResolver;
use Yiisoft\Hydrator\Tests\Support\Model\CounterModel;
use Yiisoft\Hydrator\Tests\Support\Classes\CounterClass;
use Yiisoft\Hydrator\UnexpectedAttributeException;
use Yiisoft\Test\Support\Container\SimpleContainer;

Expand All @@ -22,10 +22,10 @@ public function testUnexpectedAttributeException(): void
new SimpleContainer([CounterResolver::class => new DiResolver(new SimpleContainer())])
);

$model = new CounterModel();
$object = new CounterClass();

$this->expectException(UnexpectedAttributeException::class);
$this->expectExceptionMessage('Expected "' . Di::class . '", but "' . Counter::class . '" given.');
$hydrator->hydrate($model);
$hydrator->hydrate($object);
}
}
18 changes: 9 additions & 9 deletions tests/Attribute/Parameter/ToStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Yiisoft\Hydrator\Hydrator;
use Yiisoft\Hydrator\Tests\Support\Attribute\Counter;
use Yiisoft\Hydrator\Tests\Support\Attribute\CounterResolver;
use Yiisoft\Hydrator\Tests\Support\Model\CounterModel;
use Yiisoft\Hydrator\Tests\Support\Classes\CounterClass;
use Yiisoft\Hydrator\Tests\Support\StringableObject;
use Yiisoft\Hydrator\TypeCaster\NoTypeCaster;
use Yiisoft\Hydrator\UnexpectedAttributeException;
Expand Down Expand Up @@ -37,28 +37,28 @@ public function testBase(string $expected, mixed $value): void
{
$hydrator = new Hydrator(new SimpleContainer(), new NoTypeCaster());

$model = new class () {
$object = new class () {
#[ToString]
public string $a = '...';
};

$hydrator->hydrate($model, ['a' => $value]);
$hydrator->hydrate($object, ['a' => $value]);

$this->assertSame($expected, $model->a);
$this->assertSame($expected, $object->a);
}

public function testNotResolved(): void
{
$hydrator = new Hydrator(new SimpleContainer());

$model = new class () {
$object = new class () {
#[ToString]
public string $a = '...';
};

$hydrator->hydrate($model);
$hydrator->hydrate($object);

$this->assertSame('...', $model->a);
$this->assertSame('...', $object->a);
}

public function testUnexpectedAttributeException(): void
Expand All @@ -67,10 +67,10 @@ public function testUnexpectedAttributeException(): void
new SimpleContainer([CounterResolver::class => new ToString()])
);

$model = new CounterModel();
$object = new CounterClass();

$this->expectException(UnexpectedAttributeException::class);
$this->expectExceptionMessage('Expected "' . ToString::class . '", but "' . Counter::class . '" given.');
$hydrator->hydrate($model);
$hydrator->hydrate($object);
}
}
14 changes: 7 additions & 7 deletions tests/Attribute/SkipHydrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@

use PHPUnit\Framework\TestCase;
use Yiisoft\Hydrator\Hydrator;
use Yiisoft\Hydrator\Tests\Support\Model\SkipHydrationModel;
use Yiisoft\Hydrator\Tests\Support\Classes\SkipHydrationClass;
use Yiisoft\Test\Support\Container\SimpleContainer;

final class SkipHydrationTest extends TestCase
{
public function testBase(): void
{
$service = new Hydrator(new SimpleContainer());
$hydrator = new Hydrator(new SimpleContainer());

$model = $service->create(SkipHydrationModel::class, ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4]);
$object = $hydrator->create(SkipHydrationClass::class, ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4]);

$this->assertNull($model->a);
$this->assertSame(2, $model->b);
$this->assertNull($model->c);
$this->assertSame(4, $model->d);
$this->assertNull($object->a);
$this->assertSame(2, $object->b);
$this->assertNull($object->c);
$this->assertSame(4, $object->d);
}
}

0 comments on commit 1067099

Please sign in to comment.