Skip to content

Commit e473fc3

Browse files
Merge pull request #42 from mailerlite/develop
v0.3.0
2 parents 6d694cd + dc625d2 commit e473fc3

24 files changed

+475
-91
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ You can find more examples and information about MailerLite API v2 here: [http:/
66

77
## Getting started
88

9-
In order to use this library you need to have at least PHP 5.6 version.
9+
In order to use this library you need to have at least PHP 7.1 version.
1010

1111
There are two ways to use MailerLite PHP SDK:
1212

@@ -20,6 +20,13 @@ Then you will need to run this simple command using CLI:
2020
composer require mailerlite/mailerlite-api-v2-php-sdk
2121
```
2222

23+
This library is built atop of [PSR-7](https://www.php-fig.org/psr/psr-7/) and
24+
[PSR-18](https://www.php-fig.org/psr/psr-18/). If you are receiving `Http\Discovery\Exception\DiscoveryFailedException` exception, you will need to run:
25+
26+
```bash
27+
composer require php-http/guzzle6-adapter
28+
```
29+
2330
##### Manual (preferable for shared hostings)
2431

2532
This way is preferable only if you are using shared hosting and do not have a possibility to use Composer. You will need to download [this archive](https://bit.ly/32jmi7M)(v0.2.3), extract it and place its contents in root folder of your project. The next step is the same as using Composer, you will need to require `vendor/autoload.php` file in your index.php and lets dive in!

composer.json

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,16 @@
55
"license": "MIT",
66
"keywords": ["mailerlite", "sdk", "email", "marketing"],
77
"type": "library",
8-
"authors": [
9-
{
10-
"name": "Justinas Pošiūnas",
11-
"email": "justinas.posiunas@mailerlite.com",
12-
"homepage": "https://mailerlite.com",
13-
"role": "Developer"
14-
}
15-
],
168
"require": {
17-
"php" : "^5.5|^7.0",
18-
"guzzlehttp/psr7": "~1.2",
19-
"php-http/httplug": "^1.0",
20-
"php-http/curl-client": "^1.4",
21-
"php-http/message": "^1.2"
9+
"php" : "^7.1",
10+
"php-http/client-common": "^2.0",
11+
"php-http/discovery": "^1.7",
12+
"nyholm/psr7": "^1.0",
13+
"ext-json": "*"
2214
},
2315
"require-dev": {
24-
"php-http/guzzle6-adapter": "^1.0",
25-
"phpunit/phpunit": "5.3.*",
26-
"mockery/mockery": "^0.9.4"
16+
"php-http/guzzle6-adapter": "^2.0",
17+
"phpunit/phpunit": "6.* | 7.* | 8.* | 9.*"
2718
},
2819
"autoload": {
2920
"psr-4": {

src/Api/Batch.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace MailerLiteApi\Api;
4+
5+
use MailerLiteApi\Common\ApiAbstract;
6+
use MailerLiteApi\Common\BatchRequest;
7+
use MailerLiteApi\Exceptions\MailerLiteSdkException;
8+
9+
/**
10+
* Class Batch
11+
*
12+
* @package MailerLiteApi\Api
13+
*/
14+
class Batch extends ApiAbstract
15+
{
16+
17+
protected $endpoint = 'batch';
18+
19+
/**
20+
* @param BatchRequest[] $requests
21+
*
22+
* @return \MailerLiteApi\Common\Collection
23+
*/
24+
public function send(array $requests)
25+
{
26+
if ( ! count($requests)) {
27+
throw new MailerLiteSdkException("Provide at least one request");
28+
}
29+
30+
foreach ($requests as &$request) {
31+
if ( ! is_object($request) || get_class($request) != BatchRequest::class) {
32+
throw new MailerLiteSdkException("All requests must be of type ".BatchRequest::class);
33+
}
34+
35+
$request = $request->toArray();
36+
}
37+
38+
$response = $this->restClient->post($this->endpoint, [
39+
'requests' => $requests,
40+
]);
41+
42+
return $response['body'];
43+
}
44+
45+
}

src/Api/Campaigns.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33
namespace MailerLiteApi\Api;
44

55
use MailerLiteApi\Common\ApiAbstract;
6+
use MailerLiteApi\Exceptions\MailerLiteSdkException;
67

8+
/**
9+
* Class Campaigns
10+
*
11+
* @package MailerLiteApi\Api
12+
*/
713
class Campaigns extends ApiAbstract {
814

915
protected $endpoint = 'campaigns';
@@ -13,11 +19,17 @@ class Campaigns extends ApiAbstract {
1319
*
1420
* @param int $campaignId
1521
* @param array $contentData
16-
* @param array $params
1722
* @return [type]
1823
*/
19-
public function addContent($campaignId, $contentData = [], $params = [])
24+
public function addContent(int $campaignId, array $contentData)
2025
{
26+
if ( ! array_key_exists('html', $contentData)) {
27+
throw new MailerLiteSdkException("HTML must be provided");
28+
}
29+
if ( ! array_key_exists('plain', $contentData)) {
30+
throw new MailerLiteSdkException("Plain text must be provided");
31+
}
32+
2133
$endpoint = $this->endpoint . '/' . $campaignId . '/content';
2234

2335
$response = $this->restClient->put($endpoint, $contentData);
@@ -59,7 +71,7 @@ public function cancel($campaignId)
5971
/**
6072
* Get collection of items
6173
* @param array $fields
62-
* @return [type]
74+
* @return \MailerLiteApi\Common\Collection
6375
*/
6476
public function get($type = 'sent', $fields = ['*'])
6577
{
@@ -78,4 +90,4 @@ public function get($type = 'sent', $fields = ['*'])
7890

7991
return $entities;
8092
}
81-
}
93+
}

src/Api/Fields.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
use MailerLiteApi\Common\ApiAbstract;
66

7+
/**
8+
* Class Fields
9+
*
10+
* @package MailerLiteApi\Api
11+
*/
712
class Fields extends ApiAbstract {
813

914
protected $endpoint = 'fields';
@@ -15,4 +20,4 @@ public function getAccountFields() {
1520
return $response['body'];
1621
}
1722

18-
}
23+
}

src/Api/Groups.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
use MailerLiteApi\Common\ApiAbstract;
66

7+
/**
8+
* Class Groups
9+
*
10+
* @package MailerLiteApi\Api
11+
*/
712
class Groups extends ApiAbstract {
813

914
protected $endpoint = 'groups';
@@ -102,4 +107,4 @@ public function importSubscribers(
102107

103108
return $response['body'];
104109
}
105-
}
110+
}

src/Api/Segments.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@
44

55
use MailerLiteApi\Common\ApiAbstract;
66

7+
/**
8+
* Class Segments
9+
*
10+
* @package MailerLiteApi\Api
11+
*/
712
class Segments extends ApiAbstract {
813

914
protected $endpoint = 'segments';
1015

11-
}
16+
}

src/Api/Settings.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
use MailerLiteApi\Common\ApiAbstract;
66

7+
/**
8+
* Class Settings
9+
*
10+
* @package MailerLiteApi\Api
11+
*/
712
class Settings extends ApiAbstract {
813

914
protected $endpoint = 'settings';
@@ -33,4 +38,4 @@ public function setDoubleOptin( $status ) {
3338
return $response['body'];
3439
}
3540

36-
}
41+
}

src/Api/Stats.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
use MailerLiteApi\Common\ApiAbstract;
66

7+
/**
8+
* Class Stats
9+
*
10+
* @package MailerLiteApi\Api
11+
*/
712
class Stats extends ApiAbstract {
813

914
protected $endpoint = 'stats';
@@ -22,4 +27,4 @@ public function getHistorical($timestamp)
2227
return $response['body'];
2328
}
2429

25-
}
30+
}

src/Api/Subscribers.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,19 @@
44

55
use MailerLiteApi\Common\ApiAbstract;
66

7+
/**
8+
* Class Subscribers
9+
*
10+
* @package MailerLiteApi\Api
11+
*/
712
class Subscribers extends ApiAbstract {
813

14+
const TYPE_ACTIVE = 'active';
15+
const TYPE_UNSUBSCRIBED = 'unsubscribed';
16+
const TYPE_BOUNCED = 'bounced';
17+
const TYPE_JUNK = 'junk';
18+
const TYPE_UNCONFIRMED = 'unconfirmed';
19+
920
protected $endpoint = 'subscribers';
1021

1122
/**
@@ -66,4 +77,29 @@ public function search($query)
6677
return $response['body'];
6778
}
6879

69-
}
80+
/**
81+
* Get all subscribers
82+
*
83+
* @param string[] $fields
84+
* @param null|string $type
85+
*
86+
* @return \MailerLiteApi\Common\Collection
87+
*/
88+
public function get($fields = ['*'], $type = null)
89+
{
90+
$params = $this->prepareParams();
91+
92+
if ( ! empty($fields) && is_array($fields) && $fields != ['*']) {
93+
$params['fields'] = $fields;
94+
}
95+
96+
if ($type !== null) {
97+
$params['type'] = $type;
98+
}
99+
100+
$response = $this->restClient->get($this->endpoint, $params);
101+
102+
return $this->generateCollection($response['body']);
103+
}
104+
105+
}

0 commit comments

Comments
 (0)