Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
petrsvihlik committed Aug 3, 2017
1 parent 161cc66 commit 96df73c
Show file tree
Hide file tree
Showing 23 changed files with 494 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -3,4 +3,4 @@ composer.phar

# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
# composer.lock
composer.lock
19 changes: 19 additions & 0 deletions .vscode/launch.json
@@ -0,0 +1,19 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 9000
},
{
"name": "Launch currently open script",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 9000
}
]
}
File renamed without changes.
21 changes: 21 additions & 0 deletions ORIGINAL_WORK_LICENSE.md
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2016 Stephen Rushing

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.
28 changes: 28 additions & 0 deletions composer.json
@@ -0,0 +1,28 @@
{
"name": "kentico/delivery-sdk-php",
"type": "library",
"license": "MIT",
"version": "0.0.1",
"description": "Kentico Cloud Delivery SDK for PHP",
"authors": [
{
"name": "Kentico",
"email": "developerscommunity@kentico.com"
},
{
"name": "Stephen Rushing",
"email": "stephen.rushing@esiteful.com"
}
],
"require": {
"nategood/httpful": "^0.2.20"
},
"require-dev": {
"phpdocumentor/phpdocumentor": "2.*"
},
"autoload": {
"psr-4": {
"KenticoCloud\\": "src/KenticoCloud"
}
}
}
3 changes: 3 additions & 0 deletions index.php
@@ -0,0 +1,3 @@
<?php

header('Location: http://'.$_SERVER['HTTP_HOST'].'/site');
13 changes: 13 additions & 0 deletions phpunit.xml
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="tests/bootstrap.php">
<testsuites>
<testsuite name="unit">
<directory>tests/Unit</directory>
</testsuite>
</testsuites>
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>
88 changes: 88 additions & 0 deletions src/KenticoCloud/Delivery/Client.php
@@ -0,0 +1,88 @@
<?php

namespace KenticoCloud\Delivery;

class Client {

const MODE_PREVIEW = 1;
const MODE_PUBLISHED = 2;

public $projectID = null;
public $apiKey = null;
public $uri = 'https://deliver.kenticocloud.com/';
public $_debug = true;
public $lastRequest = null;
public $mode = null;

public function __construct($projectID, $apiKey = null){
$this->projectID = $projectID;
$this->apiKey = $apiKey;
$self = get_class($this);
$this->mode = $self::MODE_PUBLISHED;
}

public function buildURL($endpoint, $query = NULL){
$segments = array(
trim($this->uri, '/'),
trim($this->projectID, '/'),
trim($endpoint, '/')
);
$url = implode('/', $segments);
if(is_array($query)) $query = http_build_query($query);
if(is_string($query)) $url = rtrim($url, '?') . '?' . ltrim($query, '?');

return $url;
}

public function setRequestAuthorization($request){
$request->addHeader('Authorization', 'Bearer ' . $this->apiKey);
return $request;
}

public function prepRequest($request){
$request->_debug = $this->_debug;
$request->mime('json');
$request = $this->setRequestAuthorization($request);
$this->lastRequest = $request;
return $request;
}

public function getRequest($endpoint, $params = null){
$uri = $this->buildURL($endpoint, $params);

$request = \Httpful\Request::get($uri);
$request = $this->prepRequest($request);
return $request;
}

public function send($request = null){
if(!$request) $request = $this->lastRequest;
else $this->lastRequest = $request;
$response = $request->send();
$this->lastResponse = $response;
return $response;
}

public function getItems($params){
$request = $this->getRequest('items', $params);
$response = $this->send();

$items = Models\ContentItems::create($response->body);

return $items;
}

public function getItem($params){
$params['limit'] = 1;
$results = $this->getItems($params);

if(!isset($results->items) || !count($results->items)) return null;

$item = reset($results->items);
return $item;
}




}
13 changes: 13 additions & 0 deletions src/KenticoCloud/Delivery/ContentElementTypesMap.php
@@ -0,0 +1,13 @@
<?php

namespace KenticoCloud\Delivery;

class ContentElementTypesMap extends TypesMap {

public static $types = array(
'asset' => '\KenticoCloud\Delivery\Models\Asset'
);

public static $defaultTypeClass = '\KenticoCloud\Delivery\Models\ContentItemElement';

}
13 changes: 13 additions & 0 deletions src/KenticoCloud/Delivery/ContentTypesMap.php
@@ -0,0 +1,13 @@
<?php

