jhumayun/shapes
Composer 安装命令:
composer require jhumayun/shapes
包简介
Library of shapes to calculate their area and perimeter
关键字:
README 文档
README
Library of shapes to calculate their area and perimeter.
Composer install command
composer require jhumayun/shapes
Usage
The shapes class can be loaded in two ways
- Method 1: using namespace of the class
- Method 2: using ShapeFactory object (Recommended)
Loading shape using namespace
<?php
// Require Autoloader
require_once '../vendor/autoload.php';
// Namespaces of the shapes in use
use jhumayun\Shapes\shapes\Circle;
use jhumayun\Shapes\shapes\Square;
try{
$circle_params = array(
'r'=>'10'
);
$Circle = new Circle($circle_params);
echo "<pre>Parameters of Circle: ".print_r($Circle->getParams() ,1)."</pre>";
echo "<pre>Radius = 10</pre>";
echo "<pre>Circle Perimeter: ".print_r($Circle->calculatePerimeter() ,1)."</pre>";
echo "<pre>Circle Area: ".print_r($Circle->calculateArea() ,1)."</pre>";
$Circle->setParam('r',2);
echo "<pre>Radius = 2</pre>";
echo "<pre>Circle Perimeter: ".print_r($Circle->calculatePerimeter() ,1)."</pre>";
echo "<pre>Circle Area: ".print_r($Circle->calculateArea() ,1)."</pre>";
$square_params = array(
's'=>'1'
);
$Square = new Square($square_params);
echo "<pre>Parameters of Square: ".print_r($Square->getParams() ,1)."</pre>";
echo "<pre>Side = 1</pre>";
echo "<pre>Square Perimeter: ".print_r($Square->calculatePerimeter() ,1)."</pre>";
echo "<pre>Square Area: ".print_r($Square->calculateArea() ,1)."</pre>";
}
catch(\Exception $e){
die('Caught exception '.$e->getMessage());
}
?>
Loading Shape using ShapeFactory Class
<?php
require_once '../vendor/autoload.php';
use jhumayun\Shapes\core\ShapesFactory;
try{
$factory = new ShapesFactory();
$circle_params = array(
'r'=>'10'
);
$Circle = $factory::create('Circle'); // instentiating without parameters
echo "<pre>Parameters of Circle: ".print_r($Circle->getParams() ,1)."</pre>";
$Circle = $factory::create('Circle', $circle_params); // instentiating with parameters
echo "<pre>Radius = 10</pre>";
echo "<pre>Circle Perimeter: ".print_r($Circle->calculatePerimeter() ,1)."</pre>";
echo "<pre>Circle Area: ".print_r($Circle->calculateArea() ,1)."</pre>";
$Circle->setParam('r',2);
echo "<pre>Radius = 2</pre>";
echo "<pre>Circle Perimeter: ".print_r($Circle->calculatePerimeter() ,1)."</pre>";
echo "<pre>Circle Area: ".print_r($Circle->calculateArea() ,1)."</pre>";
$square_params = array(
's'=>'1'
);
$Square = $factory::create('Square', $square_params);
echo "<pre>Parameters of Square: ".print_r($Square->getParams() ,1)."</pre>";
echo "<pre>Side = 1</pre>";
echo "<pre>Square Perimeter: ".print_r($Square->calculatePerimeter() ,1)."</pre>";
echo "<pre>Square Area: ".print_r($Square->calculateArea() ,1)."</pre>";
}
catch(\Exception $e){
die('Caught exception '.$e->getMessage());
}
?>
Method Description
| Method | Description | Return Type |
|---|---|---|
| getName | get Name of the shape | string |
| getDimensions | get dimensions of the shape | numeric |
| getParams | get parameter structure of the shape | array |
| setParam | set value of single parameter of the shape | void |
| calculatePerimeter | calculate Perimeter of the shape | numeric |
| calculateArea | calculate Area of the shape | numeric |
Adding a new Shape
Each shape extends ShapesBase Class which is an abstract class, and the ShapesBase implements ShapesTemplate. All the core functions are already defined in ShapesBase Class therefore new shape will need to define only constructor, calculatePerimeter method, calculateArea method.
Lets say, name of your new shape is myShape then you need to add a file myShape.php into 'src\shapes' directory as following.
<?php
namespace jhumayun\Shapes\shapes;
use jhumayun\Shapes\core\ShapesBase;
class myShape extends ShapesBase{
protected $Name = 'myShape';
protected $Dimensions = '2';
protected $valid_params = array(
'p1' => 'where p1 is the perameter 1 of myShape',
'p2' => 'where p1 is the perameter 2 of myShape'
);
public function __construct($params){
parent::__construct($this->Name, $this->Dimensions, $params);
}
public function calculatePerimeter(){
// assume that formula of Perimeter of myShape is p1+p2
$res = floatval($this->Params['p1']['value']) + floatval($this->Params['p2']['value']);
return number_format($res,2,'.','');
}
public function calculateArea(){
// assume that formula of Perimeter of myShape is p1*p2
$res = floatval($this->Params['p1']['value']) * floatval($this->Params['p2']['value']);
return number_format($res,2,'.','');
}
}
jhumayun/shapes 适用场景与选型建议
jhumayun/shapes 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 12 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「example」 「area」 「perimeter」 「shapes」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jhumayun/shapes 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jhumayun/shapes 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jhumayun/shapes 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A very simple Rate Limiter for PHP
Impersonate kirby users in the panel
MetaModels perimeter search
The universal PHP library to convert any values and measures
Perimeter RateLimitBundle
Cache integration for the Memento caching library into Symfony2 and Perimeter API Gateway
统计信息
- 总下载量: 17
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: proprietary
- 更新时间: 2018-12-31