1: <?php
2: namespace NGS\Patterns;
3:
4: use InvalidArgumentException;
5: use NGS\Client\Exception\InvalidRequestException;
6: use NGS\Client\ReportingProxy;
7: use NGS\Patterns\GenericSearch;
8: use NGS\Patterns\Specification;
9: use NGS\Utils;
10:
11: /**
12: * Service for creating documents based on templates and data.
13: * Data can be provided or specification can be sent so data is queried
14: * on the server.
15: * <p>
16: * Byte array is returned from the server which represents docx, xlsx,
17: * text or converted pdf file.
18: * <p>
19: * More info about Templater library can be found at {@link http://templater.info}
20: */
21: class Templater
22: {
23: private $class;
24: private $file;
25:
26: /**
27: * Creates a generic templater
28: *
29: * @param string $file Name of template file located on server
30: * @param string $class Class name of domain object
31: * @throws InvalidArgumentException If invalid arguments provided
32: */
33: public function __construct($file, $class=null)
34: {
35: $this->file = $file;
36:
37: if ($class && is_string($class)) {
38: if (!class_exists($class)) {
39: throw new InvalidArgumentException('Cannot find domain object class "'.$class.'"');
40: }
41: $this->class = $class;
42: } else if($class !== null) {
43: throw new InvalidArgumentException('Class name was not a string');
44: }
45: }
46:
47: /**
48: * Fills template with domain object
49: *
50: * @param string $uri
51: * @return string Binary content of genarated template
52: * @throws InvalidArgumentException If data source was invalid type
53: */
54: public function find($uri)
55: {
56: $proxy = new ReportingProxy();
57: return $proxy->findTemplater($this->file, $this->class, $uri);
58: }
59:
60: /**
61: * Fills template with data returned from specification or generic search
62: *
63: * @param \NGS\Patterns\Specification|\NGS\Patterns\GenericSearch $source
64: * data source
65: * @return string Binary content of genarated template
66: * @throws InvalidArgumentException If data source was invalid type
67: */
68: public function search($source)
69: {
70: $proxy = new ReportingProxy();
71: if ($source instanceof Specification) {
72: return $proxy->searchTemplater(
73: $this->file,
74: $source
75: );
76: }
77: elseif ($source instanceof GenericSearch) {
78: return $proxy->searchTemplaterGeneric(
79: $this->file,
80: $source
81: );
82: }
83: else {
84: throw new InvalidArgumentException('Cannot search templater with invalid typye "'.Utils::getType($source).'"');
85: }
86: }
87: }
88: