shrink / examples
创建示例对象
Requires
- php: ^8.1
Requires (Dev)
- dq5studios/psalm-junit: ^2.0
- phpunit/phpunit: ^9.3
- squizlabs/php_codesniffer: ^3.5
- vimeo/psalm: ^4.3
README
创建示例对象。
use Shrink\Examples\E; use Shrink\Examples\Examples; final class Person { public function __construct( public readonly string $name, public readonly int $age ) { } } $examplePersonDefinition = E::define(Person::class, name: "Alice", age: 30); ($examples = new Examples())->register($examplePersonDefinition); $person = $examples->make(E::g(Person::class, name: "Bob")); self::assertSame( "Hello, Bob (age 30).", "Hello, {$person->name} (age {$person->age})." );
使用方法
安装
dev:~$ composer require shrink/examples --dev
🧶 示例的最新版本需要PHP 8.1,使用Examples v1以支持PHP 7.4和PHP 8.0。
实例化Examples
Examples::class
的实例包含你的示例定义,并从这些定义中创建对象。
use Shrink\Examples\Examples; $examples = new Examples();
定义示例
E::define()
方法接受一个类type
和一个或多个命名参数,这些参数映射到type
的构造函数参数。E::define()
返回一个定义,用于将你的Examples实例注册。
use Shrink\Examples\E; $examples->register(E::define(Person::class, name: "Alice", age: 30));
✨ 自v2版本以来,使用命名参数而不是参数数组。
🧬 E::define()
是创建简单示例定义的快捷方式,有关构建自己的实现的详细信息,请参阅内部 -> 定义。
创建对象
E::g()
方法接受一个类type
(指代已注册的示例)和一个或多个命名参数来覆盖示例默认值。E::g()
返回一个示例配置,你的Examples实例将使用它来make()
。
use Shrink\Examples\E; $example = $examples->make(E::g(Person::class, name: "Bob")); echo "Hello, {$example->name} (age {$example->age})."; // Hello, Bob (age 30).
🧬 E::g()
是创建简单示例配置的快捷方式,有关构建自己的实现的详细信息,请参阅内部 -> 创建。
特性
嵌套示例
Examples::class在创建示例时会解析遇到的任何示例定义,允许有多个层次的嵌套示例定义和配置。
final class Person { public function __construct( public readonly string $name, public readonly int $age, public readonly ?Location $location ) { } } final class Location { public function __construct( public readonly string $streetAddress, public readonly string $country ) { } } $examples = new Examples(); $examples->register( E::define( Location::class, streetAddress: "123 Default Street", country: "England" ) ); $examples->register( E::define( Person::class, name: "Alice", age: 30, location: E::g(Location::class, country: "United States") ) ); $person = $examples->make( E::g( Person::class, name: "Bob", location: E::g(Location::class, country: "The Netherlands") ) ); self::assertSame( "Hello, {$person->name} (age {$person->age}) from {$person->location->country}.", "Hello, Bob (age 30) from The Netherlands." );
内部
定义
使用示例定义(DefinesExample
)注册示例,它再使用构建器(BuildsExampleInstances
)创建一个对象,使用可选的配置。
use Shrink\Examples\Definition; use Shrink\Examples\Examples; use Shrink\Examples\Example; $examples = new Examples(); $examples->register(new Definition( Person::class, BuildsExampleInstances $builder, array $defaults )); $person = $examples->make(new Example( Person::class, array $parameters ));
隐式实例构建是通过ReflectionBuilder
通过反射处理的,它接受一个类名来构建。
use Shrink\Examples\Definition; use Shrink\Examples\ReflectionBuilder; $examples->register( new Definition(Person::class, new ReflectionBuilder(Person::class), [ "name" => "Alice", "age" => 30, ]) );
E::define()
是创建具有隐式构建的示例定义的快捷方式。显式实例构建是通过提供一个可调用来处理的,该可调用使用示例参数作为方法参数来调用。
use Shrink\Examples\CallableBuilder; use Shrink\Examples\Definition; $examples->register( new Definition( Person::class, new CallableBuilder( fn(string $name, int $age): Person => new Person($name, $age) ), [ "name" => "Alice", "age" => 30, ] ) );
创建
从示例中创建对象时,可以使用可选的参数
配置。请调用Examples
实例的make(ConfiguresExample $configuration)
方法。包含一个默认的ConfiguresExample
实现,它使用类型和参数构建。
use Shrink\Examples\Example; $person = $examples->make(new Example(Person::class));
可以提供参数以覆盖任何默认值。
use Shrink\Examples\Example; $person = $examples->make( new Example(Person::class, [ "name" => "Alice", ]) );
许可证
Examples
是开源软件,根据MIT许可证授权。