<?php
$value = [];
$canvasData = json_decode($patient->canvas_data, true);

$items = [];

if($canvasData && isset($canvasData['dx']) && isset($canvasData['dx']['items']) ){
    $items = $canvasData['dx']['items'];
}

$items = !$items ? [] : $items;
foreach($items as $item):
    $value[] = $item['title'];
endforeach;

$summaryHtml = 'Patient ' . $patient->name_first .' '.$patient->name_last . " is a {$patient->age_in_years} year old {$patient->sex}";
if(count($value)) {
    $summaryHtml .= " presenting with ";
    if(count($value) == 1){
        $summaryHtml .= implode(", ", $value);
    }else{
        $allButLast = array_slice($value, 0, count($value)-1);
        $last =  array_slice($value, count($value)-1, 1)[0];
        $summaryHtml .= implode(", ", $allButLast);
        $summaryHtml .= ' and ' . $last;
    }

}
    
$summaryHtml .= '.';

$contentData = [
    'value' => $summaryHtml
];