https://github.com/hiqdev/yii2-hiart
ActiveRecord for API
https://github.com/hiqdev/yii2-hiart
hacktoberfest
Last synced: about 1 year ago
JSON representation
ActiveRecord for API
- Host: GitHub
- URL: https://github.com/hiqdev/yii2-hiart
- Owner: hiqdev
- License: bsd-3-clause
- Created: 2015-05-24T15:56:47.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2024-06-14T11:08:24.000Z (about 2 years ago)
- Last Synced: 2025-04-02T10:48:31.845Z (about 1 year ago)
- Topics: hacktoberfest
- Language: PHP
- Homepage:
- Size: 415 KB
- Stars: 58
- Watchers: 14
- Forks: 14
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# HiArt
**ActiveRecord for API**
[](https://packagist.org/packages/hiqdev/yii2-hiart)
[](https://packagist.org/packages/hiqdev/yii2-hiart)
[](https://travis-ci.org/hiqdev/yii2-hiart)
[](https://scrutinizer-ci.com/g/hiqdev/yii2-hiart/)
[](https://scrutinizer-ci.com/g/hiqdev/yii2-hiart/)
[](https://www.versioneye.com/php/hiqdev:yii2-hiart/dev-master)
This Yii2 extension provides [ActiveRecord](http://en.wikipedia.org/wiki/Active_record_pattern)
implementation that allows to access remote/web API same way as you do with normal
[Yii2 DB ActiveRecord](http://www.yiiframework.com/doc-2.0/guide-db-active-record.html).
## Installation
The preferred way to install this yii2-extension is through [composer](http://getcomposer.org/download/).
```sh
composer require "hiqdev/yii2-hiart"
```
But if you prefer [Guzzle] transport use `yii2-hiart-guzzle` package:
```sh
composer require "hiqdev/yii2-hiart-guzzle"
```
## Configuration
To use this extension, configure hiart component in your application config:
```php
'components' => [
'hiart' => [
'class' => \hiqdev\hiart\rest\Connection::class,
'requestClass' => \hiqdev\hiart\auto\Request::class,
'baseUri' => 'https://site.com/api/v3/',
],
],
```
Note three main options:
- `class` specifies class that actually implements API to be accessed, **REST** in this case
- `requestClass` specifies transport implementation to be used, **auto** in this case
- `baseUri` specifies starting point of the API
### Transports
Available transports are:
- **auto** - autodetects best supported transport
- [PHP streams], the most generic fallback, included in this package
- [cURL], included in this package
- [Guzzle], provided with [yii2-hiart-guzzle](https://github.com/hiqdev/yii2-hiart-guzzle)
- [yii2-httpclient], provided with [yii2-hiart-httpclient](https://github.com/hiqdev/yii2-hiart-httpclient)
[PHP streams]: http://php.net/manual/en/book.stream.php
[cURL]: http://php.net/manual/en/book.curl.php
[Guzzle]: https://github.com/guzzle/guzzle
[yii2-httpclient]: https://github.com/yiisoft/yii2-httpclient
You can implement your own transport.
All you need is to create two classes: `Request` and `Response`, it's not difficult see available implementations.
Transport can be even non-HTTP based.
### Query builders
QueryBuilder is what actually implements an API.
We are developing `QueryBuilder`s for:
- Basic [REST API](https://en.wikipedia.org/wiki/Representational_state_transfer), included in this package
- [GitHub API](https://developer.github.com/v3/), provided with [yii2-hiart-github](https://github.com/hiqdev/yii2-hiart-github)
- [HiPanel API](https://hipanel.com/), provided with [hipanel-hiart](https://github.com/hiqdev/hipanel-hiart)
You can implement your own API.
Basically all you need is create your `QueryBuilder` class with these methods:
- `buildMethod(Query $query)`
- `buildHeaders(Query $query)`
- `buildUri(Query $query)`
- `buildQueryParams(Query $query)`
- `buildBody(Query $query)`
- `buildFormParams(Query $query)`
See available implementations and ask questions using issues on GitHub.
## Usage
Define your Model:
```php
class User extends \hiqdev\hiart\ActiveRecord
{
public function rules()
{
return [
['id', 'integer', 'min' => 1],
['login', 'string', 'min' => 2, 'max' => 32],
];
}
}
```
Note that you use general `hiqdev\hiart\ActiveRecord` class not specific for certain API.
API is specified in connection options and you don't need to change model classes when
you change API.
Then you just use your models same way as DB ActiveRecord models.
```php
$user = new User();
$user->login = 'sol';
$user->save();
$admins = User::find()->where(['type' => User::ADMIN_TYPE])->all();
```
Basically all the features of [Yii2 ActiveRecord] work if your API provides them.
[Yii2 ActiveRecord]: http://www.yiiframework.com/doc-2.0/guide-db-active-record.html
## License
This project is released under the terms of the BSD-3-Clause [license](LICENSE).
Read more [here](http://choosealicense.com/licenses/bsd-3-clause).
Copyright © 2015-2018, HiQDev (http://hiqdev.com/)
## Acknowledgments
- This project was started as fork from [Yii2 Elasticsearch](https://github.com/yiisoft/yii2-elasticsearch).