Skip to content

Commit

Permalink
Updated for Application so suport file related actions #32 & #33
Browse files Browse the repository at this point in the history
  • Loading branch information
calcinai committed Jun 20, 2015
1 parent a1fe59e commit 7735070
Show file tree
Hide file tree
Showing 10 changed files with 382 additions and 20 deletions.
5 changes: 3 additions & 2 deletions src/XeroPHP/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function loadByGUID($model, $guid) {

//Return the first (if any) element from the response.
foreach($request->getResponse()->getElements() as $element){
$object = new $class();
$object = new $class($this);
$object->fromStringArray($element);
return $object;
}
Expand Down Expand Up @@ -156,13 +156,14 @@ public function save(Object $object) {

//In this case it's new
if($object->hasGUID()) {

$method = Request::METHOD_POST;
$uri = sprintf('%s/%s', $object::getResourceURI(), $object->getGUID());

} else {
$method = Request::METHOD_PUT;
$uri = $object::getResourceURI();
//@todo, bump version so you must create objects with app context.
$object->setApplication($this);
}

if(!$object::supportsMethod($method))
Expand Down
204 changes: 204 additions & 0 deletions src/XeroPHP/Models/Accounting/Attachment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
<?php
//This class is a pseudo-model to represent an attachment. Can't be directly put ot fetched.


namespace XeroPHP\Models\Accounting;

use XeroPHP\Application;
use XeroPHP\Remote\Object;
use XeroPHP\Remote\Request;
use XeroPHP\Remote\URL;

class Attachment extends Object {

/**
* Xero Unique Identifier
*
* @property string AttachmentID
*/

/**
* @property string FileName
*/

/**
* @property string Url
*/

/**
* @property string MimeType
*/

/**
* @property int ContentLength
*/

/**
* Actual file content (binary)
*
* @var string $content
*/
private $content;

private $local_handle;

/**
* Get the GUID Property if it exists
*
* @return string
*/
static function getGUIDProperty() {
return 'AttachmentID';
}

/**
* Get a list of properties
*
* @return array
*/
static function getProperties() {
return array(
'AttachmentID' => array(false, self::PROPERTY_TYPE_STRING, null, false, false),
'FileName' => array(true, self::PROPERTY_TYPE_STRING, null, false, false),
'Url' => array(false, self::PROPERTY_TYPE_STRING, null, false, false),
'MimeType' => array(false, self::PROPERTY_TYPE_STRING, null, false, false),
'ContentLength' => array(false, self::PROPERTY_TYPE_INT, null, false, false)
);
}

/**
* Get a list of the supported HTTP Methods
*
* @return array
*/
static function getSupportedMethods() {
return array(
Request::METHOD_GET,
Request::METHOD_PUT,
Request::METHOD_POST
);
}

/**
* return the URI of the resource (if any)
*
* @return string
*/
static function getResourceURI() {
return '';
}


//Do this with a file handle please
public static function createFromLocalFile($file_name, $mime_type = null){

//Try to guess. Might be questionable on non-*nix machines
if($mime_type === null){
$finfo = new \finfo(FILEINFO_MIME_TYPE);
$info = $finfo->file($file_name);

if($info !== false){
$mime_type = $info;
}
}

$content_length = filesize($file_name);
$path_info = pathinfo($file_name);

$instance = new self();
$instance->fromStringArray(array(
'MimeType' => $mime_type,
'ContentLength' => $content_length,
'FileName' => $path_info['basename']
));
$instance->setLocalHandle(fopen($file_name, 'r'));

return $instance;
}

/**
* @return string
*/
public function getContent() {

if(!isset($this->content)){
//If it's been created locally, you can just read it back.
if(isset($this->local_handle)){
rewind($this->local_handle);
while(!feof($this->local_handle)){
$this->content .= fread($this->local_handle, 8192);
}
//Otherwise, if it can be fetched
} elseif(isset($this->_data['Url'])){
$this->content = self::downloadContent($this->_application, $this->_data['Url']);
}
}

return $this->content;

}


private static function downloadContent(Application $app, $url){

$url = new URL($app, $url);
$request = new Request($app, $url, Request::METHOD_GET);
$request->setHeader(Request::HEADER_ACCEPT, '*/*');

$request->send();

return $request->getResponse()->getResponseBody();
}


/**
* @return string
*/
public function getAttachmentID() {
return $this->_data['AttachmentID'];

}

/**
* @return int
*/
public function getContentLength() {
return $this->_data['ContentLength'];
}

/**
* @return string
*/
public function getFileName() {
return $this->_data['FileName'];
}

/**
* @return string
*/
public function getMimeType() {
return $this->_data['MimeType'];
}

/**
* @return string
*/
public function getUrl() {
return $this->_data['Url'];
}

/**
* @return mixed
*/
public function getLocalHandle() {
return $this->local_handle;
}

/**
* @param mixed $local_handle
*/
public function setLocalHandle($local_handle) {
$this->local_handle = $local_handle;
}

}
45 changes: 37 additions & 8 deletions src/XeroPHP/Remote/Object.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace XeroPHP\Remote;

use XeroPHP\Application;
use XeroPHP\Exception;
use XeroPHP\Helpers;

Expand Down Expand Up @@ -32,13 +33,6 @@ abstract class Object implements ObjectInterface {
const PROPERTY_TYPE_DATE = 'date';
const PROPERTY_TYPE_OBJECT = 'object';


public function __construct() {
$this->_dirty = array();
$this->_data = array();
$this->_associated_objects = array();
}

/**
* Container to the actual properties of the object
*
Expand All @@ -61,6 +55,30 @@ public function __construct() {
protected $_associated_objects;


/**
* Holds a ref to the application that was used to load the object, enables shorthand $object->save();
*
* @var Application $_application
*/
protected $_application;


public function __construct(Application $application = null) {
$this->_application = $application;
$this->_dirty = array();
$this->_data = array();
$this->_associated_objects = array();
}

/**
* This should be compulsory in the constructor int he future, but will have to be like this for BC until the next major version.
*
* @param Application $application
*/
public function setApplication(Application $application){
$this->_application = $application;
}

/**
* If there have been any properties changed since load
*
Expand Down Expand Up @@ -228,7 +246,6 @@ public static function castToString($type, $value) {
/**
* Cast the values to PHP types
*
* @param $property_name
* @param $type
* @param $value
* @param $php_type
Expand Down Expand Up @@ -307,6 +324,18 @@ public function validate($check_children = true) {
}


/**
* Shorthand save an object if it is instantiated with app context.
*
* @throws Exception
*/
public function save(){
if($this->_application === null){
throw new Exception('->save() is only available on objects that have an injected application context.');
}
$this->_application->save($this);
}

/**
* @param string $property
* @param self $object
Expand Down
9 changes: 9 additions & 0 deletions src/XeroPHP/Remote/ObjectInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,13 @@ static function getProperties();
* @return array
*/
static function getSupportedMethods();

/**
* return the URI of the resource (if any)
*
* @return string
*/
static function getResourceURI();


}
2 changes: 1 addition & 1 deletion src/XeroPHP/Remote/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function execute() {

$elements = array();
foreach($request->getResponse()->getElements() as $element) {
$built_element = new $from_class;
$built_element = new $from_class($this->app);
$built_element->fromStringArray($element);
$elements[] = $built_element;
}
Expand Down
8 changes: 4 additions & 4 deletions src/XeroPHP/Remote/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class Request {

private $oauth_client;

/*
* @var XeroPHP\Remote\Response;
/**
* @var \XeroPHP\Remote\Response;
*/
private $response;

Expand Down Expand Up @@ -135,8 +135,8 @@ public function getHeaders() {
return $this->headers;
}

/*
* @return XeroPHP\Remote\Response
/**
* @return \XeroPHP\Remote\Response
*/
public function getResponse() {
if(isset($this->response))
Expand Down
Loading

0 comments on commit 7735070

Please sign in to comment.