PHP code example of tbpixel / type-adapter
1. Go to this page and download the library: Download tbpixel/type-adapter 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/ */
tbpixel / type-adapter example snippets
class Foo
{
/** @var string **/
public $name;
}
class Bar
{
/** @var string **/
public $name;
public function __construct(string $name)
{
$this->name = $name;
}
public static function fromFoo(Foo $foo): self
{
return new self($foo->name);
}
}
use TBPixel\TypeAdapter\Adaptable;
class FooToBarAdapter implements Adaptable
{
/**
* Adapts a Foo resource into a Bar resource.
*
* @param Foo $resource
*
* @return Bar
*/
public function adapt($resource)
{
return new Bar($resource->name);
}
/**
* Returns the acceptable valid resource type as either a string or an array.
*
* @return array|string
*/
public function expects()
{
return Foo::class;
}
}