Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Geometry\Triangle;
- class Triangle
- {
- /**
- * Cathetus array of values
- * [a, b]
- *
- * @var array
- */
- protected $_cathethus = [0, 0];
- /**
- * Hypothenese value
- * c
- *
- * @var int
- */
- protected $_hypothenese = 0;
- /**
- * Triangle type
- *
- * @var
- */
- private $_triangleType;
- protected $_types = [
- 'rectangular', 'isosceles',
- 'versatile', 'equilateral'
- ];
- /**
- * @var array
- */
- private $_results = [];
- public function __construct()
- {
- $this->setCathethus();
- $this->setHypothenese();
- $this->getTriangleType();
- $this->getPerimeter();
- $this->getSquare();
- }
- /**
- * @param $cathethus
- * @return $this
- * @throws \Exception
- */
- public function setCathethus()
- {
- echo "Enter cathethus : ";
- $cathethus = explode(',', stripslashes(str_replace("\n", '', fgets(STDIN))));
- if (count($cathethus) != 2) {
- throw new \Exception('Cathethus should be limit of 2', 400);
- }
- foreach ($cathethus as $i => $catheth) {
- $this->_cathethus[$i] = (integer)$catheth;
- }
- $this->setResult('cathethus', $this->_cathethus);
- return $this;
- }
- /**
- * @param int $hypothenese
- * @return Triangle
- */
- public function setHypothenese()
- {
- echo "Enter hypothenese : ";
- $this->_hypothenese = (integer)fgets(STDIN);
- $this->setResult('hypothenese', $this->_hypothenese);
- return $this;
- }
- protected function getTriangleType()
- {
- if ($this->_cathethus[0] == $this->_cathethus[1] && $this->_cathethus[0] == $this->_hypothenese) {
- $this->_triangleType = 'Equilateral';
- $this->setResult('type', $this->_triangleType);
- return $this;
- }
- if (pow($this->_cathethus[0], 2) + pow($this->_cathethus[1], 2) == pow($this->_hypothenese, 2)) {
- $this->_triangleType = 'Rectangular';
- $this->setResult('type', $this->_triangleType);
- return $this;
- }
- if ($this->_cathethus[0] == $this->_cathethus[1] && $this->_cathethus[0] != $this->_hypothenese) {
- $this->_triangleType = 'Isosceles';
- $this->setResult('type', $this->_triangleType);
- return $this;
- }
- if ($this->_cathethus[0] != $this->_cathethus[1] && $this->_cathethus[0] != $this->_hypothenese && $this->_cathethus[1] != $this->_hypothenese) {
- $this->_triangleType = 'Versatile';
- $this->setResult('type', $this->_triangleType);
- return $this;
- }
- }
- protected function getSquare()
- {
- $p = $this->_results['perimeter'];
- $this->setResult('square', sqrt($p * ($p - $this->_cathethus[0]) * ($p - $this->_cathethus[1]) * ($p - $this->_hypothenese)));
- return $this;
- }
- protected function getPerimeter()
- {
- $this->setResult('perimeter', ($this->_cathethus[1] + $this->_cathethus[0] + $this->_hypothenese) / 2);
- return $this;
- }
- protected function setResult($key, $value)
- {
- return $this->_results[$key] = $value;
- }
- public function getResults()
- {
- return $this->_results;
- }
- }
- $triangle = new Triangle();
- var_dump($triangle->getResults());
Advertisement
Add Comment
Please, Sign In to add comment