namespace KenticoCloud\Delivery;

class ContentTypesMap extends TypesMap {

public static $types = array(

);

public static $defaultTypeClass = '\KenticoCloud\Delivery\Models\ContentItem';

}
16 changes: 16 additions & 0 deletions src/KenticoCloud/Delivery/Helpers/Helper.php
@@ -0,0 +1,16 @@
<?php

namespace KenticoCloud\Delivery\Helpers;

class Helper {

static protected $instance = null;
static public function getInstance(){
$class = get_called_class();
if(!$class::$instance){
$class::$instance = new $class;
}
return $class::$instance;
}

}
18 changes: 18 additions & 0 deletions src/KenticoCloud/Delivery/Helpers/TextHelper.php
@@ -0,0 +1,18 @@
<?php

namespace KenticoCloud\Delivery\Helpers;

class TextHelper extends Helper {

//http://syframework.alwaysdata.net/decamelize
function decamelize($input, $separator = '_') {
return strtolower(preg_replace(array('/([a-z\d])([A-Z])/', '/([^_])([A-Z][a-z])/'), '$1'. $separator .'$2', $input));
}

//http://stackoverflow.com/a/33122760/3363709
function camelize($input, $separator = '_')
{
return str_replace($separator, '', ucwords($input, $separator));
}

}
50 changes: 50 additions & 0 deletions src/KenticoCloud/Delivery/Models/ContentItem.php
@@ -0,0 +1,50 @@
<?php

namespace KenticoCloud\Delivery\Models;


class ContentItem extends Model {

public $system = null;
public $elements = null;

public function setSystem($system){
$this->system = ContentItemSystem::create($system);
return $this;
}

public function getModularContent($huh){

}

public function getElementValue($name){
if(!$this->elements) return null;
foreach($this->elements as $element){

}
return null;
}

public function getString($name){
$value = $this->getElementValue($name);
return (string)$value;
}

public function getNumber($name){
$value = $this->getElementValue($name);
return floatval($value) !== intval($value) ? floatval($value) : intval($value);
}

public function getDateTime($name, $format = NULL){
$value = $this->getElementValue($name);
if(is_string($value)){
$value = strtotime($value);
}
if($format){
$value = date($format, $value);
}
return $value;
}


}
11 changes: 11 additions & 0 deletions src/KenticoCloud/Delivery/Models/ContentItemElement.php
@@ -0,0 +1,11 @@
<?php

namespace KenticoCloud\Delivery\Models;


class ContentItemElement extends Model {

public $type = null;
public $value = null;

}
28 changes: 28 additions & 0 deletions src/KenticoCloud/Delivery/Models/ContentItemSystem.php
@@ -0,0 +1,28 @@
<?php

namespace KenticoCloud\Delivery\Models;


class ContentItemSystem extends Model {

public $id = null;
public $name = null;
public $codename = null;
public $type = null;
public $sitemap_locations = null;
public $last_modified = null;

public function getLastModified($format = null){
if(!$format) return $this->last_modified;
return date($format, $this->last_modified);
}

public function setLastModified($value){
if(is_string($value)){
$value = strtotime($value);
}
$this->last_modified = $value;
return $this;
}

}
30 changes: 30 additions & 0 deletions src/KenticoCloud/Delivery/Models/ContentItems.php
@@ -0,0 +1,30 @@
<?php

namespace KenticoCloud\Delivery\Models;

use \KenticoCloud\Delivery\ContentTypesMap;

class ContentItems extends Model {

public $items = null;
public $pagination = null;

public function setItems($value){
$this->items = array();
foreach($value as $item){
if(isset($item->system->type)){
$class = ContentTypesMap::getTypeClass($item->system->type);
}else{
$class = ContentTypesMap::$defaultTypeClass;
}
$this->items[] = $class::create($item);
}
return $this;
}

public function setPagination($value){
$this->pagination = Pagination::create($value);
return $this;
}

}
12 changes: 12 additions & 0 deletions src/KenticoCloud/Delivery/Models/Elements/Asset.php
@@ -0,0 +1,12 @@
<?php

namespace KenticoCloud\Delivery\Models\Elements;


class Asset extends ContentItemElement {

public $name = null;
public $size = null;
public $url = null;

}

0 comments on commit 96df73c

Please sign in to comment.