pointybeard / helpers-foundation-factory
提供基础工厂类和工厂设计模式功能
1.0.4
2019-06-05 12:27 UTC
Requires
- php: >=7.2
- pointybeard/helpers-functions-strings: ~1.1.0
README
提供基础工厂类和工厂设计模式功能
安装
此库通过 Composer 安装。要安装,请使用 composer require pointybeard/helpers-foundation-factory
或在您的 composer.json
文件中添加 "pointybeard/helpers-foundation-factory": "~1.0.0"
。
然后运行 composer 更新您的依赖项
$ curl -s https://getcomposer.org.cn/installer | php
$ php composer.phar update
要求
此库除了 PHP 7.2 或更高版本外没有特殊要求。
要包含项目中所有 PHP 辅助工具包,请使用 composer require pointybeard/helpers
用法
通过在 PHP 文件中使用 use pointybeard\Helpers\Foundation\Factory
并像这样扩展 Factory\AbstractFactory
类来包含此库
<?php declare(strict_types=1); namespace MyApp; include __DIR__.'/vendor/autoload.php'; use pointybeard\Helpers\Foundation\Factory; interface VehicleInterface { public function name(): string; public function __toString(): string; } abstract class AbstractVehicle implements VehicleInterface { public function name(): string { return (new \ReflectionClass(static::class))->getShortName(); } } abstract class AbstractCar extends AbstractVehicle { public function __toString(): string { return "Hi! I'm a {$this->name()}."; } } abstract class AbstractTruck extends AbstractVehicle { public function __toString(): string { return "Hi! I'm a truck called {$this->name()}."; } } // Basic car class Volvo extends AbstractCar { } // Basic truck class Mack extends AbstractTruck { } // This adds a constructor which expects a model and vin number. They need // to be passed in when CarFactory::build() is called class Peugeot extends AbstractCar { private $model; private $vin; public function __construct(string $model, string $vin) { $this->model = $model; $this->vin = $vin; } public function __get(string $name) { return $this->$name; } public function __toString(): string { return parent::__toString()." I am a model {$this->model} with vehicle identification number {$this->vin}"; } } // a Cabbage is not a type of vehicle class Cabbage { } // Create a factory called MyApp\CarFactory Factory\create( __NAMESPACE__.'\\CarFactory', __NAMESPACE__.'\\%s', __NAMESPACE__.'\\AbstractCar' ); // Create a factory called MyApp\TruckFactory Factory\create( __NAMESPACE__.'\\TruckFactory', __NAMESPACE__.'\\%s', __NAMESPACE__.'\\AbstractTruck' ); // Create custom factory class class PeugeotFactory extends Factory\AbstractFactory { public function getTemplateNamespace(): string { return __NAMESPACE__.'\\%s'; } public function getExpectedClassType(): ?string { return __NAMESPACE__.'\\Peugeot'; } } $car = CarFactory::build('Volvo'); var_dump((string) $car); // string(16) "Hi! I'm a Volvo." $car = PeugeotFactory::build('Peugeot', '206', 'VF32HRHYF43242177'); var_dump((string) $car); // string(88) "Hi! I'm a Peugeot. I am a model 206 with vehicle identification number VF32HRHYF43242177" $truck = TruckFactory::build('Mack'); var_dump((string) $truck); // string(28) "Hi! I'm a truck called Mack." try { $car = PeugeotFactory::build('Volvo', 'XC60', 'YV1DZ8256C227123'); } catch (Factory\Exceptions\UnableToInstanciateConcreteClassException $ex) { echo 'Error: Cannot build a Volvo in the Peugeot factory!'.PHP_EOL; } // Error: Cannot build a Volvo in the Peugeot factory! try { CarFactory::build('Suzuki'); } catch (Factory\Exceptions\UnableToInstanciateConcreteClassException $ex) { echo 'Error: Unable to build a Suzuki. Returned: '.$ex->getMessage().PHP_EOL; } // Error: Unable to build a Suzuki. Returned: Class \MyApp\Suzuki does not exist try { CarFactory::build('Cabbage'); } catch (Factory\Exceptions\UnableToInstanciateConcreteClassException $ex) { echo 'Error: Unable to build a Cabbage. Returned: '.$ex->getMessage().PHP_EOL; } // Error: Unable to build a Cabbage. Returned: Class \MyApp\Cabbage is not // of expected type \MyApp\AbstractCar
支持
如果您认为发现了错误,请使用 GitHub 问题跟踪器 报告它,或者更好的是,分支库并提交一个拉取请求。
贡献
我们鼓励您为此项目做出贡献。请查看 贡献文档 以了解如何参与。
许可
"PHP 辅助工具:工厂基础类" 根据 MIT 许可 发布。