<?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);



$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, $col4) . 
         padLeft($employee->getTotalPayment(), $col5) . "\n";
}

echo str_repeat("_", 64);

