PHP code example of makeabledk / php-value-objects

1. Go to this page and download the library: Download makeabledk/php-value-objects library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

makeabledk / php-value-objects example snippets


Duration::create(1,30)->toFormat(); // 01:30:00

Duration::$format = 'h:mm';
Duration::create(1,30)->toFormat(); // 1:30

Duration::create(1,30)->add(Duration::create(1,30))->toFormat(); // 03:00:00
Duration::create(1,30)->subtract(Duration::create(0,20))->toFormat(); // 01:10:00

$events = Events::all();
$eventsTotalDuration = Duration::sum($events, 'duration'); 

$exported = Duration::create(1,30)->toArray(); // ['seconds' => 5400, 'minutes' => 90, 'hours' => 1.5, 'formatted' => '01:30:00']
$imported = Duration::fromArray($exported);

$today = new Period(Carbon::today(), Carbon::tomorrow());
$future = new Period(Carbon::now());
$past = new Period(null, Carbon::now());

$thisWeek = new Period(
    Carbon::today()->previous(Carbon::MONDAY)
    Carbon::today()->next(Carbon::SUNDAY)
);
$thisWeek->earliest(Carbon::today())->getStart(); // carbon of today
$thisWeek->latest(Carbon::tomorrow())->getEnd(); // carbon of tomorrow

$exported = (new Period(Carbon::today(), Carbon::tomorrow()))->toArray(); // ['start' => '2017-06-27 00:00:00', 'end' => '2017-06-28 00:00:00']
$imported = Duration::fromArray($exported);

class OrderStatus extends Whitelist 
{
    const VALUES = ['pending', 'accepted', 'cancelled'];
}

$accepted = new OrderStatus('accepted');
$invalid = new OrderStatus('foobar'); // throws exception

OrderStatus::$exceptionClass = \Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException::class;