@@ -27,7 +27,7 @@ discard block |
||
27 | 27 | class ChainMail |
28 | 28 | { |
29 | 29 | |
30 | - const DEFAULT_CONFIG = __DIR__ . '/../config/defaults.php'; |
|
30 | + const DEFAULT_CONFIG = __DIR__.'/../config/defaults.php'; |
|
31 | 31 | |
32 | 32 | /** |
33 | 33 | * Configuration Settings. |
@@ -57,8 +57,8 @@ discard block |
||
57 | 57 | } |
58 | 58 | |
59 | 59 | $this->config = new Config(array_merge( |
60 | - (array)$defaults, |
|
61 | - (array)$config) |
|
60 | + (array) $defaults, |
|
61 | + (array) $config) |
|
62 | 62 | ); |
63 | 63 | } |
64 | 64 |
@@ -26,114 +26,114 @@ |
||
26 | 26 | class ChainMail |
27 | 27 | { |
28 | 28 | |
29 | - const DEFAULT_CONFIG = __DIR__ . '/../config/defaults.php'; |
|
30 | - |
|
31 | - /** |
|
32 | - * Configuration Settings. |
|
33 | - * |
|
34 | - * @since 1.0.0 |
|
35 | - * |
|
36 | - * @var ConfigInterface |
|
37 | - */ |
|
38 | - protected $config; |
|
39 | - |
|
40 | - /** |
|
41 | - * Instantiate a ChainMail object. |
|
42 | - * |
|
43 | - * @since 1.0.0 |
|
44 | - * |
|
45 | - * @param ConfigInterface|null $config Optional. Configuration settings. |
|
46 | - */ |
|
47 | - public function __construct(ConfigInterface $config = null) |
|
48 | - { |
|
49 | - |
|
50 | - $defaults = ConfigFactory::create(include(self::DEFAULT_CONFIG)); |
|
51 | - |
|
52 | - if ( ! $config) { |
|
53 | - $this->config = $defaults; |
|
54 | - |
|
55 | - return; |
|
56 | - } |
|
57 | - |
|
58 | - $this->config = ConfigFactory::create(array_merge( |
|
59 | - (array)$defaults, |
|
60 | - (array)$config) |
|
61 | - ); |
|
62 | - } |
|
63 | - |
|
64 | - /** |
|
65 | - * Render a specific section. |
|
66 | - * |
|
67 | - * @since 1.0.0 |
|
68 | - * |
|
69 | - * @param string $sectionType Type of section to render. |
|
70 | - * @param array $context The context in which to render the section. |
|
71 | - * |
|
72 | - * @return string Rendered HTML. |
|
73 | - */ |
|
74 | - public static function renderSection($sectionType, array $context) |
|
75 | - { |
|
76 | - /** @var Section $section */ |
|
77 | - $section = $context['sections'][$sectionType]; |
|
78 | - |
|
79 | - return $section->render($context); |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * Get an array of strings representing the sections that are used by the |
|
84 | - * template. |
|
85 | - * |
|
86 | - * @since 1.0.0 |
|
87 | - * |
|
88 | - * @param array $context The context in which to render the section. |
|
89 | - * |
|
90 | - * @return array Array of strings with section types. |
|
91 | - */ |
|
92 | - public static function getUsedSections(array $context) |
|
93 | - { |
|
94 | - /** @var Template $template */ |
|
95 | - $template = $context['template']; |
|
96 | - |
|
97 | - return $template->getUsedSections(); |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * Render all used sections. |
|
102 | - * |
|
103 | - * @since 1.0.0 |
|
104 | - * |
|
105 | - * @param array $context The context in which to render the section. |
|
106 | - * |
|
107 | - * @return string Rendered HTML. |
|
108 | - */ |
|
109 | - public static function renderSections(array $context) |
|
110 | - { |
|
111 | - $output = ''; |
|
112 | - |
|
113 | - foreach (self::getUsedSections($context) as $section) { |
|
114 | - $output .= self::renderSection($section, $context); |
|
115 | - } |
|
116 | - |
|
117 | - return $output; |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * Create a new mail object. |
|
122 | - * |
|
123 | - * @since 1.0.0 |
|
124 | - * |
|
125 | - * @param string $format Optional. Format to use. |
|
126 | - * @param string|Template $template Optional. Template to be used. |
|
127 | - * |
|
128 | - * @return Mail |
|
129 | - */ |
|
130 | - public function createMail($format = 'html', $template = 'BasicTemplate') |
|
131 | - { |
|
132 | - $mail_factory = new Factory($this->config, 'mails'); |
|
133 | - $mail_class = $this->config->getKey('formats')[$format]['mail']; |
|
134 | - $mail = $mail_factory->create($mail_class); |
|
135 | - $mail->setTemplate($template); |
|
136 | - |
|
137 | - return $mail; |
|
138 | - } |
|
29 | + const DEFAULT_CONFIG = __DIR__ . '/../config/defaults.php'; |
|
30 | + |
|
31 | + /** |
|
32 | + * Configuration Settings. |
|
33 | + * |
|
34 | + * @since 1.0.0 |
|
35 | + * |
|
36 | + * @var ConfigInterface |
|
37 | + */ |
|
38 | + protected $config; |
|
39 | + |
|
40 | + /** |
|
41 | + * Instantiate a ChainMail object. |
|
42 | + * |
|
43 | + * @since 1.0.0 |
|
44 | + * |
|
45 | + * @param ConfigInterface|null $config Optional. Configuration settings. |
|
46 | + */ |
|
47 | + public function __construct(ConfigInterface $config = null) |
|
48 | + { |
|
49 | + |
|
50 | + $defaults = ConfigFactory::create(include(self::DEFAULT_CONFIG)); |
|
51 | + |
|
52 | + if ( ! $config) { |
|
53 | + $this->config = $defaults; |
|
54 | + |
|
55 | + return; |
|
56 | + } |
|
57 | + |
|
58 | + $this->config = ConfigFactory::create(array_merge( |
|
59 | + (array)$defaults, |
|
60 | + (array)$config) |
|
61 | + ); |
|
62 | + } |
|
63 | + |
|
64 | + /** |
|
65 | + * Render a specific section. |
|
66 | + * |
|
67 | + * @since 1.0.0 |
|
68 | + * |
|
69 | + * @param string $sectionType Type of section to render. |
|
70 | + * @param array $context The context in which to render the section. |
|
71 | + * |
|
72 | + * @return string Rendered HTML. |
|
73 | + */ |
|
74 | + public static function renderSection($sectionType, array $context) |
|
75 | + { |
|
76 | + /** @var Section $section */ |
|
77 | + $section = $context['sections'][$sectionType]; |
|
78 | + |
|
79 | + return $section->render($context); |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * Get an array of strings representing the sections that are used by the |
|
84 | + * template. |
|
85 | + * |
|
86 | + * @since 1.0.0 |
|
87 | + * |
|
88 | + * @param array $context The context in which to render the section. |
|
89 | + * |
|
90 | + * @return array Array of strings with section types. |
|
91 | + */ |
|
92 | + public static function getUsedSections(array $context) |
|
93 | + { |
|
94 | + /** @var Template $template */ |
|
95 | + $template = $context['template']; |
|
96 | + |
|
97 | + return $template->getUsedSections(); |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * Render all used sections. |
|
102 | + * |
|
103 | + * @since 1.0.0 |
|
104 | + * |
|
105 | + * @param array $context The context in which to render the section. |
|
106 | + * |
|
107 | + * @return string Rendered HTML. |
|
108 | + */ |
|
109 | + public static function renderSections(array $context) |
|
110 | + { |
|
111 | + $output = ''; |
|
112 | + |
|
113 | + foreach (self::getUsedSections($context) as $section) { |
|
114 | + $output .= self::renderSection($section, $context); |
|
115 | + } |
|
116 | + |
|
117 | + return $output; |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * Create a new mail object. |
|
122 | + * |
|
123 | + * @since 1.0.0 |
|
124 | + * |
|
125 | + * @param string $format Optional. Format to use. |
|
126 | + * @param string|Template $template Optional. Template to be used. |
|
127 | + * |
|
128 | + * @return Mail |
|
129 | + */ |
|
130 | + public function createMail($format = 'html', $template = 'BasicTemplate') |
|
131 | + { |
|
132 | + $mail_factory = new Factory($this->config, 'mails'); |
|
133 | + $mail_class = $this->config->getKey('formats')[$format]['mail']; |
|
134 | + $mail = $mail_factory->create($mail_class); |
|
135 | + $mail->setTemplate($template); |
|
136 | + |
|
137 | + return $mail; |
|
138 | + } |
|
139 | 139 | } |
@@ -16,175 +16,175 @@ |
||
16 | 16 | * `ConfigInterface` to the `ChainMail` constructor. |
17 | 17 | */ |
18 | 18 | return [ |
19 | - /* |
|
19 | + /* |
|
20 | 20 | * Root location that contains the view files to be rendered. These are |
21 | 21 | * referenced by the individual sections & templates when choosing a |
22 | 22 | * specific view to be rendered. |
23 | 23 | */ |
24 | - 'view_root_locations' => [ |
|
25 | - 'default' => realpath(__DIR__ . '/../views'), |
|
26 | - ], |
|
24 | + 'view_root_locations' => [ |
|
25 | + 'default' => realpath(__DIR__ . '/../views'), |
|
26 | + ], |
|
27 | 27 | |
28 | - /* |
|
28 | + /* |
|
29 | 29 | * The default template to when none is specified. |
30 | 30 | */ |
31 | - 'default_template' => 'BasicTemplate', |
|
31 | + 'default_template' => 'BasicTemplate', |
|
32 | 32 | |
33 | - /* |
|
33 | + /* |
|
34 | 34 | * The formats in which an email can be rendered. |
35 | 35 | * Each format needs a `Mail` implementation, a |
36 | 36 | * `Validator` implementation, as well as a `Sanitizer` |
37 | 37 | * implementation. |
38 | 38 | */ |
39 | - 'formats' => [ |
|
39 | + 'formats' => [ |
|
40 | 40 | |
41 | - /* |
|
41 | + /* |
|
42 | 42 | * The `html` format should be the default format when using responsive |
43 | 43 | * emails. |
44 | 44 | */ |
45 | - 'html' => [ |
|
46 | - 'mail' => 'HTMLMail', |
|
47 | - 'validator' => 'HTMLValidator', |
|
48 | - 'sanitizer' => 'HTMLSanitizer', |
|
49 | - ], |
|
45 | + 'html' => [ |
|
46 | + 'mail' => 'HTMLMail', |
|
47 | + 'validator' => 'HTMLValidator', |
|
48 | + 'sanitizer' => 'HTMLSanitizer', |
|
49 | + ], |
|
50 | 50 | |
51 | - /* |
|
51 | + /* |
|
52 | 52 | * The `text` format is meant to be used a as a fallback, and can also |
53 | 53 | * be sent as backup content embedded within HTML emails. |
54 | 54 | */ |
55 | - 'text' => [ |
|
56 | - 'mail' => 'TextMail', |
|
57 | - 'validator' => 'TextValidator', |
|
58 | - 'sanitizer' => 'TextSanitizer', |
|
59 | - ], |
|
60 | - ], |
|
55 | + 'text' => [ |
|
56 | + 'mail' => 'TextMail', |
|
57 | + 'validator' => 'TextValidator', |
|
58 | + 'sanitizer' => 'TextSanitizer', |
|
59 | + ], |
|
60 | + ], |
|
61 | 61 | |
62 | - /* |
|
62 | + /* |
|
63 | 63 | * The `Mail` implementations that are provided. |
64 | 64 | */ |
65 | - 'mails' => [ |
|
66 | - 'HTMLMail' => [ |
|
67 | - 'class_name' => 'BrightNucleus\ChainMail\Mail\HTMLMail', |
|
68 | - ], |
|
69 | - 'TextMail' => [ |
|
70 | - 'class_name' => 'BrightNucleus\ChainMail\Mail\TextMail', |
|
71 | - ], |
|
72 | - ], |
|
65 | + 'mails' => [ |
|
66 | + 'HTMLMail' => [ |
|
67 | + 'class_name' => 'BrightNucleus\ChainMail\Mail\HTMLMail', |
|
68 | + ], |
|
69 | + 'TextMail' => [ |
|
70 | + 'class_name' => 'BrightNucleus\ChainMail\Mail\TextMail', |
|
71 | + ], |
|
72 | + ], |
|
73 | 73 | |
74 | - /* |
|
74 | + /* |
|
75 | 75 | * The `Validator` implementations that are provided. |
76 | 76 | */ |
77 | - 'validators' => [ |
|
78 | - 'HTMLValidator' => [ |
|
79 | - 'class_name' => 'BrightNucleus\ChainMail\Validator\HTMLValidator', |
|
80 | - ], |
|
81 | - 'TextValidator' => [ |
|
82 | - 'class_name' => 'BrightNucleus\ChainMail\Validator\TextValidator', |
|
83 | - ], |
|
84 | - ], |
|
77 | + 'validators' => [ |
|
78 | + 'HTMLValidator' => [ |
|
79 | + 'class_name' => 'BrightNucleus\ChainMail\Validator\HTMLValidator', |
|
80 | + ], |
|
81 | + 'TextValidator' => [ |
|
82 | + 'class_name' => 'BrightNucleus\ChainMail\Validator\TextValidator', |
|
83 | + ], |
|
84 | + ], |
|
85 | 85 | |
86 | - /* |
|
86 | + /* |
|
87 | 87 | * The `Sanitizer` implementations that are provided. |
88 | 88 | */ |
89 | - 'sanitizers' => [ |
|
90 | - 'HTMLSanitizer' => [ |
|
91 | - 'class_name' => 'BrightNucleus\ChainMail\Sanitizer\HTMLSanitizer', |
|
92 | - ], |
|
93 | - 'TextSanitizer' => [ |
|
94 | - 'class_name' => 'BrightNucleus\ChainMail\Sanitizer\TextSanitizer', |
|
95 | - ], |
|
96 | - ], |
|
89 | + 'sanitizers' => [ |
|
90 | + 'HTMLSanitizer' => [ |
|
91 | + 'class_name' => 'BrightNucleus\ChainMail\Sanitizer\HTMLSanitizer', |
|
92 | + ], |
|
93 | + 'TextSanitizer' => [ |
|
94 | + 'class_name' => 'BrightNucleus\ChainMail\Sanitizer\TextSanitizer', |
|
95 | + ], |
|
96 | + ], |
|
97 | 97 | |
98 | - /* |
|
98 | + /* |
|
99 | 99 | * ViewBuilder configuration that is passed into the `brightnucleus/view` component. |
100 | 100 | */ |
101 | - 'ViewBuilder' => [ |
|
102 | - 'ViewFinder' => [ |
|
103 | - 'Views' => [ |
|
104 | - 'MailView' => 'BrightNucleus\ChainMail\View\MailView', |
|
105 | - ], |
|
106 | - ], |
|
107 | - ], |
|
101 | + 'ViewBuilder' => [ |
|
102 | + 'ViewFinder' => [ |
|
103 | + 'Views' => [ |
|
104 | + 'MailView' => 'BrightNucleus\ChainMail\View\MailView', |
|
105 | + ], |
|
106 | + ], |
|
107 | + ], |
|
108 | 108 | |
109 | - /* |
|
109 | + /* |
|
110 | 110 | * The `Template` implementations that are provided. |
111 | 111 | * |
112 | 112 | * Each template also defines what sections it intends to use, and what |
113 | 113 | * view it intends to use for rendering. |
114 | 114 | */ |
115 | - 'templates' => [ |
|
116 | - 'BasicTemplate' => [ |
|
117 | - 'class_name' => 'BrightNucleus\ChainMail\Template\GenericTemplate', |
|
118 | - 'sections' => [ |
|
119 | - 'HeaderSection', |
|
120 | - 'BodySection', |
|
121 | - 'FooterSection', |
|
122 | - ], |
|
123 | - 'view_name' => 'GenericTemplate', |
|
124 | - ], |
|
125 | - 'HeroTemplate' => [ |
|
126 | - 'class_name' => 'BrightNucleus\ChainMail\Template\GenericTemplate', |
|
127 | - 'sections' => [ |
|
128 | - 'HeaderSection', |
|
129 | - 'HeroSection', |
|
130 | - 'BodySection', |
|
131 | - 'FooterSection', |
|
132 | - ], |
|
133 | - 'view_name' => 'GenericTemplate', |
|
134 | - ], |
|
135 | - 'SidebarTemplate' => [ |
|
136 | - 'class_name' => 'BrightNucleus\ChainMail\Template\GenericTemplate', |
|
137 | - 'sections' => [ |
|
138 | - 'HeaderSection', |
|
139 | - 'BodySection', |
|
140 | - 'SidebarSection', |
|
141 | - 'FooterSection', |
|
142 | - ], |
|
143 | - 'view_name' => 'GenericTemplate', |
|
144 | - ], |
|
145 | - 'HeroSidebarTemplate' => [ |
|
146 | - 'class_name' => 'BrightNucleus\ChainMail\Template\GenericTemplate', |
|
147 | - 'sections' => [ |
|
148 | - 'HeaderSection', |
|
149 | - 'HeroSection', |
|
150 | - 'BodySection', |
|
151 | - 'SidebarSection', |
|
152 | - 'FooterSection', |
|
153 | - ], |
|
154 | - 'view_name' => 'GenericTemplate', |
|
155 | - ], |
|
156 | - ], |
|
115 | + 'templates' => [ |
|
116 | + 'BasicTemplate' => [ |
|
117 | + 'class_name' => 'BrightNucleus\ChainMail\Template\GenericTemplate', |
|
118 | + 'sections' => [ |
|
119 | + 'HeaderSection', |
|
120 | + 'BodySection', |
|
121 | + 'FooterSection', |
|
122 | + ], |
|
123 | + 'view_name' => 'GenericTemplate', |
|
124 | + ], |
|
125 | + 'HeroTemplate' => [ |
|
126 | + 'class_name' => 'BrightNucleus\ChainMail\Template\GenericTemplate', |
|
127 | + 'sections' => [ |
|
128 | + 'HeaderSection', |
|
129 | + 'HeroSection', |
|
130 | + 'BodySection', |
|
131 | + 'FooterSection', |
|
132 | + ], |
|
133 | + 'view_name' => 'GenericTemplate', |
|
134 | + ], |
|
135 | + 'SidebarTemplate' => [ |
|
136 | + 'class_name' => 'BrightNucleus\ChainMail\Template\GenericTemplate', |
|
137 | + 'sections' => [ |
|
138 | + 'HeaderSection', |
|
139 | + 'BodySection', |
|
140 | + 'SidebarSection', |
|
141 | + 'FooterSection', |
|
142 | + ], |
|
143 | + 'view_name' => 'GenericTemplate', |
|
144 | + ], |
|
145 | + 'HeroSidebarTemplate' => [ |
|
146 | + 'class_name' => 'BrightNucleus\ChainMail\Template\GenericTemplate', |
|
147 | + 'sections' => [ |
|
148 | + 'HeaderSection', |
|
149 | + 'HeroSection', |
|
150 | + 'BodySection', |
|
151 | + 'SidebarSection', |
|
152 | + 'FooterSection', |
|
153 | + ], |
|
154 | + 'view_name' => 'GenericTemplate', |
|
155 | + ], |
|
156 | + ], |
|
157 | 157 | |
158 | - /* |
|
158 | + /* |
|
159 | 159 | * The `Section` implementations that are provided. |
160 | 160 | * |
161 | 161 | * Each section also defines what view it intends to use for rendering. |
162 | 162 | */ |
163 | - 'sections' => [ |
|
164 | - 'HeaderSection' => [ |
|
165 | - 'css_class' => 'header', |
|
166 | - 'class_name' => 'BrightNucleus\ChainMail\Section\GenericSection', |
|
167 | - 'view_name' => 'GenericSection', |
|
168 | - ], |
|
169 | - 'BodySection' => [ |
|
170 | - 'css_class' => 'body', |
|
171 | - 'class_name' => 'BrightNucleus\ChainMail\Section\GenericSection', |
|
172 | - 'view_name' => 'GenericSection', |
|
173 | - ], |
|
174 | - 'FooterSection' => [ |
|
175 | - 'css_class' => 'footer', |
|
176 | - 'class_name' => 'BrightNucleus\ChainMail\Section\GenericSection', |
|
177 | - 'view_name' => 'GenericSection', |
|
178 | - ], |
|
179 | - 'HeroSection' => [ |
|
180 | - 'css_class' => 'hero', |
|
181 | - 'class_name' => 'BrightNucleus\ChainMail\Section\GenericSection', |
|
182 | - 'view_name' => 'GenericSection', |
|
183 | - ], |
|
184 | - 'SidebarSection' => [ |
|
185 | - 'css_class' => 'sidebar', |
|
186 | - 'class_name' => 'BrightNucleus\ChainMail\Section\GenericSection', |
|
187 | - 'view_name' => 'GenericSection', |
|
188 | - ], |
|
189 | - ], |
|
163 | + 'sections' => [ |
|
164 | + 'HeaderSection' => [ |
|
165 | + 'css_class' => 'header', |
|
166 | + 'class_name' => 'BrightNucleus\ChainMail\Section\GenericSection', |
|
167 | + 'view_name' => 'GenericSection', |
|
168 | + ], |
|
169 | + 'BodySection' => [ |
|
170 | + 'css_class' => 'body', |
|
171 | + 'class_name' => 'BrightNucleus\ChainMail\Section\GenericSection', |
|
172 | + 'view_name' => 'GenericSection', |
|
173 | + ], |
|
174 | + 'FooterSection' => [ |
|
175 | + 'css_class' => 'footer', |
|
176 | + 'class_name' => 'BrightNucleus\ChainMail\Section\GenericSection', |
|
177 | + 'view_name' => 'GenericSection', |
|
178 | + ], |
|
179 | + 'HeroSection' => [ |
|
180 | + 'css_class' => 'hero', |
|
181 | + 'class_name' => 'BrightNucleus\ChainMail\Section\GenericSection', |
|
182 | + 'view_name' => 'GenericSection', |
|
183 | + ], |
|
184 | + 'SidebarSection' => [ |
|
185 | + 'css_class' => 'sidebar', |
|
186 | + 'class_name' => 'BrightNucleus\ChainMail\Section\GenericSection', |
|
187 | + 'view_name' => 'GenericSection', |
|
188 | + ], |
|
189 | + ], |
|
190 | 190 | ]; |
@@ -22,7 +22,7 @@ |
||
22 | 22 | * specific view to be rendered. |
23 | 23 | */ |
24 | 24 | 'view_root_locations' => [ |
25 | - 'default' => realpath(__DIR__ . '/../views'), |
|
25 | + 'default' => realpath(__DIR__.'/../views'), |
|
26 | 26 | ], |
27 | 27 | |
28 | 28 | /* |
@@ -24,36 +24,36 @@ |
||
24 | 24 | interface Mail extends Renderable |
25 | 25 | { |
26 | 26 | |
27 | - /** |
|
28 | - * Set the template that the email will use. |
|
29 | - * |
|
30 | - * @since 1.0.0 |
|
31 | - * |
|
32 | - * @param string|Template $template Template to use. |
|
33 | - * |
|
34 | - * @return void |
|
35 | - */ |
|
36 | - public function setTemplate($template); |
|
27 | + /** |
|
28 | + * Set the template that the email will use. |
|
29 | + * |
|
30 | + * @since 1.0.0 |
|
31 | + * |
|
32 | + * @param string|Template $template Template to use. |
|
33 | + * |
|
34 | + * @return void |
|
35 | + */ |
|
36 | + public function setTemplate($template); |
|
37 | 37 | |
38 | - /** |
|
39 | - * Get the instance of the template. |
|
40 | - * |
|
41 | - * @since 1.0.0 |
|
42 | - * |
|
43 | - * @return Template |
|
44 | - */ |
|
45 | - public function getTemplate(); |
|
38 | + /** |
|
39 | + * Get the instance of the template. |
|
40 | + * |
|
41 | + * @since 1.0.0 |
|
42 | + * |
|
43 | + * @return Template |
|
44 | + */ |
|
45 | + public function getTemplate(); |
|
46 | 46 | |
47 | - /** |
|
48 | - * Add a section to the Mail. |
|
49 | - * |
|
50 | - * @since 1.0.0 |
|
51 | - * |
|
52 | - * @param string $type Type of section to add. |
|
53 | - * @param string $content Content of the section. |
|
54 | - * |
|
55 | - * @return void |
|
56 | - * @throws RuntimeException |
|
57 | - */ |
|
58 | - public function addSection($type, $content); |
|
47 | + /** |
|
48 | + * Add a section to the Mail. |
|
49 | + * |
|
50 | + * @since 1.0.0 |
|
51 | + * |
|
52 | + * @param string $type Type of section to add. |
|
53 | + * @param string $content Content of the section. |
|
54 | + * |
|
55 | + * @return void |
|
56 | + * @throws RuntimeException |
|
57 | + */ |
|
58 | + public function addSection($type, $content); |
|
59 | 59 | } |
@@ -28,145 +28,145 @@ |
||
28 | 28 | abstract class AbstractSection implements Section |
29 | 29 | { |
30 | 30 | |
31 | - /** |
|
32 | - * Name of the Section. |
|
33 | - * |
|
34 | - * @since 1.0.0 |
|
35 | - * |
|
36 | - * @var string |
|
37 | - */ |
|
38 | - protected $sectionName; |
|
39 | - |
|
40 | - /** |
|
41 | - * Configuration Settings. |
|
42 | - * |
|
43 | - * @since 1.0.0 |
|
44 | - * |
|
45 | - * @var ConfigInterface |
|
46 | - */ |
|
47 | - protected $config; |
|
48 | - |
|
49 | - /** |
|
50 | - * Content of the section. |
|
51 | - * |
|
52 | - * @since 1.0.0 |
|
53 | - * |
|
54 | - * @var string |
|
55 | - */ |
|
56 | - protected $content; |
|
57 | - |
|
58 | - /** |
|
59 | - * ViewBuilder to create template and section views. |
|
60 | - * |
|
61 | - * @since 1.0.0 |
|
62 | - * |
|
63 | - * @var ViewBuilder |
|
64 | - */ |
|
65 | - protected $viewBuilder; |
|
66 | - |
|
67 | - /** |
|
68 | - * Instantiate a AbstractSection object. |
|
69 | - * |
|
70 | - * @since 1.0.0 |
|
71 | - * |
|
72 | - * @param ConfigInterface $config Configuration settings. |
|
73 | - * @param array $arguments Arguments that are passed through the constructor. |
|
74 | - * Contained elements: |
|
75 | - * string $section, string $content |
|
76 | - * |
|
77 | - * @throws RuntimeException |
|
78 | - */ |
|
79 | - public function __construct($config, array $arguments) |
|
80 | - { |
|
81 | - $this->config = $config; |
|
82 | - list($section, $content, $this->viewBuilder) = $arguments; |
|
83 | - $this->setSectionName($section); |
|
84 | - $this->content = $content; |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * Get the name of the Section. |
|
89 | - * |
|
90 | - * @since 1.0.0 |
|
91 | - * |
|
92 | - * @return string Name of the section. |
|
93 | - */ |
|
94 | - public function getSectionName() |
|
95 | - { |
|
96 | - return $this->sectionName; |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * Set the name of the Section. |
|
101 | - * |
|
102 | - * @since 1.0.0 |
|
103 | - * |
|
104 | - * @param string|null $section Optional. Name of the section. |
|
105 | - * |
|
106 | - * @throws FailedToInitialiseSection If no section name was passed. |
|
107 | - * @throws FailedToInitialiseSection If an unknown section name was passed. |
|
108 | - */ |
|
109 | - protected function setSectionName($section = null) |
|
110 | - { |
|
111 | - if (null === $section) { |
|
112 | - throw new FailedToInitialiseSection( |
|
113 | - 'Initialised section without passing it a section name.' |
|
114 | - ); |
|
115 | - } |
|
116 | - if ( ! array_key_exists($section, $this->config['sections'])) { |
|
117 | - throw new FailedToInitialiseSection( |
|
118 | - 'Initialised section with an unknown section name.' |
|
119 | - ); |
|
120 | - } |
|
121 | - $this->sectionName = $section; |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * Render the current Renderable for a given context. |
|
126 | - * |
|
127 | - * @since 1.0.0 |
|
128 | - * |
|
129 | - * @param array $context The context in which to render the Renderable. |
|
130 | - * |
|
131 | - * @return string Rendered output of the Renderable. |
|
132 | - * @throws RuntimeException |
|
133 | - */ |
|
134 | - public function render(array $context) |
|
135 | - { |
|
136 | - |
|
137 | - $viewName = $this->getViewName($context); |
|
138 | - $view = $this->viewBuilder->create($viewName); |
|
139 | - |
|
140 | - $context['css_class'] = $this->getCSSClass(); |
|
141 | - $context['content'] = $this->content; |
|
142 | - |
|
143 | - return $view->render($context); |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * Get the name of the View to use for rendering. |
|
148 | - * |
|
149 | - * @since 1.0.0 |
|
150 | - * |
|
151 | - * @param array $context The context in which to render the template. |
|
152 | - * |
|
153 | - * @return string Name of the view. |
|
154 | - */ |
|
155 | - protected function getViewName(array $context) |
|
156 | - { |
|
157 | - return $this->config['sections'][$this->getSectionName()]['view_name'] |
|
158 | - . '.' . $context['format']; |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * Get the CSS class that is used for the section. |
|
163 | - * |
|
164 | - * @since 1.0.0 |
|
165 | - * |
|
166 | - * @return string |
|
167 | - */ |
|
168 | - protected function getCSSClass() |
|
169 | - { |
|
170 | - return $this->config['sections'][$this->getSectionName()]['css_class']; |
|
171 | - } |
|
31 | + /** |
|
32 | + * Name of the Section. |
|
33 | + * |
|
34 | + * @since 1.0.0 |
|
35 | + * |
|
36 | + * @var string |
|
37 | + */ |
|
38 | + protected $sectionName; |
|
39 | + |
|
40 | + /** |
|
41 | + * Configuration Settings. |
|
42 | + * |
|
43 | + * @since 1.0.0 |
|
44 | + * |
|
45 | + * @var ConfigInterface |
|
46 | + */ |
|
47 | + protected $config; |
|
48 | + |
|
49 | + /** |
|
50 | + * Content of the section. |
|
51 | + * |
|
52 | + * @since 1.0.0 |
|
53 | + * |
|
54 | + * @var string |
|
55 | + */ |
|
56 | + protected $content; |
|
57 | + |
|
58 | + /** |
|
59 | + * ViewBuilder to create template and section views. |
|
60 | + * |
|
61 | + * @since 1.0.0 |
|
62 | + * |
|
63 | + * @var ViewBuilder |
|
64 | + */ |
|
65 | + protected $viewBuilder; |
|
66 | + |
|
67 | + /** |
|
68 | + * Instantiate a AbstractSection object. |
|
69 | + * |
|
70 | + * @since 1.0.0 |
|
71 | + * |
|
72 | + * @param ConfigInterface $config Configuration settings. |
|
73 | + * @param array $arguments Arguments that are passed through the constructor. |
|
74 | + * Contained elements: |
|
75 | + * string $section, string $content |
|
76 | + * |
|
77 | + * @throws RuntimeException |
|
78 | + */ |
|
79 | + public function __construct($config, array $arguments) |
|
80 | + { |
|
81 | + $this->config = $config; |
|
82 | + list($section, $content, $this->viewBuilder) = $arguments; |
|
83 | + $this->setSectionName($section); |
|
84 | + $this->content = $content; |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * Get the name of the Section. |
|
89 | + * |
|
90 | + * @since 1.0.0 |
|
91 | + * |
|
92 | + * @return string Name of the section. |
|
93 | + */ |
|
94 | + public function getSectionName() |
|
95 | + { |
|
96 | + return $this->sectionName; |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * Set the name of the Section. |
|
101 | + * |
|
102 | + * @since 1.0.0 |
|
103 | + * |
|
104 | + * @param string|null $section Optional. Name of the section. |
|
105 | + * |
|
106 | + * @throws FailedToInitialiseSection If no section name was passed. |
|
107 | + * @throws FailedToInitialiseSection If an unknown section name was passed. |
|
108 | + */ |
|
109 | + protected function setSectionName($section = null) |
|
110 | + { |
|
111 | + if (null === $section) { |
|
112 | + throw new FailedToInitialiseSection( |
|
113 | + 'Initialised section without passing it a section name.' |
|
114 | + ); |
|
115 | + } |
|
116 | + if ( ! array_key_exists($section, $this->config['sections'])) { |
|
117 | + throw new FailedToInitialiseSection( |
|
118 | + 'Initialised section with an unknown section name.' |
|
119 | + ); |
|
120 | + } |
|
121 | + $this->sectionName = $section; |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * Render the current Renderable for a given context. |
|
126 | + * |
|
127 | + * @since 1.0.0 |
|
128 | + * |
|
129 | + * @param array $context The context in which to render the Renderable. |
|
130 | + * |
|
131 | + * @return string Rendered output of the Renderable. |
|
132 | + * @throws RuntimeException |
|
133 | + */ |
|
134 | + public function render(array $context) |
|
135 | + { |
|
136 | + |
|
137 | + $viewName = $this->getViewName($context); |
|
138 | + $view = $this->viewBuilder->create($viewName); |
|
139 | + |
|
140 | + $context['css_class'] = $this->getCSSClass(); |
|
141 | + $context['content'] = $this->content; |
|
142 | + |
|
143 | + return $view->render($context); |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * Get the name of the View to use for rendering. |
|
148 | + * |
|
149 | + * @since 1.0.0 |
|
150 | + * |
|
151 | + * @param array $context The context in which to render the template. |
|
152 | + * |
|
153 | + * @return string Name of the view. |
|
154 | + */ |
|
155 | + protected function getViewName(array $context) |
|
156 | + { |
|
157 | + return $this->config['sections'][$this->getSectionName()]['view_name'] |
|
158 | + . '.' . $context['format']; |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * Get the CSS class that is used for the section. |
|
163 | + * |
|
164 | + * @since 1.0.0 |
|
165 | + * |
|
166 | + * @return string |
|
167 | + */ |
|
168 | + protected function getCSSClass() |
|
169 | + { |
|
170 | + return $this->config['sections'][$this->getSectionName()]['css_class']; |
|
171 | + } |
|
172 | 172 | } |
@@ -155,7 +155,7 @@ |
||
155 | 155 | protected function getViewName(array $context) |
156 | 156 | { |
157 | 157 | return $this->config['sections'][$this->getSectionName()]['view_name'] |
158 | - . '.' . $context['format']; |
|
158 | + . '.'.$context['format']; |
|
159 | 159 | } |
160 | 160 | |
161 | 161 | /** |
@@ -34,258 +34,258 @@ |
||
34 | 34 | abstract class AbstractMail implements Mail |
35 | 35 | { |
36 | 36 | |
37 | - /** |
|
38 | - * Configuration Settings. |
|
39 | - * |
|
40 | - * @since 1.0.0 |
|
41 | - * |
|
42 | - * @var ConfigInterface |
|
43 | - */ |
|
44 | - protected $config; |
|
45 | - |
|
46 | - /** |
|
47 | - * Template that is used for the email. |
|
48 | - * |
|
49 | - * @since 1.0.0 |
|
50 | - * |
|
51 | - * @var Template |
|
52 | - */ |
|
53 | - protected $template; |
|
54 | - |
|
55 | - /** |
|
56 | - * Content for the different sections. |
|
57 | - * |
|
58 | - * @since 1.0.0 |
|
59 | - * |
|
60 | - * @var array |
|
61 | - */ |
|
62 | - protected $sectionContent = []; |
|
63 | - |
|
64 | - /** |
|
65 | - * Format of the mail. |
|
66 | - * |
|
67 | - * @since 1.0.0 |
|
68 | - * |
|
69 | - * @var string |
|
70 | - */ |
|
71 | - protected $format; |
|
72 | - |
|
73 | - /** |
|
74 | - * ViewBuilder to create template and section views. |
|
75 | - * |
|
76 | - * @since 1.0.0 |
|
77 | - * |
|
78 | - * @var ViewBuilder |
|
79 | - */ |
|
80 | - protected $viewBuilder; |
|
81 | - |
|
82 | - /** |
|
83 | - * Instantiate an AbstractMail object. |
|
84 | - * |
|
85 | - * @since 1.0.0 |
|
86 | - * |
|
87 | - * @param ConfigInterface $config The Config to use. |
|
88 | - * |
|
89 | - * @throws FailedToProcessConfigException If the Config could not be processed. |
|
90 | - */ |
|
91 | - public function __construct(ConfigInterface $config) |
|
92 | - { |
|
93 | - $this->config = $config; |
|
94 | - $this->setFormat(); |
|
95 | - |
|
96 | - $this->viewBuilder = new ViewBuilder($config |
|
97 | - ? $config->getSubConfig('ViewBuilder') |
|
98 | - : View::getDefaultConfig() |
|
99 | - ); |
|
100 | - |
|
101 | - foreach ($this->config->getKey('view_root_locations') as $folder) { |
|
102 | - $this->viewBuilder->addLocation( |
|
103 | - new FilesystemLocation($folder) |
|
104 | - ); |
|
105 | - } |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * Get the template to use for the renderer. |
|
110 | - * |
|
111 | - * @since 1.0.0 |
|
112 | - * |
|
113 | - * @return Template Reference to the template that is used. |
|
114 | - * @throws RuntimeException |
|
115 | - */ |
|
116 | - public function getTemplate() |
|
117 | - { |
|
118 | - |
|
119 | - if ( ! $this->template) { |
|
120 | - $this->setDefaultTemplate(); |
|
121 | - } |
|
122 | - |
|
123 | - if (is_string($this->template)) { |
|
124 | - $this->template = $this->createTemplate($this->template); |
|
125 | - } |
|
126 | - |
|
127 | - return $this->template; |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * Set the template to use for the renderer. |
|
132 | - * |
|
133 | - * @since 1.0.0 |
|
134 | - * |
|
135 | - * @param string|Template $template Template to use for the |
|
136 | - * renderer. |
|
137 | - * |
|
138 | - * @throws InvalidTemplate If the template class could not be instantiated. |
|
139 | - * @throws InvalidTemplate If the template type is not recognized. |
|
140 | - */ |
|
141 | - public function setTemplate($template) |
|
142 | - { |
|
143 | - try { |
|
144 | - if (is_string($template)) { |
|
145 | - $template = $this->createTemplate($template); |
|
146 | - } |
|
147 | - } catch (Exception $exception) { |
|
148 | - throw new InvalidTemplate( |
|
149 | - 'Could not instantiate the template class "%1$s". Reason: "%2$s".', |
|
150 | - $template, |
|
151 | - $exception->getMessage() |
|
152 | - ); |
|
153 | - } |
|
154 | - |
|
155 | - if ( ! $template instanceof Template) { |
|
156 | - throw new InvalidTemplate( |
|
157 | - 'Could not set the template, invalid type.', |
|
158 | - (array)$template |
|
159 | - ); |
|
160 | - } |
|
161 | - $this->template = $template; |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * Add a section to the Mail. |
|
166 | - * |
|
167 | - * @since 1.0.0 |
|
168 | - * |
|
169 | - * @param string $type Type of section to add. |
|
170 | - * @param string $content Content of the section. |
|
171 | - * |
|
172 | - * @throws RuntimeException |
|
173 | - */ |
|
174 | - public function addSection($type, $content) |
|
175 | - { |
|
176 | - $this->sectionContent[$type] = $content; |
|
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * Render the Mail for a given context. |
|
181 | - * |
|
182 | - * @since 1.0.0 |
|
183 | - * |
|
184 | - * @param array $context The context in which to render the email. |
|
185 | - * |
|
186 | - * @return string Rendered output of the email |
|
187 | - */ |
|
188 | - public function render(array $context) |
|
189 | - { |
|
190 | - $template = $this->getTemplate(); |
|
191 | - |
|
192 | - $context['template'] = $template; |
|
193 | - |
|
194 | - $this->instantiateSections($template->getUsedSections(), $context); |
|
195 | - |
|
196 | - $context['format'] = $this->getFormat(); |
|
197 | - |
|
198 | - $context = $this->setContext($context); |
|
199 | - |
|
200 | - return $template->render($context); |
|
201 | - } |
|
202 | - |
|
203 | - /** |
|
204 | - * Instantiate the requested sections for a template. |
|
205 | - * |
|
206 | - * @since 1.0.0 |
|
207 | - * |
|
208 | - * @param array $sections Sections to instantiate. |
|
209 | - * @param array $context The context in which to instantiate the sections. |
|
210 | - */ |
|
211 | - protected function instantiateSections(array $sections, array &$context) |
|
212 | - { |
|
213 | - $sectionFactory = new Factory($this->config, 'sections'); |
|
214 | - |
|
215 | - foreach ($sections as $section) { |
|
216 | - |
|
217 | - $content = null; |
|
218 | - |
|
219 | - if (array_key_exists($section, $this->sectionContent)) { |
|
220 | - $content = $this->sectionContent[$section]; |
|
221 | - } |
|
222 | - |
|
223 | - $context['sections'][$section] = $sectionFactory->create( |
|
224 | - $section, |
|
225 | - [$section, $content, $this->viewBuilder] |
|
226 | - ); |
|
227 | - } |
|
228 | - } |
|
229 | - |
|
230 | - /** |
|
231 | - * Set the format of the mail. |
|
232 | - * |
|
233 | - * @since 1.0.0 |
|
234 | - * |
|
235 | - * @return string Format of the Mail. |
|
236 | - */ |
|
237 | - protected function getFormat() |
|
238 | - { |
|
239 | - return $this->format; |
|
240 | - } |
|
241 | - |
|
242 | - /** |
|
243 | - * Set the format of the mail. |
|
244 | - * |
|
245 | - * @since 1.0.0 |
|
246 | - * |
|
247 | - * @return void |
|
248 | - */ |
|
249 | - abstract protected function setFormat(); |
|
250 | - |
|
251 | - /** |
|
252 | - * Set the template to the default template defined in the configuration. |
|
253 | - * |
|
254 | - * @since 1.0.0 |
|
255 | - * |
|
256 | - * @throws RuntimeException |
|
257 | - */ |
|
258 | - protected function setDefaultTemplate() |
|
259 | - { |
|
260 | - $defaultTemplate = $this->config->getKey('default_template'); |
|
261 | - $this->setTemplate($defaultTemplate); |
|
262 | - } |
|
263 | - |
|
264 | - /** |
|
265 | - * Create an instance of a template. |
|
266 | - * |
|
267 | - * @since 1.0.0 |
|
268 | - * |
|
269 | - * @param string $template Template to instantiate. |
|
270 | - * |
|
271 | - * @return Template $template Newly created instance. |
|
272 | - * @throws RuntimeException |
|
273 | - */ |
|
274 | - protected function createTemplate($template) |
|
275 | - { |
|
276 | - $templateFactory = new Factory($this->config, 'templates'); |
|
277 | - |
|
278 | - return $templateFactory->create($template, [$template, $this->viewBuilder]); |
|
279 | - } |
|
280 | - |
|
281 | - /** |
|
282 | - * Set the context of the mail. |
|
283 | - * |
|
284 | - * @since 1.0.0 |
|
285 | - * |
|
286 | - * @param array $context Context to set/modify. |
|
287 | - * |
|
288 | - * @return array Updated context. |
|
289 | - */ |
|
290 | - abstract protected function setContext(array $context); |
|
37 | + /** |
|
38 | + * Configuration Settings. |
|
39 | + * |
|
40 | + * @since 1.0.0 |
|
41 | + * |
|
42 | + * @var ConfigInterface |
|
43 | + */ |
|
44 | + protected $config; |
|
45 | + |
|
46 | + /** |
|
47 | + * Template that is used for the email. |
|
48 | + * |
|
49 | + * @since 1.0.0 |
|
50 | + * |
|
51 | + * @var Template |
|
52 | + */ |
|
53 | + protected $template; |
|
54 | + |
|
55 | + /** |
|
56 | + * Content for the different sections. |
|
57 | + * |
|
58 | + * @since 1.0.0 |
|
59 | + * |
|
60 | + * @var array |
|
61 | + */ |
|
62 | + protected $sectionContent = []; |
|
63 | + |
|
64 | + /** |
|
65 | + * Format of the mail. |
|
66 | + * |
|
67 | + * @since 1.0.0 |
|
68 | + * |
|
69 | + * @var string |
|
70 | + */ |
|
71 | + protected $format; |
|
72 | + |
|
73 | + /** |
|
74 | + * ViewBuilder to create template and section views. |
|
75 | + * |
|
76 | + * @since 1.0.0 |
|
77 | + * |
|
78 | + * @var ViewBuilder |
|
79 | + */ |
|
80 | + protected $viewBuilder; |
|
81 | + |
|
82 | + /** |
|
83 | + * Instantiate an AbstractMail object. |
|
84 | + * |
|
85 | + * @since 1.0.0 |
|
86 | + * |
|
87 | + * @param ConfigInterface $config The Config to use. |
|
88 | + * |
|
89 | + * @throws FailedToProcessConfigException If the Config could not be processed. |
|
90 | + */ |
|
91 | + public function __construct(ConfigInterface $config) |
|
92 | + { |
|
93 | + $this->config = $config; |
|
94 | + $this->setFormat(); |
|
95 | + |
|
96 | + $this->viewBuilder = new ViewBuilder($config |
|
97 | + ? $config->getSubConfig('ViewBuilder') |
|
98 | + : View::getDefaultConfig() |
|
99 | + ); |
|
100 | + |
|
101 | + foreach ($this->config->getKey('view_root_locations') as $folder) { |
|
102 | + $this->viewBuilder->addLocation( |
|
103 | + new FilesystemLocation($folder) |
|
104 | + ); |
|
105 | + } |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * Get the template to use for the renderer. |
|
110 | + * |
|
111 | + * @since 1.0.0 |
|
112 | + * |
|
113 | + * @return Template Reference to the template that is used. |
|
114 | + * @throws RuntimeException |
|
115 | + */ |
|
116 | + public function getTemplate() |
|
117 | + { |
|
118 | + |
|
119 | + if ( ! $this->template) { |
|
120 | + $this->setDefaultTemplate(); |
|
121 | + } |
|
122 | + |
|
123 | + if (is_string($this->template)) { |
|
124 | + $this->template = $this->createTemplate($this->template); |
|
125 | + } |
|
126 | + |
|
127 | + return $this->template; |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * Set the template to use for the renderer. |
|
132 | + * |
|
133 | + * @since 1.0.0 |
|
134 | + * |
|
135 | + * @param string|Template $template Template to use for the |
|
136 | + * renderer. |
|
137 | + * |
|
138 | + * @throws InvalidTemplate If the template class could not be instantiated. |
|
139 | + * @throws InvalidTemplate If the template type is not recognized. |
|
140 | + */ |
|
141 | + public function setTemplate($template) |
|
142 | + { |
|
143 | + try { |
|
144 | + if (is_string($template)) { |
|
145 | + $template = $this->createTemplate($template); |
|
146 | + } |
|
147 | + } catch (Exception $exception) { |
|
148 | + throw new InvalidTemplate( |
|
149 | + 'Could not instantiate the template class "%1$s". Reason: "%2$s".', |
|
150 | + $template, |
|
151 | + $exception->getMessage() |
|
152 | + ); |
|
153 | + } |
|
154 | + |
|
155 | + if ( ! $template instanceof Template) { |
|
156 | + throw new InvalidTemplate( |
|
157 | + 'Could not set the template, invalid type.', |
|
158 | + (array)$template |
|
159 | + ); |
|
160 | + } |
|
161 | + $this->template = $template; |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * Add a section to the Mail. |
|
166 | + * |
|
167 | + * @since 1.0.0 |
|
168 | + * |
|
169 | + * @param string $type Type of section to add. |
|
170 | + * @param string $content Content of the section. |
|
171 | + * |
|
172 | + * @throws RuntimeException |
|
173 | + */ |
|
174 | + public function addSection($type, $content) |
|
175 | + { |
|
176 | + $this->sectionContent[$type] = $content; |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * Render the Mail for a given context. |
|
181 | + * |
|
182 | + * @since 1.0.0 |
|
183 | + * |
|
184 | + * @param array $context The context in which to render the email. |
|
185 | + * |
|
186 | + * @return string Rendered output of the email |
|
187 | + */ |
|
188 | + public function render(array $context) |
|
189 | + { |
|
190 | + $template = $this->getTemplate(); |
|
191 | + |
|
192 | + $context['template'] = $template; |
|
193 | + |
|
194 | + $this->instantiateSections($template->getUsedSections(), $context); |
|
195 | + |
|
196 | + $context['format'] = $this->getFormat(); |
|
197 | + |
|
198 | + $context = $this->setContext($context); |
|
199 | + |
|
200 | + return $template->render($context); |
|
201 | + } |
|
202 | + |
|
203 | + /** |
|
204 | + * Instantiate the requested sections for a template. |
|
205 | + * |
|
206 | + * @since 1.0.0 |
|
207 | + * |
|
208 | + * @param array $sections Sections to instantiate. |
|
209 | + * @param array $context The context in which to instantiate the sections. |
|
210 | + */ |
|
211 | + protected function instantiateSections(array $sections, array &$context) |
|
212 | + { |
|
213 | + $sectionFactory = new Factory($this->config, 'sections'); |
|
214 | + |
|
215 | + foreach ($sections as $section) { |
|
216 | + |
|
217 | + $content = null; |
|
218 | + |
|
219 | + if (array_key_exists($section, $this->sectionContent)) { |
|
220 | + $content = $this->sectionContent[$section]; |
|
221 | + } |
|
222 | + |
|
223 | + $context['sections'][$section] = $sectionFactory->create( |
|
224 | + $section, |
|
225 | + [$section, $content, $this->viewBuilder] |
|
226 | + ); |
|
227 | + } |
|
228 | + } |
|
229 | + |
|
230 | + /** |
|
231 | + * Set the format of the mail. |
|
232 | + * |
|
233 | + * @since 1.0.0 |
|
234 | + * |
|
235 | + * @return string Format of the Mail. |
|
236 | + */ |
|
237 | + protected function getFormat() |
|
238 | + { |
|
239 | + return $this->format; |
|
240 | + } |
|
241 | + |
|
242 | + /** |
|
243 | + * Set the format of the mail. |
|
244 | + * |
|
245 | + * @since 1.0.0 |
|
246 | + * |
|
247 | + * @return void |
|
248 | + */ |
|
249 | + abstract protected function setFormat(); |
|
250 | + |
|
251 | + /** |
|
252 | + * Set the template to the default template defined in the configuration. |
|
253 | + * |
|
254 | + * @since 1.0.0 |
|
255 | + * |
|
256 | + * @throws RuntimeException |
|
257 | + */ |
|
258 | + protected function setDefaultTemplate() |
|
259 | + { |
|
260 | + $defaultTemplate = $this->config->getKey('default_template'); |
|
261 | + $this->setTemplate($defaultTemplate); |
|
262 | + } |
|
263 | + |
|
264 | + /** |
|
265 | + * Create an instance of a template. |
|
266 | + * |
|
267 | + * @since 1.0.0 |
|
268 | + * |
|
269 | + * @param string $template Template to instantiate. |
|
270 | + * |
|
271 | + * @return Template $template Newly created instance. |
|
272 | + * @throws RuntimeException |
|
273 | + */ |
|
274 | + protected function createTemplate($template) |
|
275 | + { |
|
276 | + $templateFactory = new Factory($this->config, 'templates'); |
|
277 | + |
|
278 | + return $templateFactory->create($template, [$template, $this->viewBuilder]); |
|
279 | + } |
|
280 | + |
|
281 | + /** |
|
282 | + * Set the context of the mail. |
|
283 | + * |
|
284 | + * @since 1.0.0 |
|
285 | + * |
|
286 | + * @param array $context Context to set/modify. |
|
287 | + * |
|
288 | + * @return array Updated context. |
|
289 | + */ |
|
290 | + abstract protected function setContext(array $context); |
|
291 | 291 | } |
@@ -155,7 +155,7 @@ |
||
155 | 155 | if ( ! $template instanceof Template) { |
156 | 156 | throw new InvalidTemplate( |
157 | 157 | 'Could not set the template, invalid type.', |
158 | - (array)$template |
|
158 | + (array) $template |
|
159 | 159 | ); |
160 | 160 | } |
161 | 161 | $this->template = $template; |
@@ -26,75 +26,75 @@ |
||
26 | 26 | class Factory |
27 | 27 | { |
28 | 28 | |
29 | - /** |
|
30 | - * Configuration Settings. |
|
31 | - * |
|
32 | - * @since 1.0.0 |
|
33 | - * |
|
34 | - * @var ConfigInterface |
|
35 | - */ |
|
36 | - protected $config; |
|
29 | + /** |
|
30 | + * Configuration Settings. |
|
31 | + * |
|
32 | + * @since 1.0.0 |
|
33 | + * |
|
34 | + * @var ConfigInterface |
|
35 | + */ |
|
36 | + protected $config; |
|
37 | 37 | |
38 | - /** |
|
39 | - * Type of element the factory wants to create. |
|
40 | - * |
|
41 | - * @since 1.0.0 |
|
42 | - * |
|
43 | - * @var string |
|
44 | - */ |
|
45 | - protected $element; |
|
38 | + /** |
|
39 | + * Type of element the factory wants to create. |
|
40 | + * |
|
41 | + * @since 1.0.0 |
|
42 | + * |
|
43 | + * @var string |
|
44 | + */ |
|
45 | + protected $element; |
|
46 | 46 | |
47 | - /** |
|
48 | - * Instantiate a Factory object. |
|
49 | - * |
|
50 | - * @since 1.0.0 |
|
51 | - * |
|
52 | - * @param ConfigInterface $config Configuration settings. |
|
53 | - * @param string $element The type of element to instantiate a factory for. |
|
54 | - * |
|
55 | - * @throws FailedToInstantiateFactory When an unknown element type is requested. |
|
56 | - */ |
|
57 | - public function __construct(ConfigInterface $config, $element) |
|
58 | - { |
|
47 | + /** |
|
48 | + * Instantiate a Factory object. |
|
49 | + * |
|
50 | + * @since 1.0.0 |
|
51 | + * |
|
52 | + * @param ConfigInterface $config Configuration settings. |
|
53 | + * @param string $element The type of element to instantiate a factory for. |
|
54 | + * |
|
55 | + * @throws FailedToInstantiateFactory When an unknown element type is requested. |
|
56 | + */ |
|
57 | + public function __construct(ConfigInterface $config, $element) |
|
58 | + { |
|
59 | 59 | |
60 | - $this->config = $config; |
|
60 | + $this->config = $config; |
|
61 | 61 | |
62 | - if ( ! $this->config->hasKey($element)) { |
|
63 | - throw new FailedToInstantiateFactory(sprintf( |
|
64 | - 'Could not instantiate Factory for unknown Element Type "%1$s".', |
|
65 | - $element |
|
66 | - )); |
|
67 | - } |
|
62 | + if ( ! $this->config->hasKey($element)) { |
|
63 | + throw new FailedToInstantiateFactory(sprintf( |
|
64 | + 'Could not instantiate Factory for unknown Element Type "%1$s".', |
|
65 | + $element |
|
66 | + )); |
|
67 | + } |
|
68 | 68 | |
69 | - $this->element = $element; |
|
70 | - } |
|
69 | + $this->element = $element; |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * Create and return a new instance of an element. |
|
74 | - * |
|
75 | - * @since 1.0.0 |
|
76 | - * |
|
77 | - * @param string $type Type of element to create. |
|
78 | - * @param mixed $arguments Optional. Arguments to pass to the object. |
|
79 | - * |
|
80 | - * @return object New instance of the requested class. |
|
81 | - * @throws FailedToInstantiateClass If an unknown element type is requested. |
|
82 | - */ |
|
83 | - public function create($type, $arguments = null) |
|
84 | - { |
|
72 | + /** |
|
73 | + * Create and return a new instance of an element. |
|
74 | + * |
|
75 | + * @since 1.0.0 |
|
76 | + * |
|
77 | + * @param string $type Type of element to create. |
|
78 | + * @param mixed $arguments Optional. Arguments to pass to the object. |
|
79 | + * |
|
80 | + * @return object New instance of the requested class. |
|
81 | + * @throws FailedToInstantiateClass If an unknown element type is requested. |
|
82 | + */ |
|
83 | + public function create($type, $arguments = null) |
|
84 | + { |
|
85 | 85 | |
86 | - $classMap = $this->config[$this->element]; |
|
86 | + $classMap = $this->config[$this->element]; |
|
87 | 87 | |
88 | - if ( ! array_key_exists($type, $classMap)) { |
|
89 | - throw new FailedToInstantiateClass(sprintf( |
|
90 | - 'Could not create object, unknown Type "%1$s" for "%2$s" elements.', |
|
91 | - $type, |
|
92 | - $this->element |
|
93 | - )); |
|
94 | - } |
|
88 | + if ( ! array_key_exists($type, $classMap)) { |
|
89 | + throw new FailedToInstantiateClass(sprintf( |
|
90 | + 'Could not create object, unknown Type "%1$s" for "%2$s" elements.', |
|
91 | + $type, |
|
92 | + $this->element |
|
93 | + )); |
|
94 | + } |
|
95 | 95 | |
96 | - $className = $classMap[$type]['class_name']; |
|
96 | + $className = $classMap[$type]['class_name']; |
|
97 | 97 | |
98 | - return new $className($this->config, $arguments); |
|
99 | - } |
|
98 | + return new $className($this->config, $arguments); |
|
99 | + } |
|
100 | 100 | } |
@@ -29,132 +29,132 @@ |
||
29 | 29 | abstract class AbstractTemplate implements Template |
30 | 30 | { |
31 | 31 | |
32 | - /** |
|
33 | - * Configuration Settings. |
|
34 | - * |
|
35 | - * @since 1.0.0 |
|
36 | - * |
|
37 | - * @var ConfigInterface |
|
38 | - */ |
|
39 | - protected $config; |
|
40 | - |
|
41 | - /** |
|
42 | - * Name of the template. |
|
43 | - * |
|
44 | - * @since 1.0.0 |
|
45 | - * |
|
46 | - * @var string |
|
47 | - */ |
|
48 | - protected $templateName; |
|
49 | - |
|
50 | - /** |
|
51 | - * ViewBuilder to create template and section views. |
|
52 | - * |
|
53 | - * @since 1.0.0 |
|
54 | - * |
|
55 | - * @var ViewBuilder |
|
56 | - */ |
|
57 | - protected $viewBuilder; |
|
58 | - |
|
59 | - /** |
|
60 | - * Instantiate a AbstractTemplate object. |
|
61 | - * |
|
62 | - * @since 1.0.0 |
|
63 | - * |
|
64 | - * @param ConfigInterface $config Configuration settings. |
|
65 | - * @param array $arguments Arguments that are passed through the constructor. |
|
66 | - * Contained elements: string $template |
|
67 | - * |
|
68 | - * @throws RuntimeException |
|
69 | - */ |
|
70 | - public function __construct($config, array $arguments) |
|
71 | - { |
|
72 | - $this->config = $config; |
|
73 | - list($template, $this->viewBuilder) = $arguments; |
|
74 | - $this->setTemplateName($template); |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * Get the name of the Template. |
|
79 | - * |
|
80 | - * @since 1.0.0 |
|
81 | - * |
|
82 | - * @return string Name of the template. |
|
83 | - */ |
|
84 | - public function getTemplateName() |
|
85 | - { |
|
86 | - return $this->templateName; |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * Set the name of the Template. |
|
91 | - * |
|
92 | - * @since 1.0.0 |
|
93 | - * |
|
94 | - * @param string|null $template Optional. Name of the template. |
|
95 | - * |
|
96 | - * @throws FailedToInitialiseTemplate If no template name was passed. |
|
97 | - * @throws FailedToInitialiseTemplate If an unknown template name was passed. |
|
98 | - */ |
|
99 | - protected function setTemplateName($template = null) |
|
100 | - { |
|
101 | - if (null === $template) { |
|
102 | - throw new FailedToInitialiseTemplate('Initialised template without passing it a template name.'); |
|
103 | - } |
|
104 | - if ( ! array_key_exists($template, $this->config['templates'])) { |
|
105 | - throw new FailedToInitialiseTemplate('Initialised template with an unknown template name.'); |
|
106 | - } |
|
107 | - $this->templateName = $template; |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * Get an array of Sections that are used by this template. |
|
112 | - * |
|
113 | - * @since 1.0.0 |
|
114 | - * |
|
115 | - * @return array Sections that are used by this template. |
|
116 | - */ |
|
117 | - public function getUsedSections() |
|
118 | - { |
|
119 | - return $this->config['templates'][$this->getTemplateName()]['sections']; |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * Render the template for a given context. |
|
124 | - * |
|
125 | - * @since 1.0.0 |
|
126 | - * |
|
127 | - * @param array $context The context in which to render the template. |
|
128 | - * |
|
129 | - * @return string The rendered content. |
|
130 | - */ |
|
131 | - public function render(array $context) |
|
132 | - { |
|
133 | - |
|
134 | - $viewName = $this->getViewName($context); |
|
135 | - $view = $this->viewBuilder->create($viewName); |
|
136 | - |
|
137 | - $sanitizerType = $this->config['formats'][$context['format']]['sanitizer']; |
|
138 | - $sanitizerFactory = new Factory($this->config, 'sanitizers'); |
|
139 | - $sanitizer = $sanitizerFactory->create($sanitizerType); |
|
140 | - |
|
141 | - $output = $view->render($context); |
|
142 | - |
|
143 | - return $sanitizer->sanitize($output, $context); |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * Get the name of the View to use for rendering. |
|
148 | - * |
|
149 | - * @since 1.0.0 |
|
150 | - * |
|
151 | - * @param array $context Context in which to get the view name. |
|
152 | - * |
|
153 | - * @return string Name of the view. |
|
154 | - */ |
|
155 | - protected function getViewName(array $context) |
|
156 | - { |
|
157 | - return $this->config['templates'][$this->getTemplateName()]['view_name'] |
|
158 | - . '.' . $context['format']; |
|
159 | - } |
|
32 | + /** |
|
33 | + * Configuration Settings. |
|
34 | + * |
|
35 | + * @since 1.0.0 |
|
36 | + * |
|
37 | + * @var ConfigInterface |
|
38 | + */ |
|
39 | + protected $config; |
|
40 | + |
|
41 | + /** |
|
42 | + * Name of the template. |
|
43 | + * |
|
44 | + * @since 1.0.0 |
|
45 | + * |
|
46 | + * @var string |
|
47 | + */ |
|
48 | + protected $templateName; |
|
49 | + |
|
50 | + /** |
|
51 | + * ViewBuilder to create template and section views. |
|
52 | + * |
|
53 | + * @since 1.0.0 |
|
54 | + * |
|
55 | + * @var ViewBuilder |
|
56 | + */ |
|
57 | + protected $viewBuilder; |
|
58 | + |
|
59 | + /** |
|
60 | + * Instantiate a AbstractTemplate object. |
|
61 | + * |
|
62 | + * @since 1.0.0 |
|
63 | + * |
|
64 | + * @param ConfigInterface $config Configuration settings. |
|
65 | + * @param array $arguments Arguments that are passed through the constructor. |
|
66 | + * Contained elements: string $template |
|
67 | + * |
|
68 | + * @throws RuntimeException |
|
69 | + */ |
|
70 | + public function __construct($config, array $arguments) |
|
71 | + { |
|
72 | + $this->config = $config; |
|
73 | + list($template, $this->viewBuilder) = $arguments; |
|
74 | + $this->setTemplateName($template); |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * Get the name of the Template. |
|
79 | + * |
|
80 | + * @since 1.0.0 |
|
81 | + * |
|
82 | + * @return string Name of the template. |
|
83 | + */ |
|
84 | + public function getTemplateName() |
|
85 | + { |
|
86 | + return $this->templateName; |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * Set the name of the Template. |
|
91 | + * |
|
92 | + * @since 1.0.0 |
|
93 | + * |
|
94 | + * @param string|null $template Optional. Name of the template. |
|
95 | + * |
|
96 | + * @throws FailedToInitialiseTemplate If no template name was passed. |
|
97 | + * @throws FailedToInitialiseTemplate If an unknown template name was passed. |
|
98 | + */ |
|
99 | + protected function setTemplateName($template = null) |
|
100 | + { |
|
101 | + if (null === $template) { |
|
102 | + throw new FailedToInitialiseTemplate('Initialised template without passing it a template name.'); |
|
103 | + } |
|
104 | + if ( ! array_key_exists($template, $this->config['templates'])) { |
|
105 | + throw new FailedToInitialiseTemplate('Initialised template with an unknown template name.'); |
|
106 | + } |
|
107 | + $this->templateName = $template; |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * Get an array of Sections that are used by this template. |
|
112 | + * |
|
113 | + * @since 1.0.0 |
|
114 | + * |
|
115 | + * @return array Sections that are used by this template. |
|
116 | + */ |
|
117 | + public function getUsedSections() |
|
118 | + { |
|
119 | + return $this->config['templates'][$this->getTemplateName()]['sections']; |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * Render the template for a given context. |
|
124 | + * |
|
125 | + * @since 1.0.0 |
|
126 | + * |
|
127 | + * @param array $context The context in which to render the template. |
|
128 | + * |
|
129 | + * @return string The rendered content. |
|
130 | + */ |
|
131 | + public function render(array $context) |
|
132 | + { |
|
133 | + |
|
134 | + $viewName = $this->getViewName($context); |
|
135 | + $view = $this->viewBuilder->create($viewName); |
|
136 | + |
|
137 | + $sanitizerType = $this->config['formats'][$context['format']]['sanitizer']; |
|
138 | + $sanitizerFactory = new Factory($this->config, 'sanitizers'); |
|
139 | + $sanitizer = $sanitizerFactory->create($sanitizerType); |
|
140 | + |
|
141 | + $output = $view->render($context); |
|
142 | + |
|
143 | + return $sanitizer->sanitize($output, $context); |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * Get the name of the View to use for rendering. |
|
148 | + * |
|
149 | + * @since 1.0.0 |
|
150 | + * |
|
151 | + * @param array $context Context in which to get the view name. |
|
152 | + * |
|
153 | + * @return string Name of the view. |
|
154 | + */ |
|
155 | + protected function getViewName(array $context) |
|
156 | + { |
|
157 | + return $this->config['templates'][$this->getTemplateName()]['view_name'] |
|
158 | + . '.' . $context['format']; |
|
159 | + } |
|
160 | 160 | } |
@@ -155,6 +155,6 @@ |
||
155 | 155 | protected function getViewName(array $context) |
156 | 156 | { |
157 | 157 | return $this->config['templates'][$this->getTemplateName()]['view_name'] |
158 | - . '.' . $context['format']; |
|
158 | + . '.'.$context['format']; |
|
159 | 159 | } |
160 | 160 | } |