Skip to content

Commit

Permalink
first init
Browse files Browse the repository at this point in the history
fix composer.json
  • Loading branch information
arminulrich committed Aug 13, 2019
0 parents commit 227bfd7
Show file tree
Hide file tree
Showing 11 changed files with 323 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Changelog

All notable changes to `placid-php` will be documented in this file

## 1.0.0 - 201X-XX-XX

- initial release
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) Armin Ulrich <armin@nifty.at>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@


The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
31 changes: 31 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "placidapp/placid-php",
"description": "PHP Wrapper around the Placid.app REST API",
"keywords": [
"placid-php"
],
"homepage": "https://github.com/placidapp/placid-php",
"license": "MIT",
"authors": [
{
"name": "Armin Ulrich",
"email": "armin@nifty.at",
"homepage": "https://placid.app",
"role": "Developer"
}
],
"require": {
"php": "^7.1"
},
"require-dev": {
},
"autoload": {
"psr-4": {
"Placid\\PHP\\": "src"
}
},
"config": {
"sort-packages": true
}

}
35 changes: 35 additions & 0 deletions src/Element/Browserframe.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Placid\Element;

class Browserframe extends Element
{
protected $payload = [
'url' => null, // - text field
'image' => null // - image field
];

public function url($text)
{
$this->payload['url'] = $this->generateTextPayload($text);
return $this;
}

public function imageFromWebsite($website_url)
{
$this->payload['image'] = $this->generateImagePayload(
"website",
$website_url
);
return $this;
}

public function imageFromUrl($website_url)
{
$this->payload['image'] = $this->generateImagePayload(
"link",
$website_url
);
return $this;
}
}
35 changes: 35 additions & 0 deletions src/Element/Element.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Placid\Element;

use Illuminate\Contracts\Support\Arrayable;

abstract class Element implements Arrayable
{
protected $payload;

public function toArray()
{
return $this->payload;
}

protected function generateTextPayload($text)
{
return $text;
}

protected function generateColorPayload($hex)
{
return [
'hex' => $hex
];
}

protected function generateImagePayload($type, $website_url)
{
return [
'imageSrc' => $type,
'imageUrl' => $website_url
];
}
}
28 changes: 28 additions & 0 deletions src/Element/Picture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Placid\Element;

class Picture extends Element
{
protected $payload = [
'image' => null
];

public function imageFromWebsite($website_url)
{
$this->payload['image'] = $this->generateImagePayload(
"website",
$website_url
);
return $this;
}

public function imageFromUrl($website_url)
{
$this->payload['image'] = $this->generateImagePayload(
"link",
$website_url
);
return $this;
}
}
17 changes: 17 additions & 0 deletions src/Element/Rectangle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Placid\Element;

class Rectangle extends Element
{
protected $payload = [
'backgroundColor' => null
];

public function backgroundColor($hexcode)
{
$this->payload['backgroundColor'] = $this->generateColorPayload(
$hexcode
);
}
}
23 changes: 23 additions & 0 deletions src/Element/Text.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Placid\Element;

class Text extends Element
{
protected $payload = [
'text' => null,
'color' => null
];

public function text($text)
{
$this->payload['text'] = $this->generateTextPayload($text);
return $this;
}

public function color($hexcode)
{
$this->payload['color'] = $this->generateColorPayload($hexcode);
return $this;
}
}
32 changes: 32 additions & 0 deletions src/GeneratedImage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Placid;

class GeneratedImage
{
private $imageUrl;
private $id;
private $status;

public function __construct($id, $imageUrl, $status)
{
$this->id = $id;
$this->imageUrl = $imageUrl;
$this->status = $status;
}

public function getImageUrl()
{
return $this->imageUrl;
}

public function getId()
{
return $this->id;
}

public function getStatus()
{
return $this->status;
}
}
91 changes: 91 additions & 0 deletions src/Template.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace Placid;

use Exception;
use Placid\Element\Browserframe;
use Placid\Element\Picture;
use Placid\Element\Rectangle;
use Placid\Element\Text;

class Template
{
private $apiKey;
private $successWebhook = null;
private $uuid;
private $fields = [];
private $apiUrl = "http://placid.test/api/rest";

public function __construct($uuid, $apiKey = null)
{
$this->uuid = $uuid;
$this->fields = collect();
$this->apiKey = $apiKey;
}

public function successWebhook($webhook_url)
{
$this->successWebhook = $webhook_url;
}

public function elementPicture($string): Picture
{
return $this->fields[$string] = new Picture($string);
}

public function elementText($string): Text
{
return $this->fields[$string] = new Text($string);
}

public function elementRectangle($string): Rectangle
{
return $this->fields[$string] = new Rectangle($string);
}

public function elementBrowserframe($string): Browserframe
{
return $this->fields[$string] = new Browserframe($string);
}

public function getImage(): GeneratedImage
{
$payload = [
'webhook_success' => $this->successWebhook,
'fields' => $this->fields->toArray()
];

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $this->getRequestUrl($this->uuid));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));

$headers = array();
$headers[] = 'User-Agent: placidapp/placid-phpV0.1';
$headers[] = 'Authorization: Bearer ' . $this->apiKey;
$headers[] = 'Content-Type: application/json';
$headers[] = 'Accept: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
throw new Exception(curl_error($ch));
}
curl_close($ch);

$json = json_decode($result, true);

return new GeneratedImage(
$json['id'],
$json['image_url'],
$json['status']
);
}

private function getRequestUrl($uuid)
{
return "{$this->apiUrl}/{$uuid}";
}
}

0 comments on commit 227bfd7

Please sign in to comment.