Skip to content

Commit 756b3e8

Browse files
committed
Move AllOf, AnyOf and OneOf
1 parent ade3402 commit 756b3e8

23 files changed

+257
-237
lines changed

docs/processors.md

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,6 @@ DefaultProcessor::fromFiltersAndValidators(...$chain)
175175
|-----------|---------------------|
176176
| ...$chain | Filter or Validator |
177177

178-
## OpenAPI Processors
179-
180-
The following processors are intended for Open-API specific use cases.
181-
Alternative uses are not recommended.
182-
183178
### AllOf
184179

185180
Designed specifically to deal with the 'allOf' keyword of OpenAPI:
@@ -213,14 +208,10 @@ Designed specifically to deal with the 'oneOf' keyword of OpenAPI:
213208
The OneOf processor takes a chain of processors (one for each schema within the 'oneOf') and makes sure that one and
214209
only one processor returns a valid result.
215210

216-
### Json
217-
218-
| Parameter | Type |
219-
|-----------|-----------|
220-
| $wrapped | Processor |
211+
## OpenAPI Processors
221212

222-
Designed specifically to wrap a processor that is expecting a json object. The Json processor attempts to decode a
223-
string into a json object before passing it into the wrapped processor.
213+
The following processors are intended for Open-API specific use cases.
214+
Alternative uses are not recommended.
224215

225216
### Request
226217

src/OpenAPI/Builder/APIBuilder.php

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

77
use Membrane\Builder\Builder;
88
use Membrane\OpenAPI;
9-
use Membrane\OpenAPI\Processor\AllOf;
10-
use Membrane\OpenAPI\Processor\AnyOf;
11-
use Membrane\OpenAPI\Processor\OneOf;
129
use Membrane\OpenAPIReader\ValueObject\Valid\{Enum\Type, V30, V31};
1310
use Membrane\Processor;
11+
use Membrane\Processor\AllOf;
12+
use Membrane\Processor\AnyOf;
1413
use Membrane\Processor\Field;
14+
use Membrane\Processor\OneOf;
1515
use Membrane\Validator\Type\IsNull;
1616
use Membrane\Validator\Utility;
1717

src/OpenAPI/Processor/AllOf.php

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,54 +4,7 @@
44

55
namespace Membrane\OpenAPI\Processor;
66

7-
use Membrane\Exception\InvalidProcessorArguments;
8-
use Membrane\Processor;
9-
use Membrane\Result\FieldName;
10-
use Membrane\Result\Result;
11-
12-
class AllOf implements Processor
7+
/** @deprecated Use \Membrane\Processor\AllOf instead */
8+
final class AllOf extends \Membrane\Processor\AllOf
139
{
14-
/** @var Processor[] */
15-
public array $processors;
16-
17-
public function __construct(private readonly string $processes, Processor ...$processors)
18-
{
19-
if (count($processors) < 2) {
20-
throw InvalidProcessorArguments::redundantProcessor(AllOf::class);
21-
}
22-
$this->processors = $processors;
23-
}
24-
25-
public function __toString(): string
26-
{
27-
return "All of the following:\n\t" .
28-
implode(".\n\t", array_map(fn($p) => preg_replace("#\n#m", "\n\t", (string)$p), $this->processors)) . '.';
29-
}
30-
31-
public function __toPHP(): string
32-
{
33-
return sprintf(
34-
'new %s("%s"%s)',
35-
self::class,
36-
$this->processes(),
37-
implode('', array_map(fn($p) => ', ' . $p->__toPHP(), $this->processors))
38-
);
39-
}
40-
41-
public function processes(): string
42-
{
43-
return $this->processes;
44-
}
45-
46-
public function process(FieldName $parentFieldName, mixed $value): Result
47-
{
48-
$result = Result::noResult($value);
49-
50-
foreach ($this->processors as $processor) {
51-
$itemResult = $processor->process($parentFieldName, $value);
52-
$result = $result->merge($itemResult);
53-
}
54-
55-
return $result;
56-
}
5710
}

src/OpenAPI/Processor/AnyOf.php

Lines changed: 2 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,70 +4,7 @@
44

55
namespace Membrane\OpenAPI\Processor;
66

