Skip to content

Commit

Permalink
Small refactor - Added private methods for DTO
Browse files Browse the repository at this point in the history
  • Loading branch information
paquettg committed Aug 23, 2020
1 parent 382b98c commit cf0bb68
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 60 deletions.
1 change: 1 addition & 0 deletions .gitattributes
@@ -1,4 +1,5 @@
/tests export-ignore
/tests linguist-documentation
/.scrutinizar.yml export-ignore
/.travis.yml export-ignore
/.gitignore export-ignore
Expand Down
19 changes: 15 additions & 4 deletions src/PHPHtmlParser/DTO/Selector/ParsedSelectorCollectionDTO.php
Expand Up @@ -11,15 +11,26 @@ final class ParsedSelectorCollectionDTO
*/
private $parsedSelectorDTO = [];

public function __construct(array $values)
/**
* @param ParsedSelectorDTO[] $parsedSelectorDTOs
*/
private function __construct(array $parsedSelectorDTOs)
{
foreach ($values as $value) {
if ($value instanceof ParsedSelectorDTO) {
$this->parsedSelectorDTO[] = $value;
foreach ($parsedSelectorDTOs as $parsedSelectorDTO) {
if ($parsedSelectorDTO instanceof ParsedSelectorDTO) {
$this->parsedSelectorDTO[] = $parsedSelectorDTO;
}
}
}

/**
* @param ParsedSelectorDTO[] $parsedSelectorDTOs
*/
public static function makeCollection(array $parsedSelectorDTOs): ParsedSelectorCollectionDTO
{
return new ParsedSelectorCollectionDTO($parsedSelectorDTOs);
}

/**
* @return ParsedSelectorDTO[]
*/
Expand Down
19 changes: 15 additions & 4 deletions src/PHPHtmlParser/DTO/Selector/ParsedSelectorDTO.php
Expand Up @@ -11,15 +11,26 @@ final class ParsedSelectorDTO
*/
private $rules = [];

public function __construct(array $values)
/**
* @param RuleDTO[] $ruleDTOs
*/
private function __construct(array $ruleDTOs)
{
foreach ($values as $value) {
if ($value instanceof RuleDTO) {
$this->rules[] = $value;
foreach ($ruleDTOs as $ruleDTO) {
if ($ruleDTO instanceof RuleDTO) {
$this->rules[] = $ruleDTO;
}
}
}

/**
* @param RuleDTO[] $ruleDTOs
*/
public static function makeFromRules(array $ruleDTOs): ParsedSelectorDTO
{
return new ParsedSelectorDTO($ruleDTOs);
}

/**
* @return RuleDTO[]
*/
Expand Down
26 changes: 15 additions & 11 deletions src/PHPHtmlParser/DTO/Selector/RuleDTO.php
Expand Up @@ -36,7 +36,7 @@ final class RuleDTO
*/
private $alterNext;

public function __construct(array $values)
private function __construct(array $values)
{
$this->tag = $values['tag'];
$this->operator = $values['operator'];
Expand All @@ -47,16 +47,26 @@ public function __construct(array $values)
}

/**
* @return string
* @param string|array|null $key
* @param string|array|null $value
*/
public static function makeFromPrimitives(string $tag, string $operator, $key, $value, bool $noKey, bool $alterNext): RuleDTO
{
return new RuleDTO([
'tag' => $tag,
'operator' => $operator,
'key' => $key,
'value' => $value,
'noKey' => $noKey,
'alterNext' => $alterNext,
]);
}

public function getTag(): string
{
return $this->tag;
}

/**
* @return string
*/
public function getOperator(): string
{
return $this->operator;
Expand All @@ -78,17 +88,11 @@ public function getValue()
return $this->value;
}

/**
* @return bool
*/
public function isNoKey(): bool
{
return $this->noKey;
}

/**
* @return bool
*/
public function isAlterNext(): bool
{
return $this->alterNext;
Expand Down
10 changes: 9 additions & 1 deletion src/PHPHtmlParser/DTO/Tag/AttributeDTO.php
Expand Up @@ -19,12 +19,20 @@ final class AttributeDTO
*/
private $doubleQuote;

public function __construct(array $values)
private function __construct(array $values)
{
$this->value = $values['value'];
$this->doubleQuote = $values['doubleQuote'] ?? true;
}

public static function makeFromPrimitives(?string $value, bool $doubleQuote = true): AttributeDTO
{
return new AttributeDTO([
'value' => $value,
'doubleQuote' => $doubleQuote,
]);
}

public function getValue(): ?string
{
return $this->value;
Expand Down
12 changes: 11 additions & 1 deletion src/PHPHtmlParser/DTO/TagDTO.php
Expand Up @@ -28,14 +28,24 @@ final class TagDTO
*/
private $tag;

public function __construct(array $values = [])
private function __construct(array $values = [])
{
$this->status = $values['status'] ?? false;
$this->closing = $values['closing'] ?? false;
$this->node = $values['node'] ?? null;
$this->tag = $values['tag'] ?? null;
}

public static function makeFromPrimitives(bool $status = false, bool $closing = false, ?HtmlNode $node = null, ?string $tag = null): TagDTO
{
return new TagDTO([
'status' => $status,
'closing' => $closing,
'node' => $node,
'tag' => $tag,
]);
}

public function isStatus(): bool
{
return $this->status;
Expand Down
22 changes: 6 additions & 16 deletions src/PHPHtmlParser/Dom/Parser.php
Expand Up @@ -160,18 +160,17 @@ public function detectCharset(Options $options, string $defaultCharset, Abstract
*/
private function parseTag(Options $options, Content $content, int $size): TagDTO
{
$return = [];
if ($content->char() != '<') {
// we are not at the beginning of a tag
return new TagDTO();
return TagDTO::makeFromPrimitives();
}

// check if this is a closing tag
try {
$content->fastForward(1);
} catch (ContentLengthException $exception) {
// we are at the end of the file
return new TagDTO();
return TagDTO::makeFromPrimitives();
}
if ($content->char() == '/') {
return $this->makeEndTag($content, $options);
Expand All @@ -188,7 +187,7 @@ private function parseTag(Options $options, Content $content, int $size): TagDTO
$tag = \strtolower($content->copyByToken(StringToken::SLASH(), true));
if (\trim($tag) == '') {
// no tag found, invalid < found
return new TagDTO();
return TagDTO::makeFromPrimitives();
}
}
$node = new HtmlNode($tag);
Expand Down Expand Up @@ -220,10 +219,7 @@ private function parseTag(Options $options, Content $content, int $size): TagDTO
$content->fastForward(1);
}

$return['status'] = true;
$return['node'] = $node;

return new TagDTO($return);
return TagDTO::makeFromPrimitives(true, false, $node);
}

/**
Expand All @@ -249,7 +245,6 @@ private function detectHTML5Charset(Encode $encode, AbstractNode $root): bool
*/
private function makeEndTag(Content $content, Options $options): TagDTO
{
$return = [];
$tag = $content->fastForward(1)
->copyByToken(StringToken::SLASH(), true);
// move to end of tag
Expand All @@ -259,15 +254,10 @@ private function makeEndTag(Content $content, Options $options): TagDTO
// check if this closing tag counts
$tag = \strtolower($tag);
if (\in_array($tag, $options->getSelfClosing(), true)) {
$return['status'] = true;

return new TagDTO($return);
return TagDTO::makeFromPrimitives(true);
}
$return['status'] = true;
$return['closing'] = true;
$return['tag'] = \strtolower($tag);

return new TagDTO($return);
return TagDTO::makeFromPrimitives(true, true, null, \strtolower($tag));
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/PHPHtmlParser/Dom/Tag.php
Expand Up @@ -163,10 +163,10 @@ public function noise(string $noise): Tag
*/
public function setAttribute(string $key, ?string $attributeValue, bool $doubleQuote = true): Tag
{
$attributeDTO = new AttributeDTO([
'value' => $attributeValue,
'doubleQuote' => $doubleQuote,
]);
$attributeDTO = AttributeDTO::makeFromPrimitives(
$attributeValue,
$doubleQuote
);
if ($this->HtmlSpecialCharsDecode) {
$attributeDTO->htmlspecialcharsDecode();
}
Expand Down
22 changes: 11 additions & 11 deletions src/PHPHtmlParser/Selector/Parser.php
Expand Up @@ -92,25 +92,25 @@ public function parseSelectorString(string $selector): ParsedSelectorCollectionD
$noKey = true;
}

$rules[] = new RuleDTO([
'tag' => $tag,
'key' => $key,
'value' => $value,
'operator' => $operator,
'noKey' => $noKey,
'alterNext' => $alterNext,
]);
$rules[] = RuleDTO::makeFromPrimitives(
$tag,
$operator,
$key,
$value,
$noKey,
$alterNext
);
if (isset($match[7]) && \is_string($match[7]) && \trim($match[7]) == ',') {
$selectors[] = new ParsedSelectorDTO($rules);
$selectors[] = ParsedSelectorDTO::makeFromRules($rules);
$rules = [];
}
}

// save last results
if (\count($rules) > 0) {
$selectors[] = new ParsedSelectorDTO($rules);
$selectors[] = ParsedSelectorDTO::makeFromRules($rules);
}

return new ParsedSelectorCollectionDTO($selectors);
return ParsedSelectorCollectionDTO::makeCollection($selectors);
}
}
16 changes: 8 additions & 8 deletions tests/Selector/SeekerTest.php
Expand Up @@ -10,14 +10,14 @@ class SeekerTest extends TestCase
{
public function testSeekReturnEmptyArray()
{
$ruleDTO = new RuleDTO([
'tag' => 'tag',
'key' => 1,
'value' => null,
'operator' => null,
'noKey' => false,
'alterNext' => false,
]);
$ruleDTO = RuleDTO::makeFromPrimitives(
'tag',
'=',
null,
null,
false,
false
);
$seeker = new Seeker();
$results = $seeker->seek([], $ruleDTO, []);
$this->assertCount(0, $results);
Expand Down

0 comments on commit cf0bb68

Please sign in to comment.