-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
B#188652 AccessorPair accept all array notations (#62)
* B#188652 AccessorPair accept all array notations
- Loading branch information
1 parent
973706c
commit 82cc2fc
Showing
10 changed files
with
188 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/Constraint/ValueProvider/Compound/ArrayShapeProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace DigitalRevolution\AccessorPairConstraint\Constraint\ValueProvider\Compound; | ||
|
||
use DigitalRevolution\AccessorPairConstraint\Constraint\ValueProvider\ValueProvider; | ||
|
||
class ArrayShapeProvider implements ValueProvider | ||
{ | ||
/** | ||
* @param array<string, ValueProvider> $items | ||
*/ | ||
public function __construct(protected array $items) | ||
{ | ||
} | ||
|
||
/** | ||
* @return array<array<int|string, mixed>> | ||
* @inheritDoc | ||
*/ | ||
public function getValues(): array | ||
{ | ||
$testArray = []; | ||
$keyValues = []; | ||
|
||
$minValues = null; | ||
foreach ($this->items as $key => $item) { | ||
$values = $item->getValues(); | ||
if ($minValues === null || count($values) < $minValues) { | ||
$minValues = count($values); | ||
} | ||
$keyValues[$key] = $item->getValues(); | ||
} | ||
|
||
foreach ($keyValues as $key => $values) { | ||
$keyValues[$key] = array_slice($values, 0, $minValues); | ||
} | ||
|
||
foreach ($keyValues as $key => $values) { | ||
foreach ($values as $index => $value) { | ||
$testArray[$index][$key] = $value; | ||
} | ||
} | ||
|
||
return $testArray; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
tests/Integration/data/success/Regular/Types/CompoundTypes/ArrayShapeProperty.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace DigitalRevolution\AccessorPairConstraint\Tests\Integration\data\success\Regular\Types\CompoundTypes; | ||
|
||
class ArrayShapeProperty | ||
{ | ||
/** @var array{foo: int, bar: string}[] */ | ||
private array $items; | ||
|
||
/** | ||
* @param array{foo: int, bar: string}[] $items | ||
*/ | ||
public function __construct(array $items) | ||
{ | ||
$this->items = $items; | ||
} | ||
|
||
/** | ||
* @param array{foo: int, bar: string}[] $items | ||
*/ | ||
public function setItems(array $items): void | ||
{ | ||
$this->items = $items; | ||
} | ||
|
||
/** | ||
* @return array{foo: int, bar: string}[] | ||
*/ | ||
public function getItems(): array | ||
{ | ||
return $this->items; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
tests/Unit/Constraint/MethodPair/AccessorPair/data/success/TypedArrayShapeSetGet.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace DigitalRevolution\AccessorPairConstraint\Tests\Unit\Constraint\MethodPair\AccessorPair\data\success; | ||
|
||
use DigitalRevolution\AccessorPairConstraint\Tests\Unit\Constraint\MethodPair\AbstractDataClass; | ||
|
||
class TypedArrayShapeSetGet extends AbstractDataClass | ||
{ | ||
/** @var array{foo: int, bar: string}[] */ | ||
private $items; | ||
|
||
/** | ||
* @param array{foo: int, bar: string}[] $items | ||
*/ | ||
public function __construct(array $items) | ||
{ | ||
$this->items = $items; | ||
} | ||
|
||
/** | ||
* @param array{foo: int, bar: string} $item | ||
*/ | ||
public function addItem(array $item): void | ||
{ | ||
$this->items[] = $item; | ||
} | ||
|
||
/** | ||
* @param array{foo: int, bar: string}[] $items | ||
*/ | ||
public function setItems(array $items): void | ||
{ | ||
$this->items = $items; | ||
} | ||
|
||
/** | ||
* @return array{foo: int, bar: string}[] | ||
*/ | ||
public function getItems(): array | ||
{ | ||
return $this->items; | ||
} | ||
|
||
public function getExpectedPairs(): array | ||
{ | ||
return [['getItems', 'setItems', false], ['getItems', 'addItem', true]]; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
tests/Unit/Constraint/ValueProvider/Compound/ArrayShapeProviderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Constraint\ValueProvider\Compound; | ||
|
||
use DigitalRevolution\AccessorPairConstraint\Constraint\ValueProvider\Compound\ArrayShapeProvider; | ||
use DigitalRevolution\AccessorPairConstraint\Constraint\ValueProvider\Scalar\IntProvider; | ||
use DigitalRevolution\AccessorPairConstraint\Constraint\ValueProvider\Scalar\StringProvider; | ||
use DigitalRevolution\AccessorPairConstraint\Tests\Unit\Constraint\ValueProvider\AbstractValueProviderTestCase; | ||
use Exception; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\UsesClass; | ||
|
||
#[CoversClass(ArrayShapeProvider::class)] | ||
#[UsesClass(IntProvider::class)] | ||
#[UsesClass(StringProvider::class)] | ||
class ArrayShapeProviderTest extends AbstractValueProviderTestCase | ||
{ | ||
/** | ||
* @throws Exception | ||
*/ | ||
public function testGetValues(): void | ||
{ | ||
$valueProvider = new ArrayShapeProvider(['foo' => new IntProvider(), 'bar' => new StringProvider()]); | ||
$values = $valueProvider->getValues(); | ||
|
||
static::assertNotCount(0, $values); | ||
foreach ($values as $value) { | ||
static::assertCount(2, $value); | ||
static::assertArrayHasKey('foo', $value); | ||
static::assertIsInt($value['foo']); | ||
static::assertArrayHasKey('bar', $value); | ||
static::assertIsString($value['bar']); | ||
} | ||
} | ||
} |