Skip to content

Commit 14c4921

Browse files
committed
Add collection ordering feature
- Fix cs - Add setter for `withKeys` option
1 parent 9562457 commit 14c4921

16 files changed

+95
-41
lines changed

src/Collection.php

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44

55
namespace Murtukov\PHPCodeGenerator;
66

7+
use Closure;
78
use function count;
89
use function is_bool;
910
use function is_callable;
1011

1112
class Collection extends DependencyAwareGenerator
1213
{
13-
protected array $items = [];
14-
protected bool $multiline = false;
15-
protected bool $withKeys = true;
16-
protected array $converters = [];
14+
protected array $items = [];
15+
protected bool $multiline = false;
16+
protected bool $withKeys = true;
17+
protected array $converters = [];
18+
protected string $orderBy = 'none';
1719

1820
protected Utils $utils;
1921

@@ -43,9 +45,9 @@ public static function assoc(array $items = [], bool $multiline = true): self
4345
/**
4446
* Creates a multiline array and adds all provided items, after applying a callback to them.
4547
*/
46-
public static function map(array $items, callable $map): self
48+
public static function map(array $items, callable $map, bool $withKeys = true): self
4749
{
48-
$array = new static([], true);
50+
$array = new static([], true, $withKeys);
4951

5052
foreach ($items as $key => $value) {
5153
$array->addItem($key, $map($value, $key));
@@ -115,7 +117,7 @@ public function addIfNotFalse(string $key, $value): self
115117
/**
116118
* Returns self if value is true or callback returns true, otherwise returns a mock object.
117119
*
118-
* @param bool|\Closure $value
120+
* @param bool|Closure $value
119121
*
120122
* @return self|Mock
121123
*/
@@ -178,6 +180,14 @@ public function getFirstItem()
178180

179181
public function generate(): string
180182
{
183+
if ('none' !== $this->orderBy) {
184+
if ('asc' === $this->orderBy) {
185+
ksort($this->items);
186+
} elseif ('desc' === $this->orderBy) {
187+
krsort($this->items);
188+
}
189+
}
190+
181191
return $this->utils->stringify(
182192
$this->items,
183193
$this->multiline,
@@ -199,4 +209,15 @@ public function push($item): self
199209

200210
return $this;
201211
}
212+
213+
public function setKeyOrder(string $orderBy): self
214+
{
215+
$orderBy = strtolower($orderBy);
216+
217+
if (in_array($orderBy, ['none', 'asc', 'desc'])) {
218+
$this->orderBy = $orderBy;
219+
}
220+
221+
return $this;
222+
}
202223
}

src/Func.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
class Func extends AbstractFunction implements BlockInterface
88
{
9-
use ScopedContentTrait, DocBlockTrait;
9+
use ScopedContentTrait;
10+
use DocBlockTrait;
1011

1112
public function __construct(string $name, string $returnType = '')
1213
{
@@ -28,4 +29,4 @@ public function generate(): string
2829
}
2930
CODE;
3031
}
31-
}
32+
}

src/IfElse.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ public function generate(): string
5656
*/
5757
public function createElseIf($expression = ''): object
5858
{
59-
return $this->elseIfBlocks[] = new class($expression, $this) extends DependencyAwareGenerator
60-
{
59+
return $this->elseIfBlocks[] = new class($expression, $this) extends DependencyAwareGenerator {
6160
use ScopedContentTrait;
6261

6362
/** @var GeneratorInterface|string */
@@ -85,8 +84,7 @@ public function end()
8584

8685
public function createElse(): object
8786
{
88-
return $this->elseBlock = new class($this) extends DependencyAwareGenerator
89-
{
87+
return $this->elseBlock = new class($this) extends DependencyAwareGenerator {
9088
use ScopedContentTrait;
9189

9290
public IfElse $parent;

src/OOPStructureMemberInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44

55
interface OOPStructureMemberInterface
66
{
7-
}
7+
}

src/PhpTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,4 @@ public function generate(): string
5252
}
5353
CODE;
5454
}
55-
}
55+
}

src/Property.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function generate(): string
5454
$typeHint = '';
5555
if ($this->typeHint) {
5656
if ($this->isNullable) {
57-
$typeHint = "?";
57+
$typeHint = '?';
5858
}
5959

6060
$typeHint .= "$this->typeHint ";

src/ScopedContentTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ protected function generateContent(): string
7575
if (!empty($this->content)) {
7676
$content = Utils::indent(join(
7777
"\n",
78-
array_map(fn($line) => join('', $line), $this->content)
78+
array_map(fn ($line) => join('', $line), $this->content)
7979
));
8080
}
8181

tests/ArrowFunctionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function emptyBody()
2222

2323
return $arrow;
2424
}
25-
25+
2626
/**
2727
* @test
2828
* @depends emptyBody
@@ -31,7 +31,7 @@ public function setExpression(ArrowFunction $arrow)
3131
{
3232
$innerArrow = ArrowFunction::new([
3333
'name' => 'Alrik',
34-
'age' => 30
34+
'age' => 30,
3535
]);
3636

3737
$arrow->setExpression($innerArrow);

tests/CollectionTest.php

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@ public function conditionalAdd()
9898
->addIfNotFalse('number', 1)
9999
->addIfNotNull('type', null)
100100
->addIfNotNull('test', 'Test')
101-
->ifTrue(fn() => true)
101+
->ifTrue(fn () => true)
102102
->addItem('test2', 'value2')
103103
->ifTrue(true)
104104
->addItem('test3', 'value3')
105-
->ifTrue(fn() => false)
105+
->ifTrue(fn () => false)
106106
->addItem('test4', 'value4')
107107
->ifTrue(false)
108108
->addItem('test5', 'value5')
@@ -143,16 +143,16 @@ public function mapCollection()
143143
'args' => [
144144
[
145145
'name' => 'default',
146-
'type' => 'string'
146+
'type' => 'string',
147147
],
148148
[
149149
'name' => 'explicit',
150-
'type' => 'int'
150+
'type' => 'int',
151151
],
152-
]
152+
],
153153
];
154154

155-
$collection = Collection::map($array, function($val, $key) {
155+
$collection = Collection::map($array, function ($val, $key) {
156156
if ('constraints' === $key) {
157157
$collection = Collection::numeric()->setMultiline();
158158

@@ -203,16 +203,15 @@ public function mapCollection()
203203
*/
204204
public function stringifyWithCustomConverter()
205205
{
206-
$converter = new class implements ConverterInterface
207-
{
206+
$converter = new class() implements ConverterInterface {
208207
public function convert($value)
209208
{
210209
return new Text(ltrim($value, 'pre_'));
211210
}
212211

213212
public function check($string): bool
214213
{
215-
if (\is_string($string) && substr($string, 0, 4) === 'pre_') {
214+
if (\is_string($string) && 'pre_' === substr($string, 0, 4)) {
216215
return true;
217216
}
218217

@@ -249,4 +248,41 @@ public function check($string): bool
249248
Collection::assoc($array)->addConverter($converter)->generate()
250249
);
251250
}
251+
252+
/**
253+
* @test
254+
*/
255+
public function orderBy()
256+
{
257+
$collection = Collection::assoc();
258+
259+
$collection
260+
->addItem('name', 'Timur')
261+
->addItem('age', 30)
262+
->addItem('type', 'human')
263+
->addItem('friends', [])
264+
;
265+
266+
$collection->setKeyOrder('asc');
267+
268+
$this->assertEquals(<<<CODE
269+
[
270+
'age' => 30,
271+
'friends' => [],
272+
'name' => 'Timur',
273+
'type' => 'human',
274+
]
275+
CODE, $collection->generate());
276+
277+
$collection->setKeyOrder('DESC');
278+
279+
$this->assertEquals(<<<CODE
280+
[
281+
'type' => 'human',
282+
'name' => 'Timur',
283+
'friends' => [],
284+
'age' => 30,
285+
]
286+
CODE, $collection->generate());
287+
}
252288
}

tests/FuncTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Murtukov\PHPCodeGenerator\Argument;
66
use Murtukov\PHPCodeGenerator\Block;
77
use Murtukov\PHPCodeGenerator\Func;
8-
use Murtukov\PHPCodeGenerator\Instance;
98
use PHPUnit\Framework\TestCase;
109

1110
class FuncTest extends TestCase
@@ -77,4 +76,4 @@ function myMethod(?SplHeap $arg1 = null, string $arg2 = '', $arg3): void
7776

7877
return $func;
7978
}
80-
}
79+
}

0 commit comments

Comments
 (0)