|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PhpTwinfield\DomDocuments; |
| 4 | + |
| 5 | +use PhpTwinfield\Project; |
| 6 | + |
| 7 | +class ProjectDocument extends BaseDocument |
| 8 | +{ |
| 9 | + /** |
| 10 | + * The root tag, e.g. |
| 11 | + * |
| 12 | + * @return string |
| 13 | + */ |
| 14 | + protected function getRootTagName(): string |
| 15 | + { |
| 16 | + return 'dimension'; |
| 17 | + } |
| 18 | + |
| 19 | + /** |
| 20 | + * Turns a passed Project class into the required markup for interacting |
| 21 | + * with Twinfield. |
| 22 | + * |
| 23 | + * This method doesn't return anything, instead just adds the transaction |
| 24 | + * to this DOMDocument instance for submission usage. |
| 25 | + * |
| 26 | + * @param Project $project |
| 27 | + */ |
| 28 | + public function addProject(Project $project) |
| 29 | + { |
| 30 | + $office = $project->getOffice(); |
| 31 | + |
| 32 | + // Transaction->status |
| 33 | + $this->rootElement->setAttribute('status', $project->getStatus()); |
| 34 | + |
| 35 | + // Transaction > Office |
| 36 | + if(!is_null($office)) { |
| 37 | + $officeElement = $this->createNodeWithTextContent('office', $office->getCode()); |
| 38 | + $this->rootElement->appendChild($officeElement); |
| 39 | + } |
| 40 | + |
| 41 | + // Transaction > Type |
| 42 | + $typeElement = $this->createNodeWithTextContent('type', 'PRJ'); |
| 43 | + $this->rootElement->appendChild($typeElement); |
| 44 | + |
| 45 | + // Transaction > Code |
| 46 | + if(!is_null($project->getCode())) { |
| 47 | + $codeElement = $this->createNodeWithTextContent('code', $project->getCode()); |
| 48 | + $this->rootElement->appendChild($codeElement); |
| 49 | + } |
| 50 | + |
| 51 | + // Transaction > Name |
| 52 | + $nameElement = $this->createNodeWithTextContent('name', $project->getName()); |
| 53 | + $this->rootElement->appendChild($nameElement); |
| 54 | + |
| 55 | + // Transaction > Short name |
| 56 | + $shortNameElement = $this->createNodeWithTextContent('shortname', $project->getShortName()); |
| 57 | + $this->rootElement->appendChild($shortNameElement); |
| 58 | + |
| 59 | + // Transaction > Projects |
| 60 | + $projectsElement = $this->createElement('projects'); |
| 61 | + |
| 62 | + // Transaction > Projects > Invoice description |
| 63 | + if(!is_null($project->getInvoiceDescription())) { |
| 64 | + $invoiceDescriptionElement = $this->createNodeWithTextContent('invoicedescription', |
| 65 | + $project->getInvoiceDescription()); |
| 66 | + $projectsElement->appendChild($invoiceDescriptionElement); |
| 67 | + } |
| 68 | + |
| 69 | + // Transaction > Projects > authoriser |
| 70 | + // Todo: set the attributes on the authoriser element |
| 71 | + $authoriserElement = $this->createNodeWithTextContent('authoriser', $project->getAuthoriser()); |
| 72 | + $projectsElement->appendChild($authoriserElement); |
| 73 | + |
| 74 | + // Transaction > Projects > customer |
| 75 | + // Todo: set the attributes on the customer element |
| 76 | + $customerElement = $this->createNodeWithTextContent('customer', $project->getCustomer()); |
| 77 | + $projectsElement->appendChild($customerElement); |
| 78 | + |
| 79 | + // Transaction > Projects > billable |
| 80 | + // Todo: set the attributes on the billable element |
| 81 | + $billableElement = $this->createNodeWithTextContent('billable', $project->getBillable()); |
| 82 | + $projectsElement->appendChild($billableElement); |
| 83 | + |
| 84 | + // Transaction > Projects > rate |
| 85 | + // Todo: set the attributes on the rate element |
| 86 | + if(!is_null($project->getRate())) { |
| 87 | + $rateElement = $this->createNodeWithTextContent('rate', $project->getRate()); |
| 88 | + $projectsElement->appendChild($rateElement); |
| 89 | + } |
| 90 | + |
| 91 | + // Transaction > Projects > Quantities |
| 92 | + if(!is_null($project->getQuantities())) { |
| 93 | + $projectsElement->appendChild($this->createQuantityElement($project)); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * @param Project $project |
| 99 | + * @return \DOMElement |
| 100 | + */ |
| 101 | + private function createQuantityElement(Project $project): \DOMElement |
| 102 | + { |
| 103 | + $quantityTags = [ |
| 104 | + 'label', |
| 105 | + 'rate', |
| 106 | + 'billable', |
| 107 | + 'mandatory', |
| 108 | + ]; |
| 109 | + |
| 110 | + $quantitiesElement = $this->createElement('quantities'); |
| 111 | + |
| 112 | + foreach ($project->getQuantities() as $quantity) { |
| 113 | + // Create quantity tags |
| 114 | + foreach ($quantityTags as $tag) { |
| 115 | + // Get value |
| 116 | + $getter = 'get' . ucfirst($tag); |
| 117 | + $value = $quantity->{$getter}(); |
| 118 | + if(is_bool($value)) { |
| 119 | + $value = ($value) ? 'true' : 'false'; |
| 120 | + } |
| 121 | + |
| 122 | + $tagElement = $this->createNodeWithTextContent($tag, $value); |
| 123 | + $quantitiesElement->appendChild($tagElement); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return $quantitiesElement; |
| 128 | + } |
| 129 | + |
| 130 | +} |
0 commit comments