Skip to content

Commit

Permalink
Updated Services/Ident* models. Added Smart models with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakub Elias committed May 13, 2015
1 parent 5b5eb23 commit a2be050
Show file tree
Hide file tree
Showing 26 changed files with 610 additions and 79 deletions.
12 changes: 12 additions & 0 deletions lib/secucard/client/base/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ abstract class BaseModel
* Date type constants
*/
const DATA_TYPE_ARRAY = 'array';
const DATA_TYPE_ARRAY_SUBOBJECT = 'array_subobject';
const DATA_TYPE_BOOLEAN = 'boolean';
const DATA_TYPE_DATE = 'date';
const DATA_TYPE_DATETIME = 'datetime';
Expand Down Expand Up @@ -172,6 +173,17 @@ protected function setAttribute($name, $value)
$this->_attributes[$name] = array($value);
}
break;
case self::DATA_TYPE_ARRAY_SUBOBJECT:
if (!empty($value) && is_array($value)) {
// we have a subobject inside an array. We will just validate the $value and set the array to _attributes
$class_name = '\\secucard\\models\\' . $definition['category'] . '\\' . $definition['model'];
$attribute_obj = new $class_name();
$attribute_obj->initValues($value);
$this->_attributes[$name] = $value;
} else {
$this->_attributes[$name] = array($value);
}
break;
case self::DATA_TYPE_BOOLEAN:
$this->_attributes[$name] = null;
if ($value === true || $value === false) {
Expand Down
38 changes: 23 additions & 15 deletions lib/secucard/client/base/MainModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ abstract class MainModel extends BaseModel

/**
* Api client used for lazy loading
* @var \secucard\client\api\Client
* @var \secucard\Client
*/
protected $client;

Expand Down Expand Up @@ -94,7 +94,8 @@ public function __get($name)
// TODO recognize if object is loaded , if not, then load it!
if (!$this->initialized) {
if (!isset($this->_attributes[$name]) && !empty($this->_attribute_defs[$name])
|| !isset($this->_related[$name]) && !empty($this->_relations[$name])) {
|| !isset($this->_related[$name]) && !empty($this->_relations[$name])
) {
// lazy load current object!
$this->lazyLoadSelf();
}
Expand Down Expand Up @@ -151,7 +152,7 @@ public function __unset($name)
* @param array $options
* @return BaseCollection $list
*/
public function getList($options)
public function getList($options = array())
{
$list = new BaseCollection($this->client, get_class($this));
$list->loadItems($options);
Expand Down Expand Up @@ -197,16 +198,17 @@ public function delete($id = null)
$path = '/app.core.connector/api/v2/' . $this->getCurrentModelUrlPath() . '/';
if (empty($id)) {
if (!$this->initialized || empty($this->_attributes[$this->_id_column])) {
throw new \Exception('cannot delete object with empty primary key value');
throw new \Exception('Cannot delete object with empty primary key value');
}
$path .= $this->_attributes[$this->_id_column];
$path .= $this->_attributes[$this->_id_column];
} else {
$path .= $id;
}

$data = array();
$options = array();

$response = $this->client->delete($path, $options);
$response = $this->client->delete($path, $data, $options);
return $response;
}

Expand All @@ -222,7 +224,7 @@ public function getUpdateAttributes()
switch ($this->_relations[$name]['type']) {
case MainModel::RELATION_HAS_MANY:
foreach ($related as $related_sub) {
$ret[$name][] = $related_sub->getUpdateAttributes();
$ret[$name][] = $related_sub->getUpdateAttributes();
}
break;
case MainModel::RELATION_HAS_ONE:
Expand Down Expand Up @@ -281,9 +283,9 @@ public function save()
* @return mixed the related object
* @throws Exception if the relation is not specified
*/
public function getRelated($name, $refresh=false)
public function getRelated($name, $refresh = false)
{
if(!$refresh && (isset($this->_related[$name]) || array_key_exists($name, $this->_related))) {
if (!$refresh && (isset($this->_related[$name]) || array_key_exists($name, $this->_related))) {
return $this->_related[$name];
}

Expand All @@ -293,7 +295,7 @@ public function getRelated($name, $refresh=false)
}
$relation_def = $this->_relations[$name];

$this->client->logger->info('lazy loading '. get_class($this).'.'. $name);
$this->client->logger->info('lazy loading ' . get_class($this) . '.' . $name);

if ($refresh) {
unset($this->_related[$name]);
Expand All @@ -304,7 +306,7 @@ public function getRelated($name, $refresh=false)
$this->_related[$name] = null;
} else {
// get new object
$this->lazyLoad($relation_def);
$this->lazyLoad($name, $relation_def);
}

return $this->_related[$name];
Expand Down Expand Up @@ -357,8 +359,14 @@ public function setAttribute($name, $value)
return parent::setAttribute($name, $value);
}

// TODO think what would be the best return value
private function lazyLoad($relation_def)
/**
* Function to lazy load related object
* @param string $name
* @param array $relation_def
* @return bool
* @throws Exception
*/
private function lazyLoad($name, $relation_def)
{
$related_category = $relation_def['category'];
$related_model = $relation_def['model'];
Expand Down Expand Up @@ -425,8 +433,8 @@ private function getResponse($id)
$options = array();

// TODO fix
$response = $this->client->get($path . '/' . $id, array('query'=>$options));
return $response->json();
$response = $this->client->get($path . '/' . $id, array('query' => $options));
return $response->json();
}

/**
Expand Down
37 changes: 37 additions & 0 deletions lib/secucard/models/Common/Contact.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* Contact Common model class
*/

namespace secucard\models\Common;

use secucard\client\base\BaseModel;

/**
* Contact Data Model class
*
*/
class Contact extends BaseModel
{
protected $_attribute_defs = array(
'salutation' => array('type' => BaseModel::DATA_TYPE_STRING),
'title' => array('type' => BaseModel::DATA_TYPE_STRING),
'forename' => array('type' => BaseModel::DATA_TYPE_STRING),
'surname' => array('type' => BaseModel::DATA_TYPE_STRING),
'name' => array('type' => BaseModel::DATA_TYPE_STRING), // not used for now
'companyname' => array('type' => BaseModel::DATA_TYPE_STRING),
// TODO check if date is correct!
'dob' => array('type' => BaseModel::DATA_TYPE_DATE),
'birthplace' => array('type' => BaseModel::DATA_TYPE_STRING),
'nationality' => array('type' => BaseModel::DATA_TYPE_STRING),
'gender' => array('type' => BaseModel::DATA_TYPE_STRING),
'phone' => array('type' => BaseModel::DATA_TYPE_STRING),
'mobile' => array('type' => BaseModel::DATA_TYPE_STRING),
'email' => array('type' => BaseModel::DATA_TYPE_STRING),
'picture' => array('type' => BaseModel::DATA_TYPE_STRING),
'url_website' => array('type' => BaseModel::DATA_TYPE_STRING),
// Address is subarray
'address' => array('type' => BaseModel::DATA_TYPE_ARRAY_SUBOBJECT, 'category' => 'Common', 'model' => 'Contactaddress'),
);

}
24 changes: 24 additions & 0 deletions lib/secucard/models/Common/Contactaddress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Contactaddress Common model class
*/

namespace secucard\models\Common;

use secucard\client\base\BaseModel;

/**
* Contactaddress Data Model class
*
*/
class Contactaddress extends BaseModel
{
protected $_attribute_defs = array(
'street' => array('type' => BaseModel::DATA_TYPE_STRING),
'street_number' => array('type' => BaseModel::DATA_TYPE_STRING),
'city' => array('type' => BaseModel::DATA_TYPE_STRING),
'postal_code' => array('type' => BaseModel::DATA_TYPE_STRING),
'country' => array('type' => BaseModel::DATA_TYPE_STRING),
);

}
21 changes: 11 additions & 10 deletions lib/secucard/models/General/Accounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@
class Accounts extends MainModel
{
protected $_attribute_defs = array(
'object'=>array('type'=>BaseModel::DATA_TYPE_STRING),
'id'=>array('type'=>BaseModel::DATA_TYPE_STRING, 'options'=>array('id'=>true)),
'iban'=>array('type'=>BaseModel::DATA_TYPE_STRING),
'bic'=>array('type'=>BaseModel::DATA_TYPE_STRING),
'forename'=>array('type'=>BaseModel::DATA_TYPE_STRING),
'surname'=>array('type'=>BaseModel::DATA_TYPE_STRING),
'display_name'=>array('type'=>BaseModel::DATA_TYPE_STRING),
);

protected $_relations = array();
'object' => array('type' => BaseModel::DATA_TYPE_STRING),
'id' => array('type' => BaseModel::DATA_TYPE_STRING, 'options' => array('id' => true)),
'username' => array('type' => BaseModel::DATA_TYPE_STRING),
'role' => array('type' => BaseModel::DATA_TYPE_STRING),
'assignment' => array('type' => BaseModel::DATA_TYPE_ARRAY),
'invitation' => array('type' => BaseModel::DATA_TYPE_ARRAY),
'social' => array('type' => BaseModel::DATA_TYPE_ARRAY),
);

protected $_relations = array(
'contact' => array('type' => MainModel::RELATION_HAS_ONE, 'category' => 'Common', 'model' => 'Contact'),
);

// model specific functions

Expand Down
27 changes: 27 additions & 0 deletions lib/secucard/models/General/Devices.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* Devices Api Model class
*/

namespace secucard\models\General;

use secucard\client\base\BaseModel;
use secucard\client\base\MainModel;

/**
* Devices Api Model class
*
*/
class Devices extends MainModel
{
// TODO this model is not yet implemented correctly
protected $_attribute_defs = array(
'object' => array('type' => BaseModel::DATA_TYPE_STRING),
'id' => array('type' => BaseModel::DATA_TYPE_STRING, 'options' => array('id' => true)),
);

protected $_relations = array();

// model specific functions

}
26 changes: 26 additions & 0 deletions lib/secucard/models/General/Merchants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* Merchants Api Model class
*/

namespace secucard\models\General;

use secucard\client\base\BaseModel;
use secucard\client\base\MainModel;

/**
* Merchants Api Model class
*
*/
class Merchants extends MainModel
{
// TODO this class is not yet implemented
protected $_attribute_defs = array(
'object' => array('type' => BaseModel::DATA_TYPE_STRING),
'id' => array('type' => BaseModel::DATA_TYPE_STRING, 'options' => array('id' => true)),
);

protected $_relations = array(
//
);
}
28 changes: 28 additions & 0 deletions lib/secucard/models/General/Stores.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Stores Api Model class
*/

namespace secucard\models\General;

use secucard\client\base\BaseModel;
use secucard\client\base\MainModel;

/**
* Stores Api Model class
*
*/
class Stores extends MainModel
{
// TODO this model is not yet implemented correctly
protected $_attribute_defs = array(
'object' => array('type' => BaseModel::DATA_TYPE_STRING),
'id' => array('type' => BaseModel::DATA_TYPE_STRING, 'options' => array('id' => true)),
);

protected $_relations = array();


// model specific functions

}
31 changes: 31 additions & 0 deletions lib/secucard/models/Services/Identcontracts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Identcontracts Api Model class
*/

namespace secucard\models\Services;

use secucard\client\base\BaseModel;
use secucard\client\base\MainModel;

/**
* Identcontracts Api Model class
*
*/
class Identcontracts extends MainModel
{
protected $_attribute_defs = array(
'object' => array('type' => BaseModel::DATA_TYPE_STRING),
'id' => array('type' => BaseModel::DATA_TYPE_STRING, 'options' => array('id' => true)),
'demo' => array('type' => BaseModel::DATA_TYPE_BOOLEAN),
'redirect_url_success' => array('type' => BaseModel::DATA_TYPE_STRING),
'redirect_url_failed' => array('type' => BaseModel::DATA_TYPE_STRING),
'push_url' => array('type' => BaseModel::DATA_TYPE_STRING),
'created' => array('type' => BaseModel::DATA_TYPE_DATETIME),
);

protected $_relations = array(
'merchant' => array('type' => MainModel::RELATION_HAS_ONE, 'category' => 'General', 'model' => 'Merchants'),
);

}
21 changes: 11 additions & 10 deletions lib/secucard/models/Services/Identrequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@
class Identrequests extends MainModel
{
protected $_attribute_defs = array(
'object'=>array('type'=>BaseModel::DATA_TYPE_STRING),
'id'=>array('type'=>BaseModel::DATA_TYPE_STRING, 'options'=>array('id'=>true)),
'type'=>array('type'=>BaseModel::DATA_TYPE_STRING),
'status'=>array('type'=>BaseModel::DATA_TYPE_STRING),
'owner_transaction_id'=>array('type'=>BaseModel::DATA_TYPE_STRING),
'owner'=>array('type'=>BaseModel::DATA_TYPE_STRING),
'created'=>array('type'=>BaseModel::DATA_TYPE_DATETIME),
);
'object' => array('type' => BaseModel::DATA_TYPE_STRING),
'id' => array('type' => BaseModel::DATA_TYPE_STRING, 'options' => array('id' => true)),
'type' => array('type' => BaseModel::DATA_TYPE_STRING),
'demo' => array('type' => BaseModel::DATA_TYPE_BOOLEAN),
'status' => array('type' => BaseModel::DATA_TYPE_STRING),
'owner_transaction_id' => array('type' => BaseModel::DATA_TYPE_STRING),
'created' => array('type' => BaseModel::DATA_TYPE_DATETIME),
);

protected $_relations = array(
'person'=>array('type'=>MainModel::RELATION_HAS_MANY, 'category'=>'Services', 'model'=>'IdentrequestsPerson'),
);
'contract' => array('type' => MainModel::RELATION_HAS_ONE, 'category' => 'Services', 'model' => 'Identcontracts'),
'person' => array('type' => MainModel::RELATION_HAS_MANY, 'category' => 'Services', 'model' => 'IdentrequestsPerson'),
);

}
Loading

0 comments on commit a2be050

Please sign in to comment.