7-
use Membrane\Exception\InvalidProcessorArguments;
8-
use Membrane\Processor;
9-
use Membrane\Result\FieldName;
10-
use Membrane\Result\Result;
11-
12-
class AnyOf implements Processor
7+
/** @deprecated Use \Membrane\Processor\AnyOf instead */
8+
final class AnyOf extends \Membrane\Processor\AnyOf
139
{
14-
/** @var Processor[] */
15-
public array $processors;
16-
17-
public function __construct(private readonly string $processes, Processor ...$processors)
18-
{
19-
if (count($processors) < 2) {
20-
throw InvalidProcessorArguments::redundantProcessor(AnyOf::class);
21-
}
22-
$this->processors = $processors;
23-
}
24-
25-
public function __toPHP(): string
26-
{
27-
return sprintf(
28-
'new %s("%s"%s)',
29-
self::class,
30-
$this->processes(),
31-
implode('', array_map(fn($p) => ', ' . $p->__toPHP(), $this->processors))
32-
);
33-
}
34-
35-
public function __toString(): string
36-
{
37-
return "Any of the following:\n\t" .
38-
implode(".\n\t", array_map(fn($p) => preg_replace("#\n#m", "\n\t", (string)$p), $this->processors)) . '.';
39-
}
40-
41-
public function processes(): string
42-
{
43-
return $this->processes;
44-
}
45-
46-
public function process(FieldName $parentFieldName, mixed $value): Result
47-
{
48-
$results = [];
49-
$messageSets = [];
50-
51-
foreach ($this->processors as $fieldSet) {
52-
$itemResult = $fieldSet->process($parentFieldName, $value);
53-
54-
if ($itemResult->result === Result::VALID) {
55-
return $itemResult;
56-
}
57-
58-
if ($itemResult->result === Result::INVALID) {
59-
$messageSets [] = $itemResult->messageSets[0];
60-
}
61-
62-
$results [] = $itemResult->result;
63-
}
64-
65-
$result = in_array(Result::INVALID, $results) ? Result::INVALID : Result::NO_RESULT;
66-
67-
return new Result(
68-
$value,
69-
$result,
70-
...($result === Result::INVALID ? $messageSets : [])
71-
);
72-
}
7310
}

src/OpenAPI/Processor/OneOf.php

Lines changed: 2 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -4,81 +4,7 @@
44

55
namespace Membrane\OpenAPI\Processor;
66

7-
use Membrane\Exception\InvalidProcessorArguments;
8-
use Membrane\Processor;
9-
use Membrane\Result\FieldName;
10-
use Membrane\Result\Message;
11-
use Membrane\Result\MessageSet;
12-
use Membrane\Result\Result;
13-
14-
class OneOf implements Processor
7+
/** @deprecated Use \Membrane\Processor\OneOf instead */
8+
final class OneOf extends \Membrane\Processor\OneOf
159
{
16-
/** @var Processor[] */
17-
public array $processors;
18-
19-
public function __construct(private readonly string $processes, Processor ...$processors)
20-
{
21-
if (count($processors) < 2) {
22-
throw InvalidProcessorArguments::redundantProcessor(OneOf::class);
23-
}
24-
$this->processors = $processors;
25-
}
26-
27-
public function __toPHP(): string
28-
{
29-
return sprintf(
30-
'new %s("%s"%s)',
31-
self::class,
32-
$this->processes(),
33-
implode('', array_map(fn($p) => ', ' . $p->__toPHP(), $this->processors))
34-
);
35-
}
36-
37-
public function __toString(): string
38-
{
39-
return "One of the following:\n\t" .
40-
implode(".\n\t", array_map(fn($p) => preg_replace("#\n#m", "\n\t", (string)$p), $this->processors)) . '.';
41-
}
42-
43-
public function processes(): string
44-
{
45-
return $this->processes;
46-
}
47-
48-
public function process(FieldName $parentFieldName, mixed $value): Result
49-
{
50-
$results = array_map(
51-
fn($p) => $p->process($parentFieldName, $value),
52-
$this->processors
53-
);
54-
55-
if ($this->hasExactlyOneValidResult($results)) {
56-
return Result::valid($value);
57-
}
58-
59-
$messageSets = [
60-
new MessageSet(
61-
$parentFieldName,
62-
new Message('one and only one schema must pass', [])
63-
),
64-
];
65-
66-
foreach ($results as $result) {
67-
if (!$result->isValid()) {
68-
foreach ($result->messageSets as $messageSet) {
69-
if (!$messageSet->isEmpty()) {
70-
$messageSets[] = $messageSet;
71-
}
72-
}
73-
}
74-
}
75-
76-
return Result::invalid($value, ...$messageSets);
77-
}
78-
79-
/** @param Result[] $results */
80-
private function hasExactlyOneValidResult(array $results): bool
81-
{
82-
return count(array_filter($results, fn($r) => $r->isValid())) === 1;
83-
}
8410
}

