11<?php
22
3- namespace Northys ;
3+ namespace Northys \ CSSInliner ;
44
5+ use Northys \CSSInliner \Exceptions ;
6+ use DOMDocument ;
7+ use DOMXPath ;
8+ use Exception ;
59use Sabberworm \CSS ;
610use Symfony \Component \CssSelector \CssSelector ;
711
812
9-
1013/**
14+ * Class CSSInliner
1115 * @author Northys
16+ * @package Northys\CSSInliner
1217 */
1318class CSSInliner
1419{
@@ -19,29 +24,37 @@ class CSSInliner
1924 private $ css ;
2025
2126 /**
22- * @var DOMDocument
27+ * @var \ DOMDocument
2328 */
2429 private $ dom ;
2530
2631 /**
27- * @var DOMXPath
32+ * @var \ DOMXPath
2833 */
2934 private $ finder ;
3035
3136
32-
33- public function addCSS ($ filename )
37+ /**
38+ * Provides you an option to add as many CSS files as you want
39+ * @param string $filename represents css file path
40+ * @throws \Exception
41+ */
42+ public function addCSS ($ filename )
3443 {
3544 if ( ! $ css = @file_get_contents ($ filename )) {
36- throw new \ Exception ( " Failed on loading CSS file. Check the file path you have provided! " , 1 );
45+ throw new Exceptions \ InvalidCssFilePathException ( ' Invalid css file path provided. ' );
3746 }
3847 // merge all CSS content into $this-css variable
3948 $ this ->css .= $ css ;
4049 }
4150
4251
43-
44- private function getCSS () {
52+ /**
53+ * Gets styles from <style> html tag - if there are any it will be merged with another styles added by addCss()
54+ * @return CSS\CSSList\Document
55+ * @throws \Exception
56+ */
57+ private function getCSS () {
4558 // get styles inside <style> tags in provided HTML
4659 foreach ($ this ->dom ->getElementsByTagName ('style ' ) as $ style ) {
4760 $ this ->css .= $ style ->textContent ;
@@ -50,14 +63,18 @@ private function getCSS() {
5063
5164 $ css = $ parser ->parse ();
5265 if (!$ css ) {
53- throw new \ Exception ( " You haven't provided any styles by addCSS() method. There is also no styles inside HTML. " , 1 );
66+ throw new Exceptions \ NoCssRulesException ( ' There are no CSS rules provided. ' );
5467 }
5568 return $ css ;
5669 }
5770
5871
59-
60- public function render ($ html )
72+ /**
73+ * Prepares everything and inserts inline styles into html
74+ * @param string $html represents html document
75+ * @return string
76+ */
77+ public function render ($ html )
6178 {
6279 $ this ->dom = new \DOMDocument ;
6380 $ this ->dom ->loadHTML ($ html );
0 commit comments