Skip to content

Commit

Permalink
V3
Browse files Browse the repository at this point in the history
  • Loading branch information
badraxas committed Jan 23, 2024
1 parent 17bf573 commit bdd9db7
Show file tree
Hide file tree
Showing 24 changed files with 317 additions and 277 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ $missingLines = $adsTxt->diff($otherAdsTxt);
$newAdsTxt = new AdsTxt();
$newAdsTxt
->addLine(new Comment(' app-ads.txt file for vMVPD B:'))
->addLine(new Record('ssp.com', 'vwxyz', Relationship::DIRECT))
->addLine(new Record('ssp.com', 'vwxyz', 'DIRECT'))
->addLine(new Variable('inventorypartnerdomain', 'programmerA.com'))

// display ads.txt as string
Expand All @@ -140,7 +140,7 @@ use Badraxas\Adstxt\Enums\Relationship;
$vendorLine = new Record(
domain: 'example.com',
publisherId: '123456',
relationship: Relationship::DIRECT,
relationship: 'DIRECT',
certificationId: 'ABCD1234',
comment: null
);
Expand Down
6 changes: 3 additions & 3 deletions src/AdsTxt.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* Represents the content of an ads.txt file and provides methods to manage its lines.
*/
class AdsTxt implements \Stringable
class AdsTxt
{
/**
* @var array<AdsTxtLineInterface> An array containing all the lines of this ads.txt file.
Expand All @@ -27,12 +27,12 @@ class AdsTxt implements \Stringable
*
* @return string Returns the ads.txt content as a string.
*/
public function __toString(): string
public function pretty(bool $withComment = true): string
{
$output = '';

foreach ($this->lines as $line) {
$output .= sprintf('%s%s', $line->__toString(), PHP_EOL);
$output .= sprintf('%s%s', $line->pretty($withComment), PHP_EOL);
}

return trim($output);
Expand Down
38 changes: 0 additions & 38 deletions src/Enums/Relationship.php

This file was deleted.

3 changes: 2 additions & 1 deletion src/Interfaces/AdsTxtLineInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
* Represents an interface for lines in the ads.txt file.
* Implementing classes should provide their own custom logic for the __toString() method.
*/
interface AdsTxtLineInterface extends \Stringable
interface AdsTxtLineInterface
{
public function equals(AdsTxtLineInterface $adsTxtLine): bool;
public function pretty(bool $withComment = true): string;
}
53 changes: 53 additions & 0 deletions src/Lines/AbstractAdsTxtLine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Badraxas\Adstxt\Lines;

use Badraxas\Adstxt\Interfaces\AdsTxtLineInterface;

abstract class AbstractAdsTxtLine implements AdsTxtLineInterface
{
protected string $rawValue = '';
protected array $notice = [];
protected array $warning = [];
protected array $error = [];

public function getWarning()
{
return $this->warning;
}

public function addWarning(string $warning)
{
$this->warning[] = $warning;
}

public function getNotice()
{
return $this->notice;
}

public function addNotice(string $notice)
{
$this->notice[] = $notice;
}

public function getError()
{
return $this->error;
}

public function addError(string $error)
{
$this->error[] = $error;
}

public function setRawValue(string $rawValue)
{
$this->rawValue = $rawValue;
}

public function getRawValue(): string
{
return $this->rawValue;
}
}
19 changes: 7 additions & 12 deletions src/Lines/Blank.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,8 @@
*
* Represents a blank line in the ads.txt file.
*/
class Blank implements AdsTxtLineInterface
class Blank extends AbstractAdsTxtLine
{
/**
* Get the string representation of the Blank line.
*
* @return string returns an empty string representing the Blank line
*/
public function __toString(): string
{
return '';
}

/**
* Compares the current Record object with another AdsTxtLineInterface object.
*
Expand All @@ -31,6 +21,11 @@ public function __toString(): string
*/
public function equals(AdsTxtLineInterface $adsTxtLine): bool
{
return $adsTxtLine instanceof Blank && $adsTxtLine->__toString() === $this->__toString();
return $adsTxtLine instanceof Blank && $adsTxtLine->pretty() === $this->pretty();
}

public function pretty(bool $withComment = true): string
{
return '';
}
}
19 changes: 7 additions & 12 deletions src/Lines/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* Represents a comment line in the ads.txt file.
*/
class Comment implements AdsTxtLineInterface
class Comment extends AbstractAdsTxtLine
{
/**
* Comment constructor.
Expand All @@ -18,16 +18,6 @@ class Comment implements AdsTxtLineInterface
*/
public function __construct(private readonly string $comment) {}

/**
* Get the string representation of the Comment line.
*
* @return string returns the Comment line as a string
*/
public function __toString(): string
{
return sprintf('#%s', $this->comment);
}

/**
* Compares the current Record object with another AdsTxtLineInterface object.
*
Expand All @@ -38,11 +28,16 @@ public function __toString(): string
*/
public function equals(AdsTxtLineInterface $adsTxtLine): bool
{
return $adsTxtLine instanceof Comment && $adsTxtLine->__toString() === $this->__toString();
return $adsTxtLine instanceof Comment && $adsTxtLine->pretty() === $this->pretty();
}

public function getComment(): string
{
return $this->comment;
}

public function pretty(bool $withComment = true): string
{
return !$withComment ? '' : sprintf("# %s", $this->getComment());
}
}
32 changes: 11 additions & 21 deletions src/Lines/Invalid.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,15 @@
*
* Represents an invalid line in the ads.txt file.
*/
class Invalid implements AdsTxtLineInterface
class Invalid extends AbstractAdsTxtLine
{
/**
* Invalid constructor.
*
* @param string $value the value of the invalid line
* @param null|Comment $comment the comment associated with the invalid line (optional)
*/
public function __construct(private readonly string $value, private string $reason, private readonly ?Comment $comment = null) {}

/**
* Get the string representation of the Invalid line.
*
* @return string returns the Invalid line as a string
*/
public function __toString(): string
{
if (!isset($this->comment)) {
return $this->value;
}

return sprintf('%s %s', $this->value, $this->comment->__toString());
}
public function __construct(private readonly string $value, private readonly ?Comment $comment = null) {}

/**
* Compares the current Record object with another AdsTxtLineInterface object.
Expand All @@ -43,21 +29,25 @@ public function __toString(): string
*/
public function equals(AdsTxtLineInterface $adsTxtLine): bool
{
return $adsTxtLine instanceof Invalid && $adsTxtLine->__toString() === $this->__toString();
return $adsTxtLine instanceof Invalid && $adsTxtLine->pretty() === $this->pretty();
}

public function getComment(): ?Comment
{
return $this->comment;
}

public function getReason(): string
public function getValue(): string
{
return $this->reason;
return $this->value;
}

public function getValue(): string
public function pretty(bool $withComment = true): string
{
return $this->value;
if (!isset($this->comment)) {
return $this->value;
}

return sprintf('%s%s', $this->value, $this->comment->pretty($withComment));
}
}
60 changes: 15 additions & 45 deletions src/Lines/Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,24 @@
*
* Represents a line in the ads.txt file containing record information.
*/
class Record implements AdsTxtLineInterface
class Record extends AbstractAdsTxtLine
{
/**
* Record constructor.
*
* @param string $domain the domain associated with the record
* @param mixed $publisherId the ID of the publisher associated with the record
* @param Relationship $relationship The relationship of the record (e.g., DIRECT, RESELLER).
* @param null|mixed $certificationId the certification ID of the record (optional)
* @param string $relationship The relationship of the record (e.g., DIRECT, RESELLER).
* @param null|string $certificationId the certification ID of the record (optional)
* @param null|Comment $comment the comment associated with the record (optional)
*/
public function __construct(
private readonly string $domain,
private readonly mixed $publisherId,
private readonly Relationship $relationship,
private readonly mixed $certificationId = null,
private readonly string $relationship,
private readonly ?string $certificationId = null,
private readonly ?Comment $comment = null
) {
$this->validateDomain();
$this->validatePublisherId();
$this->validateCertificationId();
}

/**
* Get the string representation of the Record line.
*
* @return string returns the Record line as a string
*/
public function __toString(): string
{
$vendor = sprintf('%s, %s, %s', $this->domain, $this->publisherId, $this->relationship->name);

if (isset($this->certificationId)) {
$vendor = sprintf('%s, %s', $vendor, $this->certificationId);
}

if (isset($this->comment)) {
$vendor = sprintf('%s %s', $vendor, $this->comment->__toString());
}

return $vendor;
}

/**
Expand All @@ -64,7 +41,7 @@ public function __toString(): string
*/
public function equals(AdsTxtLineInterface $adsTxtLine): bool
{
return $adsTxtLine instanceof Record && $adsTxtLine->__toString() === $this->__toString();
return $adsTxtLine instanceof Record && $adsTxtLine->pretty() === $this->pretty();
}

public function getCertificationId(): mixed
Expand All @@ -87,30 +64,23 @@ public function getPublisherId(): mixed
return $this->publisherId;
}

public function getRelationship(): Relationship
public function getRelationship(): string
{
return $this->relationship;
}

private function validateCertificationId(): void
public function pretty(bool $withComment = true): string
{
if (isset($this->certificationId) && !preg_match('/^[a-f0-9]{9,16}$/', $this->certificationId)) {
throw new RecordArgumentException(sprintf('Certification authority ID "%s" is invalid. It may only contain numbers and lowercase letters, and must be 9 or 16 characters.', $this->certificationId));
}
}
$vendor = sprintf('%s, %s, %s', $this->domain, $this->publisherId, $this->relationship);

private function validateDomain(): void
{
// We check if the domain is valid and if the domain had a dot (we don't validate localhost)
if (!filter_var($this->domain, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME) || !str_contains($this->domain, '.')) {
throw new RecordArgumentException(sprintf('Domain "%s" does not appear valid.', $this->domain));
if (isset($this->certificationId)) {
$vendor = sprintf('%s, %s', $vendor, $this->certificationId);
}
}

private function validatePublisherId(): void
{
if (empty($this->publisherId) || !preg_match('/^[a-z0-9-]+$/i', $this->publisherId)) {
throw new RecordArgumentException(sprintf('Publisher ID "%s" contains invalid characters.', $this->publisherId));
if (isset($this->comment)) {
$vendor = sprintf('%s%s', $vendor, $this->comment->pretty($withComment));
}

return $vendor;
}
}
Loading

0 comments on commit bdd9db7

Please sign in to comment.