Skip to content

Commit c4691e2

Browse files
Merge pull request #15 from ButterCMS/feature/api-changes
feat: Update due to API changes
2 parents 75b1a42 + 70afe0a commit c4691e2

File tree

6 files changed

+194
-2
lines changed

6 files changed

+194
-2
lines changed

Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM php:8.2-cli
2+
3+
WORKDIR /app
4+
COPY . .
5+
6+
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
7+
RUN composer install
8+
9+
ENV API_BASE_URL=https://api.buttercms.com/v2
10+
ENV API_KEY=your_api_key
11+
12+
CMD ["php", "demo/demo.php"]

demo/demo.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
require_once 'vendor/autoload.php';
4+
5+
use ButterCMS\ButterCMS;
6+
7+
$apiKey = getenv('API_KEY');
8+
if (empty($apiKey)) {
9+
die("API_KEY environment variable is not set\n");
10+
}
11+
12+
$client = new ButterCMS($apiKey);
13+
14+
try {
15+
$params = ['preview' => 1];
16+
$page = $client->fetchPage('*', 'test-page-1', $params);
17+
echo "Page Slug: " . $page->getSlug() . "\n";
18+
echo "Page Status: " . $page->getStatus() . "\n";
19+
echo "Page Scheduled Date: " . $page->getScheduled() . "\n";
20+
} catch (Exception $e) {
21+
echo "Error fetching page: " . $e->getMessage() . "\n";
22+
}
23+
24+
try {
25+
$postResponse = $client->fetchPost('test-blog-post');
26+
$post = $postResponse->getPost();
27+
echo "Post Slug: " . $post->getSlug() . "\n";
28+
echo "Post Status: " . $post->getStatus() . "\n";
29+
echo "Post Scheduled Date: " . $post->getScheduled() . "\n";
30+
} catch (Exception $e) {
31+
echo "Error fetching post: " . $e->getMessage() . "\n";
32+
}
33+
34+
?>

src/ButterCMS/ButterCMS.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
class ButterCMS
1919
{
2020
protected const VERSION = '3.0.1';
21-
protected const API_ROOT_URL = 'https://api.buttercms.com/v2/';
21+
protected const API_DEFAULT_URL = 'https://api.buttercms.com/v2/';
2222

2323
protected $maxRetryCount = 1;
2424
protected $readAuthToken;
@@ -71,12 +71,13 @@ protected function request(string $method, $url, $query = [], $data = [], $tryCo
7171
}
7272
}
7373

74+
$apiRootUrl = getenv('API_BASE_URL') ?: self::API_DEFAULT_URL;
7475
try {
7576
$options = array_filter([
7677
'query' => $query,
7778
'json' => $data,
7879
]);
79-
$response = $client->$method(self::API_ROOT_URL . $url, $options);
80+
$response = $client->$method($apiRootUrl . $url, $options);
8081
} catch (BadResponseException $e) {
8182
$httpCode = (int)$e->getResponse()->getStatusCode();
8283
if ($tryCount < $this->maxRetryCount && $httpCode !== 404) {

src/ButterCMS/Model/Page.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,57 @@ class Page extends Model
99
protected $published;
1010
protected $updated;
1111
protected $fields;
12+
protected $status;
13+
protected $scheduled;
14+
15+
public function getSlug()
16+
{
17+
return $this->slug;
18+
}
19+
20+
public function getPageType()
21+
{
22+
return $this->page_type;
23+
}
24+
25+
public function getPublished()
26+
{
27+
return $this->published;
28+
}
29+
30+
public function getUpdated()
31+
{
32+
return $this->updated;
33+
}
34+
35+
public function getStatus()
36+
{
37+
return $this->status;
38+
}
39+
40+
public function getScheduled()
41+
{
42+
return $this->scheduled;
43+
}
1244

1345
public function getField($fieldName, $default = null)
1446
{
1547
return isset($this->fields[$fieldName]) ? $this->fields[$fieldName] : $default;
1648
}
49+
50+
public function isPublished()
51+
{
52+
return 'published' === $this->status;
53+
}
54+
55+
public function isScheduled()
56+
{
57+
return 'scheduled' === $this->status;
58+
}
59+
60+
public function isDraft()
61+
{
62+
return 'draft' === $this->status;
63+
}
1764
}
65+

src/ButterCMS/Model/Post.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class Post extends Model
99
protected $published;
1010
protected $created;
1111
protected $status;
12+
protected $scheduled;
1213
protected $title;
1314
protected $body;
1415
protected $summary;
@@ -46,8 +47,99 @@ public function __construct(array $data)
4647
parent::__construct($data);
4748
}
4849

50+
public function getSlug()
51+
{
52+
return $this->slug;
53+
}
54+
55+
public function getUrl()
56+
{
57+
return $this->url;
58+
}
59+
60+
public function getPublished()
61+
{
62+
return $this->published;
63+
}
64+
65+
public function getCreated()
66+
{
67+
return $this->created;
68+
}
69+
70+
public function getStatus()
71+
{
72+
return $this->status;
73+
}
74+
75+
public function getScheduled()
76+
{
77+
return $this->scheduled;
78+
}
79+
80+
public function getTitle()
81+
{
82+
return $this->title;
83+
}
84+
85+
public function getBody()
86+
{
87+
return $this->body;
88+
}
89+
90+
public function getSummary()
91+
{
92+
return $this->summary;
93+
}
94+
95+
public function getSeoTitle()
96+
{
97+
return $this->seo_title;
98+
}
99+
100+
public function getMetaDescription()
101+
{
102+
return $this->meta_description;
103+
}
104+
105+
public function getAuthor()
106+
{
107+
return $this->author;
108+
}
109+
110+
public function getCategories()
111+
{
112+
return $this->categories;
113+
}
114+
115+
public function getTags()
116+
{
117+
return $this->tags;
118+
}
119+
120+
public function getFeaturedImage()
121+
{
122+
return $this->featured_image;
123+
}
124+
125+
public function getFeaturedImageAlt()
126+
{
127+
return $this->featured_image_alt;
128+
}
129+
49130
public function isPublished()
50131
{
51132
return 'published' === $this->status;
52133
}
134+
135+
public function isScheduled()
136+
{
137+
return 'scheduled' === $this->status;
138+
}
139+
140+
public function isDraft()
141+
{
142+
return 'draft' === $this->status;
143+
}
53144
}
145+

src/ButterCMS/Model/PostResponse.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,9 @@ public function __construct(array $dataArray)
1515

1616
parent::__construct($dataArray);
1717
}
18+
19+
public function getPost()
20+
{
21+
return $this->post;
22+
}
1823
}

0 commit comments

Comments
 (0)