Project: parse time entries
Lesson 43 of 48 in Coddy's PHP for Ruby Developers course.
Challenge
EasyInput [1,row]. Complete Parser::parse(string):?Entry. Accept exactly lowercase ASCII letters, colon and one to three digits, no surrounding whitespace. Return Entry(name,minutes) or null. Leading zeros are accepted. Entry is supplied. Leave later-stage placeholders for now.
The supplied main script reads one JSON value from standard input, checks the named public APIs, and prints the result. Implement the module declarations without printing; do not replace the launcher or read input from a module. Input satisfies the stated types and bounds.
Edit Entry.php, Parser.php, Ledger.php, Reporter.php. main.php is the locked entry file. Dependencies use require_once __DIR__ with a relative filename.
Try it yourself
<?php
declare(strict_types=1);
require_once __DIR__."/Entry.php";
require_once __DIR__."/Parser.php";
require_once __DIR__."/Ledger.php";
require_once __DIR__."/Reporter.php";
function ensure(bool $ok,string $message):void{if(!$ok)throw new RuntimeException("API: ".$message);}
function projectReport(array $input):string{
[$stage,$data]=$input;
if($stage===1){$e=(new Parser())->parse($data);return $e===null?"invalid":$e->name.":".$e->minutes;}
if($stage===2){$l=new Ledger();foreach($data[0] as [$name,$minutes])$l->add(new Entry($name,$minutes));return (string)$l->total($data[1]);}
if($stage===3){$l=new Ledger();foreach($data as [$name,$minutes])$l->add(new Entry($name,$minutes));$out=$l->lines();return $out===[]?"empty":implode("\n",$out);}
return (new Reporter())->report($data);
}
$input=json_decode(trim(stream_get_contents(STDIN)),true,512,JSON_THROW_ON_ERROR);
$stage=$input[0];$parser=new Parser();$e=$parser->parse("tea:007");ensure($e instanceof Entry && $e->name==="tea" && $e->minutes===7 && $parser->parse("tea:-1")===null && $parser->parse(" tea:2")===null,"parser API");
if($stage>=2){$l=new Ledger();$l->add(new Entry("b",4));$l->add(new Entry("b",3));$l->add(new Entry("a",0));ensure($l->total("b")===7 && $l->total("x")===0,"aggregation API");}
if($stage>=3){ensure($l->lines()===["a:0","b:7"] && $l->lines()===["a:0","b:7"],"ordered snapshot");}
if($stage>=4){$r=new Reporter();ensure($r->report(["z:3","bad","z:2"])==="z:5" && $r->report([])==="empty","fresh report state");}
echo projectReport($input),"\n";
All lessons in PHP for Ruby Developers
1Values and output
Modules and first runFormatting returned textAssignments and variablesDivision and conversionStrings and byte lengthsExplicit Boolean rulesRecap: receipt text4Functions and modules
Functions and return typesDefaults and named callsVariadic argumentsCallbacks and capturesrequire_once and scopeRecap: integer cents2Control flow
Branches and precedenceStrict match expressionsLoops and early exitsKeys and iterationRecap: shipping rules5Errors and boundaries
Exceptions and stateStrict input validationNull is not falsyRecap: validate a batch3Collections
Lists are PHP arraysKeys, null and presenceArray copies and referencesFiltering and dense keysOrdering without mutationRecap: word counts6Objects and state
Classes and typed propertiesConstructors and validationMethods and independent stateRecap: bounded stock9Time ledger project
Project: parse time entriesProject: accumulate minutesPractice on your own: Online PHP compiler