Skip to content

Commit 476ee69

Browse files
committed
added custom exceptions, apigen comments and changed namespaces
1 parent dc4d23c commit 476ee69

2 files changed

Lines changed: 56 additions & 12 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Northys
5+
* Date: 25.7.14
6+
* Time: 4:10
7+
*/
8+
9+
namespace Northys\CSSInliner\Exceptions;
10+
11+
12+
/**
13+
* The exception that is thrown when invalid css file path is provided.
14+
* @package Northys\CSSInliner\Exceptions
15+
*/
16+
class InvalidCssFilePathException extends \Exception {
17+
18+
}
19+
20+
21+
/**
22+
* The exception that is thrown when there are no css rules.
23+
* @package Northys\CSSInliner\Exceptions
24+
*/
25+
class NoCssRulesException extends \Exception {
26+
27+
}

src/Northys/CssInliner/CssInliner.php

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
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;
59
use Sabberworm\CSS;
610
use Symfony\Component\CssSelector\CssSelector;
711

812

9-
1013
/**
14+
* Class CSSInliner
1115
* @author Northys
16+
* @package Northys\CSSInliner
1217
*/
1318
class 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

Comments
 (0)