Skip to content

Commit

Permalink
Resolve category lifecycle (#17)
Browse files Browse the repository at this point in the history
* Resolve category lifecycle

* Shorten php.xml.dist file
  • Loading branch information
cable8mm committed Mar 5, 2024
1 parent ef0c6a6 commit b598b3a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
16 changes: 5 additions & 11 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.0/phpunit.xsd"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheDirectory=".phpunit.cache"
executionOrder="depends,defects"
beStrictAboutCoverageMetadata="true"
beStrictAboutOutputDuringTests="true"
failOnRisky="true"
failOnWarning="true"
cacheResult="false">

colors="true"
testdox="true">
<testsuites>
<testsuite name="default">
<directory>tests</directory>
</testsuite>
</testsuites>

<source restrictDeprecations="true" restrictNotices="true" restrictWarnings="true">
<source>
<include>
<directory>src</directory>
</include>
</source>
</phpunit>
</phpunit>
26 changes: 15 additions & 11 deletions src/Models/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class Article

public ?string $categories = null;

private $resolveCategories;

public string $body;

private $bodyCallbacks;
Expand Down Expand Up @@ -63,6 +65,18 @@ private function slug(): void
private function categories(): void
{
$this->categories = $this->row[$this->map['categories']];

if (! is_null($this->resolveCategories)) {
if (is_null($this->categories)) {
throw new InvalidArgumentException($this->slug.' slug\'s category must exist.');
}

if (! in_array($this->categories, array_keys($this->resolveCategories))) {
throw new InvalidArgumentException($this->categories.' is an invalid category. Categories must include one of '.implode(',', array_keys($this->resolveCategories)));
}

$this->categories = $this->resolveCategories[$this->categories];
}
}

private function body(): void
Expand Down Expand Up @@ -140,17 +154,7 @@ public function setAddHours(int $hours): static

public function resolveCategories(?array $categories = null): static
{
if (! is_null($categories)) {
if (is_null($this->categories)) {
throw new InvalidArgumentException($this->slug.' slug\'s category must exist.');
}

if (! in_array($this->categories, array_keys($categories))) {
throw new InvalidArgumentException($this->categories.' is an invalid category. Categories must include one of '.implode(',', array_keys($categories)));
}

$this->categories = $categories[$this->categories];
}
$this->resolveCategories = $categories;

return $this;
}
Expand Down

0 comments on commit b598b3a

Please sign in to comment.