Skip to content

Commit ac50015

Browse files
committed
init
0 parents  commit ac50015

18 files changed

+855
-0
lines changed

.editorconfig

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 4
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = false
10+
11+
[*.{vue,js,scss}]
12+
charset = utf-8
13+
indent_style = space
14+
indent_size = 2
15+
end_of_line = lf
16+
insert_final_newline = true
17+
trim_trailing_whitespace = true
18+
19+
[*.md]
20+
trim_trailing_whitespace = false

.gitattributes

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
* text=auto
2+
3+
/tests export-ignore
4+
.gitattributes export-ignore
5+
.gitignore export-ignore
6+
.scrutinizer.yml export-ignore
7+
.travis.yml export-ignore
8+
phpunit.php export-ignore
9+
phpunit.xml.dist export-ignore
10+
phpunit.xml export-ignore
11+
.php_cs export-ignore

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*.DS_Store
2+
*.idea
3+
/vendor
4+
/coverage
5+
sftp-config.json
6+
composer.lock
7+
.subsplit
8+
.php_cs.cache

.php_cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
$header = <<<EOF
3+
This file is part of the littlesqx/douban-book.
4+
5+
(c) littlesqx <[email protected]>
6+
7+
This source file is subject to the MIT license that is bundled.
8+
EOF;
9+
10+
return PhpCsFixer\Config::create()
11+
->setRiskyAllowed(true)
12+
->setRules(array(
13+
'@Symfony' => true,
14+
'header_comment' => array('header' => $header),
15+
'array_syntax' => array('syntax' => 'short'),
16+
'ordered_imports' => true,
17+
'no_useless_else' => true,
18+
'no_useless_return' => true,
19+
'php_unit_construct' => true,
20+
'php_unit_strict' => true,
21+
))
22+
->setFinder(
23+
PhpCsFixer\Finder::create()
24+
->exclude('vendor')
25+
->in(__DIR__)
26+
)
27+
;

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<h1 align="center"> douban-book </h1>
2+
3+
<p align="center"> A book SDK.</p>
4+
5+
6+
## Installing
7+
8+
```shell
9+
$ composer require littlesqx/douban-book -vvv
10+
```
11+
12+
## Usage
13+
14+
TODO
15+
16+
## Contributing
17+
18+
You can contribute in one of three ways:
19+
20+
1. File bug reports using the [issue tracker](https://github.com/littlesqx/douban-book/issues).
21+
2. Answer questions or fix bugs on the [issue tracker](https://github.com/littlesqx/douban-book/issues).
22+
3. Contribute new features or update the wiki.
23+
24+
_The code contribution process is not very formal. You just need to make sure that you follow the PSR-0, PSR-1, and PSR-2 coding guidelines. Any new code contributions must be accompanied by unit tests where applicable._
25+
26+
## Thanks
27+
28+
- [豆瓣 API](https://developers.douban.com)
29+
30+
- [overtrue](https://github.com/overtrue)
31+
32+
## License
33+
34+
MIT

composer.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "littlesqx/douban-book",
3+
"description": "A book SDK.",
4+
"license": "MIT",
5+
"authors": [
6+
{
7+
"name": "littlesqx",
8+
"email": "[email protected]"
9+
}
10+
],
11+
"require": {
12+
"php": ">=7.1.0",
13+
"guzzlehttp/guzzle": "^6.3"
14+
},
15+
"require-dev": {
16+
"phpunit/phpunit": "^7.3",
17+
"mockery/mockery": "^1.1"
18+
},
19+
"autoload": {
20+
"psr-4": {
21+
"Littlesqx\\Book\\": "src"
22+
},
23+
"files": [
24+
"src/helper.php"
25+
]
26+
}
27+
}

phpunit.xml.dist

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false">
11+
<testsuites>
12+
<testsuite name="Application Test Suite">
13+
<directory>./tests/</directory>
14+
</testsuite>
15+
</testsuites>
16+
<filter>
17+
<whitelist>
18+
<directory suffix=".php">src/</directory>
19+
</whitelist>
20+
</filter>
21+
</phpunit>

src/.gitkeep

Whitespace-only changes.

src/Application.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the littlesqx/douban-book.
5+
*
6+
* (c) littlesqx <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Littlesqx\Book;
13+
14+
use GuzzleHttp\Client;
15+
use Littlesqx\Book\Entity\Entity;
16+
use Littlesqx\Book\Exception\HttpException;
17+
use Littlesqx\Book\Exception\InvalidArgumentException;
18+
19+
class Application
20+
{
21+
protected $httpOptions = [];
22+
23+
protected $requestUrl = 'https://api.douban.com/v2/book/';
24+
25+
/**
26+
* set http client options.
27+
*
28+
* @param array $httpOptions
29+
* @return $this
30+
*/
31+
public function setHttpOptions(array $httpOptions)
32+
{
33+
$this->httpOptions = $httpOptions;
34+
return $this;
35+
}
36+
37+
/**
38+
* get http client options.
39+
*
40+
* @return array
41+
*/
42+
public function getHttpOptions(): array
43+
{
44+
return $this->httpOptions;
45+
}
46+
47+
48+
/**
49+
* get a http client.
50+
*
51+
* @return Client
52+
*/
53+
public function getHttpClient(): Client
54+
{
55+
return new Client($this->httpOptions);
56+
}
57+
58+
/**
59+
* get a book by isbn code.
60+
*
61+
* @param string $isbn
62+
* @return Entity
63+
* @throws HttpException
64+
* @throws InvalidArgumentException
65+
*/
66+
public function getBook(string $isbn):? Entity
67+
{
68+
if (strlen($isbn) !== 13 && strlen($isbn) !== 10) {
69+
throw new InvalidArgumentException('Invalid isbn code(isbn10 or isbn13): ' . $isbn);
70+
}
71+
$queryParams = ['isbn' => $isbn];
72+
try {
73+
$response = $this->getHttpClient()->get(
74+
$this->requestUrl . array_to_path($queryParams)
75+
);
76+
if (200 === $response->getStatusCode()) {
77+
return BookFactory::make($response->getBody()->getContents());
78+
}
79+
} catch (\Exception $e) {
80+
throw new HttpException($e->getMessage(), $e->getCode(), $e);
81+
}
82+
}
83+
84+
}

src/BookFactory.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the littlesqx/douban-book.
5+
*
6+
* (c) littlesqx <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Littlesqx\Book;
13+
14+
15+
use Littlesqx\Book\Entity\Book;
16+
use Littlesqx\Book\Entity\Entity as EntityInterface;
17+
18+
class BookFactory
19+
{
20+
/**
21+
* make a book entity.
22+
*
23+
* @param $params
24+
* @return EntityInterface|null
25+
*/
26+
public static function make($params):? EntityInterface
27+
{
28+
$params = \json_decode($params, true);
29+
if (!isset($params['title'])) {
30+
return null;
31+
}
32+
$book = new Book();
33+
$book->setIsbn($params['isbn13'])
34+
->setTitle($params['title'])
35+
->setSubtitle($params['subtitle'])
36+
->setAuthor($params['author'])
37+
->setAuthorIntro($params['author_intro'])
38+
->setPrice($params['price'])
39+
->setCatalog($params['catalog'])
40+
->setPublicationDate($params['pubdate'])
41+
->setPublisher($params['publisher'])
42+
->setSummary($params['summary'])
43+
->setCover($params['images']['large'])
44+
->setTags(array_column($params['tags'], 'name'));
45+
return $book;
46+
}
47+
}

0 commit comments

Comments
 (0)