2018-02-11 11:36:13 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Support\Units;
|
2018-02-21 12:33:09 +08:00
|
|
|
|
2018-02-11 12:04:30 +08:00
|
|
|
use Illuminate\Contracts\Support\Arrayable;
|
2018-02-11 11:36:13 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Time
|
|
|
|
* @package App\Support\Units
|
|
|
|
*/
|
2018-02-11 12:04:30 +08:00
|
|
|
class Time implements Arrayable
|
2018-02-11 11:36:13 +08:00
|
|
|
{
|
|
|
|
public $hours,
|
2018-03-20 09:50:40 +08:00
|
|
|
$minutes;
|
2018-02-11 11:36:13 +08:00
|
|
|
|
2018-02-20 03:33:26 +08:00
|
|
|
/**
|
|
|
|
* @param $minutes
|
|
|
|
* @param $hours
|
|
|
|
* @return static
|
|
|
|
*/
|
|
|
|
public static function init($minutes, $hours)
|
|
|
|
{
|
|
|
|
return new Time($minutes, $hours);
|
|
|
|
}
|
|
|
|
|
2018-02-11 11:36:13 +08:00
|
|
|
/**
|
|
|
|
* Pass just minutes to figure out how many hours
|
|
|
|
* Or both hours and minutes
|
|
|
|
* @param $minutes
|
|
|
|
* @param $hours
|
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
public function __construct($minutes, $hours = null)
|
2018-02-11 11:36:13 +08:00
|
|
|
{
|
2018-02-12 10:38:56 +08:00
|
|
|
$minutes = (int) $minutes;
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
if (!empty($hours)) {
|
2018-02-12 10:38:56 +08:00
|
|
|
$this->hours = (int) $hours;
|
2018-02-11 11:36:13 +08:00
|
|
|
} else {
|
|
|
|
$this->hours = floor($minutes / 60);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->minutes = $minutes % 60;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the total number minutes, adding up the hours
|
|
|
|
* @return float|int
|
|
|
|
*/
|
|
|
|
public function getMinutes()
|
|
|
|
{
|
|
|
|
return ($this->hours * 60) + $this->minutes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Alias to getMinutes()
|
|
|
|
* @alias getMinutes()
|
|
|
|
* @return float|int
|
|
|
|
*/
|
|
|
|
public function asInt()
|
|
|
|
{
|
2018-02-11 12:04:30 +08:00
|
|
|
return $this->getMinutes();
|
2018-02-11 11:36:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a time string
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function __toString()
|
|
|
|
{
|
2018-03-20 09:50:40 +08:00
|
|
|
return $this->hours.'h '.$this->minutes.'m';
|
2018-02-11 11:36:13 +08:00
|
|
|
}
|
2018-02-11 12:04:30 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return float|int
|
|
|
|
*/
|
|
|
|
public function toObject()
|
|
|
|
{
|
|
|
|
return $this->getMinutes();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the instance as an array.
|
|
|
|
*/
|
|
|
|
public function toArray()
|
|
|
|
{
|
|
|
|
return $this->getMinutes();
|
|
|
|
}
|
2018-02-11 11:36:13 +08:00
|
|
|
}
|