Romanovich095

Untitled

Dec 25th, 2016
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.46 KB | None | 0 0
  1. <?php
  2. namespace Geometry\Triangle;
  3.  
  4. class Triangle
  5. {
  6.     /**
  7.      * Cathetus array of values
  8.      * [a, b]
  9.      *
  10.      * @var array
  11.      */
  12.     protected $_cathethus = [0, 0];
  13.  
  14.     /**
  15.      * Hypothenese value
  16.      * c
  17.      *
  18.      * @var int
  19.      */
  20.     protected $_hypothenese = 0;
  21.  
  22.     /**
  23.      * Triangle type
  24.      *
  25.      * @var
  26.      */
  27.     private $_triangleType;
  28.  
  29.     protected $_types = [
  30.         'rectangular', 'isosceles',
  31.         'versatile', 'equilateral'
  32.     ];
  33.  
  34.     /**
  35.      * @var array
  36.      */
  37.     private $_results = [];
  38.  
  39.     public function __construct()
  40.     {
  41.         $this->setCathethus();
  42.         $this->setHypothenese();
  43.  
  44.         $this->getTriangleType();
  45.         $this->getPerimeter();
  46.         $this->getSquare();
  47.     }
  48.  
  49.     /**
  50.      * @param $cathethus
  51.      * @return $this
  52.      * @throws \Exception
  53.      */
  54.     public function setCathethus()
  55.     {
  56.         echo "Enter cathethus : ";
  57.         $cathethus = explode(',', stripslashes(str_replace("\n", '', fgets(STDIN))));
  58.  
  59.         if (count($cathethus) != 2) {
  60.             throw new \Exception('Cathethus should be limit of 2', 400);
  61.         }
  62.  
  63.         foreach ($cathethus as $i => $catheth) {
  64.             $this->_cathethus[$i] = (integer)$catheth;
  65.         }
  66.  
  67.         $this->setResult('cathethus', $this->_cathethus);
  68.         return $this;
  69.     }
  70.  
  71.     /**
  72.      * @param int $hypothenese
  73.      * @return Triangle
  74.      */
  75.     public function setHypothenese()
  76.     {
  77.         echo "Enter hypothenese : ";
  78.         $this->_hypothenese = (integer)fgets(STDIN);
  79.  
  80.         $this->setResult('hypothenese', $this->_hypothenese);
  81.  
  82.         return $this;
  83.     }
  84.  
  85.     protected function getTriangleType()
  86.     {
  87.  
  88.         if ($this->_cathethus[0] == $this->_cathethus[1] && $this->_cathethus[0] == $this->_hypothenese) {
  89.             $this->_triangleType = 'Equilateral';
  90.             $this->setResult('type', $this->_triangleType);
  91.             return $this;
  92.         }
  93.  
  94.         if (pow($this->_cathethus[0], 2) + pow($this->_cathethus[1], 2) == pow($this->_hypothenese, 2)) {
  95.             $this->_triangleType = 'Rectangular';
  96.             $this->setResult('type', $this->_triangleType);
  97.             return $this;
  98.         }
  99.  
  100.         if ($this->_cathethus[0] == $this->_cathethus[1] && $this->_cathethus[0] != $this->_hypothenese) {
  101.             $this->_triangleType = 'Isosceles';
  102.             $this->setResult('type', $this->_triangleType);
  103.             return $this;
  104.         }
  105.  
  106.         if ($this->_cathethus[0] != $this->_cathethus[1] && $this->_cathethus[0] != $this->_hypothenese && $this->_cathethus[1] != $this->_hypothenese) {
  107.             $this->_triangleType = 'Versatile';
  108.             $this->setResult('type', $this->_triangleType);
  109.             return $this;
  110.         }
  111.     }
  112.  
  113.     protected function getSquare()
  114.     {
  115.         $p = $this->_results['perimeter'];
  116.         $this->setResult('square', sqrt($p * ($p - $this->_cathethus[0]) * ($p - $this->_cathethus[1]) * ($p - $this->_hypothenese)));
  117.         return $this;
  118.     }
  119.  
  120.     protected function getPerimeter()
  121.     {
  122.         $this->setResult('perimeter', ($this->_cathethus[1] + $this->_cathethus[0] + $this->_hypothenese) / 2);
  123.         return $this;
  124.     }
  125.  
  126.     protected function setResult($key, $value)
  127.     {
  128.         return $this->_results[$key] = $value;
  129.     }
  130.  
  131.     public function getResults()
  132.     {
  133.         return $this->_results;
  134.     }
  135. }
  136.  
  137. $triangle = new Triangle();
  138. var_dump($triangle->getResults());
Advertisement
Add Comment
Please, Sign In to add comment