design = $design; $this->input_variables = $input_variables; } /** * Returns the design * formatted HTML * @return string The HTML design built */ public function build() :string { $this->setDesign($this->getSection('header')) ->setDesign($this->getSection('body')) ->setDesign($this->getSection('table')) ->setDesign($this->getSection('footer')); return $this->html; } private function setDesign($section) { $this->html .= $section; return $this; } /** * Returns the template section on with the * stacked variables replaced with single variables. * * @param string $section the method name to be executed ie header/body/table/footer * @return string The HTML of the template section */ public function getSection($section) :string { $this->exportVariables(); return str_replace(array_keys($this->exported_variables), array_values($this->exported_variables), $this->design->{$section}()); } private function exportVariables() { $this->exported_variables['$client_details'] = $this->processVariables($this->input_variables['client_details'], $this->clientDetails()); $this->exported_variables['$company_details'] = $this->processVariables($this->input_variables['company_details'], $this->companyDetails()); $this->exported_variables['$company_address'] = $this->processVariables($this->input_variables['company_address'], $this->companyAddress()); $this->exported_variables['$invoice_details_labels'] = $this->processLabels($this->input_variables['invoice_details'], $this->invoiceDetails()); $this->exported_variables['$invoice_details'] = $this->processVariables($this->input_variables['invoice_details'], $this->invoiceDetails()); return $this; } private function processVariables($input_variables, $variables) :string { $output = ''; foreach($input_variables as $value) $output .= $variables[$value]; return $output; } private function processLabels($input_variables, $variables) :string { $output = ''; foreach($input_variables as $value) { $tmp = str_replace("", "_label", $variables[$value]); $output .= $tmp; } return $output; } // private function exportVariables() // { // /* // * $invoice_details_labels // * $invoice_details // */ // $header = $this->design->header(); // /* // * $company_logo - full URL // * $client_details // */ // $body = $this->design->body(); // /* // * $table_header // * $table_body // * $total_labels // * $total_values // */ // $table = $this->design->table(); // /* // * $company_details // * $company_address // */ // $footer = $this->design->footer(); // } private function clientDetails() { return [ 'name' => '

$client.name

', 'id_number' => '

$client.id_number

', 'vat_number' => '

$client.vat_number

', 'address1' => '

$client.address1

', 'address2' => '

$client.address2

', 'city_state_postal' => '

$client.city_state_postal

', 'postal_city_state' => '

$client.postal_city_state

', 'country' => '

$client.country

', 'email' => '

$client.email

', 'custom_value1' => '

$client.custom_value1

', 'custom_value2' => '

$client.custom_value2

', 'custom_value3' => '

$client.custom_value3

', 'custom_value4' => '

$client.custom_value4

', ]; } private function companyDetails() { return [ 'company_name' => '$company.company_name', 'id_number' => '$company.id_number', 'vat_number' => '$company.vat_number', 'website' => '$company.website', 'email' => '$company.email', 'phone' => '$company.phone', 'custom_value1' => '$company.custom_value1', 'custom_value2' => '$company.custom_value2', 'custom_value3' => '$company.custom_value3', 'custom_value4' => '$company.custom_value4', ]; } private function companyAddress() { return [ 'address1' => '$company.address1', 'address2' => '$company.address1', 'city_state_postal' => '$company.city_state_postal', 'postal_city_state' => '$company.postal_city_state', 'country' => '$company.country', 'custom_value1' => '$company.custom_value1', 'custom_value2' => '$company.custom_value2', 'custom_value3' => '$company.custom_value3', 'custom_value4' => '$company.custom_value4', ]; } private function invoiceDetails() { return [ 'invoice_number' => '$invoice_number', 'po_number' => '$po_number', 'date' => '$date', 'due_date' => '$due_date', 'balance_due' => '$balance_due', 'invoice_total' => '$invoice_total', 'partial_due' => '$partial_due', 'custom_value1' => '$invoice.custom_value1', 'custom_value2' => '$invoice.custom_value2', 'custom_value3' => '$invoice.custom_value3', 'custom_value4' => '$invoice.custom_value4', ]; } }