|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace Bug6633; |
| 4 | + |
| 5 | +use function PHPStan\Testing\assertType; |
| 6 | + |
| 7 | +class CreateServiceSolrData |
| 8 | +{ |
| 9 | + public ?string $name; |
| 10 | + public ?string $version; |
| 11 | +} |
| 12 | + |
| 13 | +class CreateServiceRedisData |
| 14 | +{ |
| 15 | + public ?string $name; |
| 16 | + public ?string $version; |
| 17 | + public ?bool $persistent; |
| 18 | + |
| 19 | +} |
| 20 | + |
| 21 | +class ServiceSolr |
| 22 | +{ |
| 23 | + public function __construct( |
| 24 | + private string $name, |
| 25 | + private string $version, |
| 26 | + ) {} |
| 27 | + |
| 28 | + public function touchAll() : string{ |
| 29 | + return $this->name . $this->version; |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +class ServiceRedis |
| 34 | +{ |
| 35 | + public function __construct( |
| 36 | + private string $name, |
| 37 | + private string $version, |
| 38 | + private bool $persistent, |
| 39 | + ) {} |
| 40 | + |
| 41 | + public function touchAll() : string{ |
| 42 | + return $this->persistent ? $this->name : $this->version; |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +function test(?string $type = NULL) : void { |
| 47 | + $types = [ |
| 48 | + 'solr' => [ |
| 49 | + 'label' => 'SOLR Search', |
| 50 | + 'data_class' => CreateServiceSolrData::class, |
| 51 | + 'to_entity' => function (CreateServiceSolrData $data) { |
| 52 | + assert($data->name !== NULL && $data->version !== NULL, "Incorrect form validation"); |
| 53 | + return new ServiceSolr($data->name, $data->version); |
| 54 | + }, |
| 55 | + ], |
| 56 | + 'redis' => [ |
| 57 | + 'label' => 'Redis', |
| 58 | + 'data_class' => CreateServiceRedisData::class, |
| 59 | + 'to_entity' => function (CreateServiceRedisData $data) { |
| 60 | + assert($data->name !== NULL && $data->version !== NULL && $data->persistent !== NULL, "Incorrect form validation"); |
| 61 | + return new ServiceRedis($data->name, $data->version, $data->persistent); |
| 62 | + }, |
| 63 | + ], |
| 64 | + ]; |
| 65 | + |
| 66 | + if ($type === NULL || !isset($types[$type])) { |
| 67 | + throw new \RuntimeException("404 or choice form here"); |
| 68 | + } |
| 69 | + |
| 70 | + $data = new $types[$type]['data_class'](); |
| 71 | + |
| 72 | + $service = $types[$type]['to_entity']($data); |
| 73 | + |
| 74 | + assertType('Bug6633\ServiceRedis|Bug6633\ServiceSolr', $service); |
| 75 | +} |
0 commit comments