Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MAL Articles #563

Open
wants to merge 4 commits into
base: 5.0.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![Jikan](https://i.imgur.com/ccx3pxo.png)](#jikan---unofficial-myanimelistnet-php-api)
[![Jikan](https://i.imgur.com/WY8KuOp.png)](#jikan---unofficial-myanimelistnet-php-api)

# Jikan - Unofficial MyAnimeList.net PHP API

Expand Down
6 changes: 3 additions & 3 deletions src/Helper/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ class Parser
* @return Crawler
* @throws \InvalidArgumentException
*/
public static function removeChildNodes(Crawler $crawler): Crawler
public static function removeChildNodes(Crawler $crawler, bool $removeAllNodes = false): Crawler
{
if (!$crawler->count()) {
return $crawler;
}
$crawler->children()->each(
function (Crawler $crawler) {
function (Crawler $crawler) use ($removeAllNodes) {
$node = $crawler->getNode(0);
if ($node === null || $node->nodeType === 3 || \in_array($node->nodeName, self::ALLOWED_NODES, true)) {
if ($node === null || $node->nodeType === 3 || \in_array($node->nodeName, $removeAllNodes ? [] : self::ALLOWED_NODES, true)) {
return;
}
$node->parentNode->removeChild($node);
Expand Down
71 changes: 71 additions & 0 deletions src/Model/Article/ArticleList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Jikan\Model\Article;

use Jikan\Model\Common\Collection\Pagination;
use Jikan\Model\Common\Collection\Results;
use Jikan\Parser;

/**
* Class ArticleList
*/
class ArticleList extends Results implements Pagination
{
/**
* @var bool
*/
private $hasNextPage = false;

/**
* @var int
*/
private $lastVisiblePage = 1;


/**
* @param Parser\Article\ArticleListParser $parser
* @return self
*/
public static function fromParser(Parser\Article\ArticleListParser $parser): self
{
$instance = new self();

$instance->results = $parser->getResults();
$instance->lastVisiblePage = $parser->getLastVisiblePage();
$instance->hasNextPage = $parser->getHasNextPage();

return $instance;
}

/**
* @return static
*/
public static function mock(): self
{
return new self();
}

/**
* @return array
*/
public function getResults(): array
{
return $this->results;
}

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

/**
* @return int
*/
public function getLastVisiblePage(): int
{
return $this->lastVisiblePage;
}
}
154 changes: 154 additions & 0 deletions src/Model/Article/ArticleListItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?php

namespace Jikan\Model\Article;

use Jikan\Model\Resource\NewsImageResource\NewsImageResource;
use Jikan\Model\Resource\WrapImageResource\WrapImageResource;
use Jikan\Parser\Article\ArticleListItemParser;

/**
* Class ArticleListItem
*
* @package Jikan\Model
*/
class ArticleListItem
{
/**
* @var int|null
*/
private int|null $malId;

/**
* @var string
*/
private string $url;

/**
* @var string
*/
private string $title;

/**
* @var string
*/
private string $authorUsername;

/**
* @var string
*/
private string $authorUrl;

/**
* @var WrapImageResource
*/
private WrapImageResource $images;

/**
* @var int
*/
private int $views;

/**
* @var string
*/
private string $excerpt;

/**
* @var array
*/
private array $tags;

/**
* @param ArticleListItemParser $parser
*
* @return ArticleListItem
* @throws \InvalidArgumentException
*/
public static function fromParser(ArticleListItemParser $parser): ArticleListItem
{
$instance = new self();
$instance->malId = $parser->getMalId();
$instance->url = $parser->getUrl();
$instance->title = $parser->getTitle();
$instance->views = $parser->getViews();
$instance->tags = $parser->getTags();
$instance->authorUsername = $parser->getAuthor()->getName();
$instance->authorUrl = $parser->getAuthor()->getUrl();
$instance->images = WrapImageResource::factory($parser->getImageUrl());
$instance->excerpt = $parser->getExcerpt();

return $instance;
}

/**
* @return int|null
*/
public function getMalId(): ?int
{
return $this->malId;
}

/**
* @return string
*/
public function getUrl(): string
{
return $this->url;
}

/**
* @return string
*/
public function getTitle(): string
{
return $this->title;
}

/**
* @return string
*/
public function getAuthorUsername(): string
{
return $this->authorUsername;
}

/**
* @return string
*/
public function getAuthorUrl(): string
{
return $this->authorUrl;
}

/**
* @return NewsImageResource
*/
public function getImages(): NewsImageResource
{
return $this->images;
}

/**
* @return int
*/
public function getViews(): int
{
return $this->views;
}

/**
* @return string
*/
public function getExcerpt(): string
{
return $this->excerpt;
}

/**
* @return array
*/
public function getTags(): array
{
return $this->tags;
}
}
41 changes: 41 additions & 0 deletions src/Model/Article/PinnedArticleList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Jikan\Model\Article;

use Jikan\Model\Common\Collection\Results;
use Jikan\Parser;

/**
* Class PinnedArticleList
*/
class PinnedArticleList extends Results
{
/**
* @param Parser\Article\PinnedArticleListParser $parser
* @return self
*/
public static function fromParser(Parser\Article\PinnedArticleListParser $parser): self
{
$instance = new self();

$instance->results = $parser->getResults();

return $instance;
}

/**
* @return static
*/
public static function mock(): self
{
return new self();
}

/**
* @return array
*/
public function getResults(): array
{
return $this->results;
}
}
Loading
Loading