src/Processor/AllOf.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Membrane\Processor;
6+
7+
use Membrane\Exception\InvalidProcessorArguments;
8+
use Membrane\Processor;
9+
use Membrane\Result\FieldName;
10+
use Membrane\Result\Result;
11+
12+
class AllOf implements Processor
13+
{
14+
/** @var Processor[] */
15+
public array $processors;
16+
17+
public function __construct(private readonly string $processes, Processor ...$processors)
18+
{
19+
if (count($processors) < 2) {
20+
throw InvalidProcessorArguments::redundantProcessor(AllOf::class);
21+
}
22+
$this->processors = $processors;
23+
}
24+
25+
public function __toString(): string
26+
{
27+
return "All of the following:\n\t" .
28+
implode(".\n\t", array_map(fn($p) => preg_replace("#\n#m", "\n\t", (string)$p), $this->processors)) . '.';
29+
}
30+
31+
public function __toPHP(): string
32+
{
33+
return sprintf(
34+
'new %s("%s"%s)',
35+
self::class,
36+
$this->processes(),
37+
implode('', array_map(fn($p) => ', ' . $p->__toPHP(), $this->processors))
38+
);
39+
}
40+
41+
public function processes(): string
42+
{
43+
return $this->processes;
44+
}
45+
46+
public function process(FieldName $parentFieldName, mixed $value): Result
47+
{
48+
$result = Result::noResult($value);
49+
50+
foreach ($this->processors as $processor) {
51+
$itemResult = $processor->process($parentFieldName, $value);
52+
$result = $result->merge($itemResult);
53+
}
54+
55+
return $result;
56+
}
57+
}

src/Processor/AnyOf.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Membrane\Processor;
6+
7+
use Membrane\Exception\InvalidProcessorArguments;
8+
use Membrane\Processor;
9+
use Membrane\Result\FieldName;
10+
use Membrane\Result\Result;
11+
12+
class AnyOf implements Processor
13+
{
14+
/** @var Processor[] */
15+
public array $processors;
16+
17+
public function __construct(private readonly string $processes, Processor ...$processors)
18+
{
19+
if (count($processors) < 2) {
20+
throw InvalidProcessorArguments::redundantProcessor(AnyOf::class);
21+
}
22+
$this->processors = $processors;
23+
}
24+
25+
public function __toPHP(): string
26+
{
27+
return sprintf(
28+
'new %s("%s"%s)',
29+
self::class,
30+
$this->processes(),
31+
implode('', array_map(fn($p) => ', ' . $p->__toPHP(), $this->processors))
32+
);
33+
}
34+
35+
public function __toString(): string
36+
{
37+
return "Any of the following:\n\t" .
38+
implode(".\n\t", array_map(fn($p) => preg_replace("#\n#m", "\n\t", (string)$p), $this->processors)) . '.';
39+
}
40+
41+
public function processes(): string
42+
{
43+
return $this->processes;
44+
}
45+
46+
public function process(FieldName $parentFieldName, mixed $value): Result
47+
{
48+
$results = [];
49+
$messageSets = [];
50+
51+
foreach ($this->processors as $fieldSet) {
52+
$itemResult = $fieldSet->process($parentFieldName, $value);
53+
54+
if ($itemResult->result === Result::VALID) {
55+
return $itemResult;
56+
}
57+
58+
if ($itemResult->result === Result::INVALID) {
59+
$messageSets [] = $itemResult->messageSets[0];
60+
}
61+
62+
$results [] = $itemResult->result;
63+
}
64+
65+
$result = in_array(Result::INVALID, $results) ? Result::INVALID : Result::NO_RESULT;
66+
67+
return new Result(
68+
$value,
69+
$result,
70+
...($result === Result::INVALID ? $messageSets : [])
71+
);
72+
}
73+
}

0 commit comments

Comments
 (0)