PHP code example of shrink / examples

1. Go to this page and download the library: Download shrink/examples library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

shrink / examples example snippets


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})."
);

use Shrink\Examples\Examples;

$examples = new Examples();

use Shrink\Examples\E;

$examples->register(E::define(Person::class, name: "Alice", age: 30));

use Shrink\Examples\E;

$example = $examples->make(E::g(Person::class, name: "Bob"));

echo "Hello, {$example->name} (age {$example->age}).";
// Hello, Bob (age 30).

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."
);

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
));

use Shrink\Examples\Definition;
use Shrink\Examples\ReflectionBuilder;

$examples->register(
  new Definition(Person::class, new ReflectionBuilder(Person::class), [
    "name" => "Alice",
    "age" => 30,
  ])
);

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,
    ]
  )
);

use Shrink\Examples\Example;

$person = $examples->make(new Example(Person::class));

use Shrink\Examples\Example;

$person = $examples->make(
  new Example(Person::class, [
    "name" => "Alice",
  ])
);