Skip to content

Commit aba5539

Browse files
committed
Changed all exception to specific objects
1 parent 5df4c5f commit aba5539

14 files changed

+61
-36
lines changed

src/PHPHtmlParser/Curl.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
<?php
22
namespace PHPHtmlParser;
33

4+
use PHPHtmlParser\Exceptions\CurlException;
5+
46
class Curl implements CurlInterface {
57

68
/**
79
* A simple curl implementation to get the content of the url.
810
*
911
* @param string $url
1012
* @return string
11-
* @throws Exception
13+
* @throws CurlException
1214
*/
1315
public function get($url)
1416
{
@@ -22,7 +24,7 @@ public function get($url)
2224
{
2325
// there was a problem
2426
$error = curl_error($ch);
25-
throw new Exception('Error retrieving "'.$url.'" ('.$error.')');
27+
throw new CurlException('Error retrieving "'.$url.'" ('.$error.')');
2628
}
2729

2830
return $content;

src/PHPHtmlParser/Dom.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use PHPHtmlParser\Dom\HtmlNode;
55
use PHPHtmlParser\Dom\TextNode;
6+
use PHPHtmlParser\Exceptions\NotLoadedException;
67
use stringEncode\Encode;
78

89
class Dom {
@@ -306,11 +307,6 @@ protected function loadStr($str, $option)
306307
$this->rawSize = strlen($str);
307308
$this->raw = $str;
308309

309-
// clean out none-html text
310-
if ( ! $this instanceof Dom)
311-
{
312-
throw new \Exception(get_class($this));
313-
}
314310
$html = $this->clean($str);
315311

316312
$this->size = strlen($str);
@@ -325,13 +321,13 @@ protected function loadStr($str, $option)
325321
/**
326322
* Checks if the load methods have been called.
327323
*
328-
* @throws Exception
324+
* @throws NotLoadedException
329325
*/
330326
protected function isLoaded()
331327
{
332328
if (is_null($this->content))
333329
{
334-
throw new Exception('Content is not loaded!');
330+
throw new NotLoadedException('Content is not loaded!');
335331
}
336332
}
337333

src/PHPHtmlParser/Dom/AbstractNode.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
namespace PHPHtmlParser\Dom;
33

44
use PHPHtmlParser\Selector;
5+
use PHPHtmlParser\Exceptions\ChildNotFoundException;
6+
use PHPHtmlParser\Exceptions\CircularException;
7+
use PHPHtmlParser\Exceptions\ParentNotFoundException;
58
use stringEncode\Encode;
69

710
/**
@@ -130,14 +133,14 @@ public function getParent()
130133
*
131134
* @param AbstractNode $parent
132135
* @chainable
133-
* @throws Exception
136+
* @throws CircularException
134137
*/
135138
public function setParent(AbstractNode $parent)
136139
{
137140
// check integrity
138141
if ($this->isDescendant($parent->id()))
139142
{
140-
throw new Exception('Can not add descendant "'.$parent->id().'" as my parent.');
143+
throw new CircularException('Can not add descendant "'.$parent->id().'" as my parent.');
141144
}
142145

143146
// remove from old parent
@@ -195,13 +198,13 @@ public function hasChildren()
195198
*
196199
* @param int $id
197200
* @return AbstractNode
198-
* @throw Exception
201+
* @throws ChildNotFoundException
199202
*/
200203
public function getChild($id)
201204
{
202205
if ( ! isset($this->children[$id]))
203206
{
204-
throw new Exception('Child "'.$id.'" not found in this node.');
207+
throw new ChildNotFoundException("Child '$id' not found in this node.");
205208
}
206209

207210
return $this->children[$id]['node'];
@@ -213,6 +216,7 @@ public function getChild($id)
213216
*
214217
* @param AbstractNode $child
215218
* @return bool
219+
* @throws CircularExceptionException
216220
*/
217221
public function addChild(AbstractNode $child)
218222
{
@@ -222,13 +226,13 @@ public function addChild(AbstractNode $child)
222226
// check integrity
223227
if ($this->isAncestor($child->id()))
224228
{
225-
throw new Exception('Can not add child. It is my ancestor.');
229+
throw new CircularException('Can not add child. It is my ancestor.');
226230
}
227231

228232
// check if child is itself
229233
if ($child->id() == $this->id)
230234
{
231-
throw new Exception('Can not set itself as a child.');
235+
throw new CircularException('Can not set itself as a child.');
232236
}
233237

234238
if ($this->hasChildren())
@@ -416,13 +420,13 @@ public function lastChild()
416420
* Attempts to get the next sibling.
417421
*
418422
* @return AbstractNode
419-
* @throws Exception
423+
* @throws ParentNotFoundException
420424
*/
421425
public function nextSibling()
422426
{
423427
if (is_null($this->parent))
424428
{
425-
throw new Exception('Parent is not set for this node.');
429+
throw new ParentNotFoundException('Parent is not set for this node.');
426430
}
427431

428432
return $this->parent->nextChild($this->id);
@@ -432,13 +436,13 @@ public function nextSibling()
432436
* Attempts to get the previous sibling
433437
*
434438
* @return AbstractNode
435-
* @throw Exception
439+
* @throws ParentNotFoundException
436440
*/
437441
public function previousSibling()
438442
{
439443
if (is_null($this->parent))
440444
{
441-
throw new Exception('Parent is not set for this node.');
445+
throw new ParentNotFoundException('Parent is not set for this node.');
442446
}
443447

444448
return $this->parent->previousChild($this->id);
@@ -493,7 +497,7 @@ public function getAttribute($key)
493497
*
494498
* @param string $tag
495499
* @return AbstractNode
496-
* @throws Exception
500+
* @throws ParentNotFoundException
497501
*/
498502
public function ancestorByTag($tag)
499503
{
@@ -510,7 +514,7 @@ public function ancestorByTag($tag)
510514
$node = $node->getParent();
511515
}
512516

513-
throw new Exception('Could not find an ancestor with "'.$tag.'" tag');
517+
throw new ParentNotFoundException('Could not find an ancestor with "'.$tag.'" tag');
514518
}
515519

516520
/**

src/PHPHtmlParser/Dom/Exception.php

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/PHPHtmlParser/Dom/HtmlNode.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?php
22
namespace PHPHtmlParser\Dom;
33

4+
use PHPHtmlParser\Exceptions\UnkownChildTypeException;
5+
use PHPHtmlParser\Exceptions\ChildNotFoundException;
6+
47
class HtmlNode extends AbstractNode {
58

69
/**
@@ -39,6 +42,7 @@ public function __construct($tag)
3942
* Gets the inner html of this node.
4043
*
4144
* @return string
45+
* @throws UnkownChildTypeException
4246
*/
4347
public function innerHtml()
4448
{
@@ -70,14 +74,14 @@ public function innerHtml()
7074
}
7175
else
7276
{
73-
throw new Exception('Error: Unkowne child type "'.get_class($child).'" found in node');
77+
throw new UnknownChildTypeException('Unknown child type "'.get_class($child).'" found in node');
7478
}
7579

7680
try
7781
{
7882
$child = $this->nextChild($child->id());
7983
}
80-
catch (Exception $e)
84+
catch (ChildNotFoundException $e)
8185
{
8286
// no more children
8387
$child = null;

src/PHPHtmlParser/Exception.php

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
namespace PHPHtmlParser\Exceptions;
3+
4+
final class ChildNotFoundException extends \Exception {}
5+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
namespace PHPHtmlParser\Exceptions;
3+
4+
final class CircularException extends \Exception {};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
namespace PHPHtmlParser\Exceptions;
3+
4+
class CurlException extends \Exception {}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
namespace PHPHtmlParser\Exceptions;
3+
4+
final class NotLoadedException extends \Exception {}

0 commit comments

Comments
 (0)