<?php
 
class Employee 
{
 
    public $name;
    public $rate;
    public $hours = array();
 
    public function __construct($name, $rate)
    {
 
        $this->name = $name;
        $this->rate = $rate;
 
    }
 
    public function getTotalHoursWorked()
    {
 
        return array_sum($this->hours);
 
    }
 
    public function isOverdrafted()
    {
        $totalOverdraft = 0;
        $overdraft = 0;
        foreach($this->hours as $hour) {
          if($hour > 40) {
              $overdraft = $hour - 40;
          } else {
              $overdraft = 0;
          } 
              $totalOverdraft += $overdraft;
        }
        return $totalOverdraft;
    }
 
    public function getTotalPayment()
    {
 
        $hours = $this->getTotalHoursWorked();
        $salary = ($hours - $this->isOverdrafted()) * $this->rate + ($this->isOverdrafted() * ($this->rate *= 2));
        return $salary;
 
    }
    
 
    public function nameTrimmer(){
        $firstLetter = substr($this->name, 0, 1);
        $regexp = '/([a-zA-Z])* /';
        $replacement = $firstLetter; 
        $trimmedName = preg_replace($regexp,  $replacement . ". ", $this->name);
        return $trimmedName;
 
    }
}
 
function padRight($string, $length)
{ 
 
    $string = str_pad ( $string, $length, " " );
    return $string;
 
}
 
function padLeft($string, $length)
{
 
     $string = str_pad ( $string, $length, " ", STR_PAD_LEFT );
     return $string;
 
}
 
$petya = new Employee("Peter Sunberg", 10);
$petya->hours = array(40, 50, 10, 40);
 
$tom = new Employee("Tomas Holloway", 8);
$tom->hours = array(40, 40, 40, 40);
 
$saymon = new Employee("Saymon Smit", 15);
$saymon->hours = array(50, 60, 50, 50);
 
$employees = array($petya, $tom, $saymon);
 
foreach ($employees as $employee) {
    $allRates += $employee->rate; 
    $totalHours += $employee->getTotalHoursWorked();
    $totalOverdraft += $employee->isOverdrafted();
    $totalSalary += $employee->getTotalPayment(); 
} 

$avarageRate = $allRates / count($employees);
$avarageHours = $totalHours / count($employees);
$avarageOverdraft = $totalOverdraft / count($employees);
$avarageSalary = round($totalSalary  / count($employees));

$col1 = 30;
$col2 = 8;
$col3 = 10;
$col4 = 8;
$col5 = 8;
 
 
    echo padRight("Name", $col1) .
         padLeft("Hours", $col2) .
         padLeft("Overdraft", $col3) .
         padLeft("Rate", $col4) . 
         padLeft("Salary", $col5) . "\n";
echo str_repeat("_", 64) . "\n";
 
foreach ($employees as $employee) { 
 
    echo padRight($employee->nameTrimmer(), $col1) .
         padLeft($employee->getTotalHoursWorked(), $col2) .
         padLeft($employee->isOverdrafted(), $col3) .
         padLeft($employee->rate / 2, $col4) . 
         padLeft($employee->getTotalPayment() / 2, $col5) . "\n";      
        
}
 
echo str_repeat("_", 64) . "\n";
 
echo padRight("Total:", $col1) .
         padLeft($totalHours, $col2) .
         padLeft($totalOverdraft, $col3) .
         padLeft(" ", $col4) . 
         padLeft($totalSalary, $col5) . "\n";

echo str_repeat("_", 64) . "\n";

echo padRight("Avarage:", $col1) .
         padLeft($avarageHours, $col2) .
         padLeft($avarageOverdraft, $col3) .
         padLeft($avarageRate, $col4) . 
         padLeft($avarageSalary, $col5) . "\n";



