Skip to content

Commit

Permalink
Merge pull request #22 from MichaelJ2324/2.0
Browse files Browse the repository at this point in the history
FilterData Updates + Count Endppoints
  • Loading branch information
geraldclark committed Jun 30, 2017
2 parents e44dd18 + 317c358 commit fb59d3f
Show file tree
Hide file tree
Showing 11 changed files with 225 additions and 79 deletions.
2 changes: 1 addition & 1 deletion examples/FilterAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
->endOr()
->equals('assigned_user_id','seed_max_id')
->endAnd();
echo "<pre> Filter Accounts that are assigned to User Max, and that either start with an S or contain 'test' in the name: ".var_dump($Accounts->filter()->compile())."</pre><br>";
echo "<pre> Filter Accounts that are assigned to User Max, and that either start with an S or contain 'test' in the name: ".print_r($Accounts->filter()->compile(),true)."</pre><br>";
$Accounts->filter()->execute();
echo "<pre> Response:".print_r($Accounts->getResponse()->getBody(),true)."</pre><br>";
}catch (Exception $ex){
Expand Down
10 changes: 6 additions & 4 deletions examples/FilterRelated.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
echo "</pre>";
$Accounts = $SugarAPI->list('Accounts');
$Accounts->fetch();
$account = current($Accounts->asArray());
$Account = $Accounts->get($account['id']);
$Account = $Accounts->at(1);
$Account->getRelated('contacts',true);
echo $Account->getRequest()->getURL()."<br>";
echo "<pre> Response:".print_r($Account->getResponse()->getBody(),true)."</pre><br>";
$Filter = $Account->filterRelated('contacts')->contains('first_name','s');
echo "<pre> Filter Contacts related to Account {$account['id']} where first_name contains an 's': ".var_dump($Filter->compile())."</pre><br>";
echo "<pre> Filter Contacts related to Account {$account['id']} where first_name contains an 's': ".print_r($Filter->compile(),true)."</pre><br>";
$Filter->execute();
echo "<pre> Response:".print_r($Account->getRequest(),true)."</pre><br>";
echo "<pre> Response:".print_r($Account->getResponse()->getBody(),true)."</pre><br>";
}catch (Exception $ex){
echo "<pre>";
//print_r($SugarAPI);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ abstract class AbstractSugarBeanCollectionEndpoint extends AbstractSugarCollecti

public function setOptions(array $options)
{
$opts = array();
if (isset($options[0])) {
$this->setModule($options[0]);
$opts['module'] = $this->module;
$options['module'] = $options[0];
$this->setModule($options['module']);
unset($options[0]);
}
return parent::setOptions($opts);
return parent::setOptions($options);
}

/**
Expand Down
53 changes: 37 additions & 16 deletions src/Endpoint/Abstracts/AbstractSugarBeanEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ abstract class AbstractSugarBeanEndpoint extends ModelEndpoint implements SugarE

const BEAN_ACTION_ATTACH_FILE = 'attachFile';

const BEAN_ACTION_ARG1_VAR = 'actionArg1';

const BEAN_ACTION_ARG2_VAR = 'actionArg2';

const BEAN_ACTION_ARG3_VAR = 'actionArg3';

const BEAN_MODULE_VAR = 'module';

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -94,7 +102,6 @@ public function __construct(array $options = array(), array $properties = array(
* @inheritdoc
*/
public function compileRequest(){
$this->configureAction($this->getCurrentAction());
return $this->configureRequest($this->getRequest());
}

Expand All @@ -106,7 +113,7 @@ public function compileRequest(){
public function setOptions(array $options) {
if (isset($options[0])){
$this->setModule($options[0]);
$options['module'] = $this->module;
$options[self::BEAN_MODULE_VAR] = $this->module;
unset($options[0]);
}
if (isset($options[1])){
Expand Down Expand Up @@ -140,8 +147,8 @@ public function getModule(){
* @inheritdoc
*/
protected function configureURL(array $options) {
$action = $this->action;
switch($this->action){
$action = $this->getCurrentAction();
switch($action){
case self::BEAN_ACTION_CREATE_RELATED:
case self::BEAN_ACTION_MASS_RELATE:
case self::BEAN_ACTION_UNLINK:
Expand Down Expand Up @@ -173,6 +180,11 @@ protected function configureURL(array $options) {
* @inheritdoc
*/
protected function configureAction($action,array $arguments = array()) {
$options = $this->getOptions();
$options[self::BEAN_MODULE_VAR] = $this->module;
if (isset($options[self::BEAN_ACTION_ARG1_VAR])) unset($options[self::BEAN_ACTION_ARG1_VAR]);
if (isset($options[self::BEAN_ACTION_ARG2_VAR])) unset($options[self::BEAN_ACTION_ARG2_VAR]);
if (isset($options[self::BEAN_ACTION_ARG3_VAR])) unset($options[self::BEAN_ACTION_ARG3_VAR]);
if (!empty($arguments)){
switch($action){
case self::BEAN_ACTION_RELATE:
Expand All @@ -182,18 +194,18 @@ protected function configureAction($action,array $arguments = array()) {
case self::BEAN_ACTION_CREATE_RELATED:
case self::BEAN_ACTION_FILTER_RELATED:
if (isset($arguments[0])){
$this->options['actionArg1'] = $arguments[0];
}
if (isset($arguments[1])){
$this->options['actionArg2'] = $arguments[1];
}
if (isset($arguments[2])){
$this->options['actionArg3'] = $arguments[2];
$options[self::BEAN_ACTION_ARG1_VAR] = $arguments[0];
if (isset($arguments[1])){
$options[self::BEAN_ACTION_ARG2_VAR] = $arguments[1];
if (isset($arguments[2])){
$options[self::BEAN_ACTION_ARG3_VAR] = $arguments[2];
}
}
}
}
}
$this->options['module'] = $this->module;
parent::configureAction($action);
$this->setOptions($options);
parent::configureAction($action,$arguments);
}

/**
Expand Down Expand Up @@ -245,21 +257,30 @@ public function getFile($field){
/**
* Human friendly overload for filterLink action
* @param $linkName - Name of Relationship Link
* @param bool $count
* @return self
*/
public function getRelated($linkName){
public function getRelated($linkName,$count = false){
if ($count){
return $this->filterLink($linkName,'count');
}
return $this->filterLink($linkName);
}

/**
* Filter generator for Related Links
* @param $linkName - Name of Relationship Link
* @param bool $count - Whether or not to just do a count request
* @return FilterData
*/
public function filterRelated($linkName){
public function filterRelated($linkName,$count = false){
$Filter = new FilterData($this);
$this->setCurrentAction(self::BEAN_ACTION_FILTER_RELATED);
$this->configureAction($this->action,array($linkName));
$args = array($linkName);
if ($count){
$args[] = 'count';
}
$this->configureAction($this->action,$args);
return $Filter;
}

Expand Down
17 changes: 15 additions & 2 deletions src/Endpoint/Data/FilterData.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
class FilterData extends AbstractExpression implements DataInterface
{
const FILTER_PARAM = 'filter';

/**
* @var AbstractSmartEndpoint
*/
Expand Down Expand Up @@ -96,7 +98,7 @@ public function offsetGet($offset) {
public function asArray($compile = TRUE){
if ($compile){
$data = $this->compile();
$this->data[ModuleFilter::FILTER_PARAM] = $data;
$this->data = array_replace_recursive($this->data,$data);
}
return $this->data;
}
Expand Down Expand Up @@ -148,6 +150,7 @@ public function update(array $data){
}

/**
* Set the Endpoint using the Filter Data
* @param AbstractSmartEndpoint $Endpoint
* @return self
*/
Expand All @@ -156,13 +159,23 @@ public function setEndpoint(AbstractSmartEndpoint $Endpoint){
return $this;
}

/**
* Return the Endpoint being used with the Filter Data
* @return AbstractSmartEndpoint
* @codeCoverageIgnore
*/
public function getEndpoint(){
return $this->Endpoint;
}

/**
* @return AbstractSmartEndpoint|false
* @throws \MRussell\REST\Exception\Endpoint\InvalidRequest
*/
public function execute(){
if (isset($this->Endpoint)){
$this->Endpoint->getData()->update($this->asArray());
$filter = $this->asArray();
$this->Endpoint->getData()->update(array(self::FILTER_PARAM => $filter));
return $this->Endpoint->execute();
}
return false;
Expand Down
73 changes: 48 additions & 25 deletions src/Endpoint/ModuleFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
*/
class ModuleFilter extends AbstractSugarBeanCollectionEndpoint
{
const FILTER_PARAM = 'filter';
const COUNT_OPTION = 'count';

protected static $_ENDPOINT_URL = '$module/$:filter';
protected static $_ENDPOINT_URL = '$module/filter/$:count';

/**
* @var FilterData
Expand All @@ -30,14 +30,30 @@ class ModuleFilter extends AbstractSugarBeanCollectionEndpoint
*/
protected $data;

/**
* @var
*/
protected $totalCount;

/**
* Sanitize passed in options
* @param array $options
* @return $this|mixed
*/
public function setOptions(array $options)
{
if (isset($options[1])){
unset($options[1]);
$options[self::COUNT_OPTION] = self::COUNT_OPTION;
}
return parent::setOptions($options);
}

/**
* @inheritdoc
*/
public function fetch(){
if (isset($this->options[self::FILTER_PARAM])){
unset($this->options[self::FILTER_PARAM]);
}
$this->setProperty('httpMethod',JSON::HTTP_GET);
$this->setProperty(self::PROPERTY_HTTP_METHOD,JSON::HTTP_GET);
return parent::fetch();
}

Expand All @@ -47,42 +63,49 @@ public function fetch(){
*/
protected function configureData($data)
{
if (isset($this->options[self::FILTER_PARAM]) && is_object($this->Filter)){
$data->update($this->Filter->asArray());
if (is_object($this->Filter)){
$compiledFilter = $this->Filter->asArray();
if (!empty($compiledFilter)){
$data->update(array(FilterData::FILTER_PARAM => $this->Filter->asArray()));
}
}
return parent::configureData($data);
}

/**
* Check for httpMethod setting, and if configured for POST make sure to add Filter to URL
* @param array $options
* @return string
*/
protected function configureURL(array $options)
{
$properties = $this->getProperties();
if (!isset($options[self::FILTER_PARAM]) && $properties['httpMethod'] == JSON::HTTP_POST){
$options[self::FILTER_PARAM] = self::FILTER_PARAM;
}
return parent::configureURL($options);
}

/**
* Configure the Filter Parameters for the Filter API
* @param bool $reset
* @return FilterData
*/
public function filter($reset = FALSE){
$this->options[self::FILTER_PARAM] = self::FILTER_PARAM;
$this->setProperty('httpMethod',JSON::HTTP_POST);
public function filter($reset = FALSE)
{
$this->setProperty(self::PROPERTY_HTTP_METHOD,JSON::HTTP_POST);
if (empty($this->Filter)){
$this->Filter = new FilterData();
$this->Filter->setEndpoint($this);
$data = $this->getData();
if (isset($data[FilterData::FILTER_PARAM]) && !empty($data[FilterData::FILTER_PARAM])){
$this->Filter->update($data[FilterData::FILTER_PARAM]);
}
}
if ($reset){
$this->Filter->reset();
$data = $this->getData();
if (isset($data[FilterData::FILTER_PARAM]) && !empty($data[FilterData::FILTER_PARAM])){
unset($data[FilterData::FILTER_PARAM]);
$this->setData($data);
}
}
return $this->Filter;
}

/**
* Configure the Request to use Count Endpoint
*/
public function count()
{
$this->setOptions(array($this->getModule(),self::COUNT_OPTION));
return $this->execute();
}

}
1 change: 1 addition & 0 deletions src/Helpers/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
class Helper
{
const API_VERSION = 10;

const API_URL = '/rest/v%d/';

/**
Expand Down
3 changes: 2 additions & 1 deletion tests/Endpoint/AbstractSugarBeanCollectionEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public function testSetOptions()
'foo'
)));
$this->assertEquals(array(
'module' => 'Accounts'
'module' => 'Accounts',
1 => 'foo'
),$Endpoint->getOptions());
}

Expand Down
11 changes: 11 additions & 0 deletions tests/Endpoint/AbstractSugarBeanEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ public function testGetRelated(){
$this->assertEquals($Bean,$Bean->getRelated('test'));
$this->assertEquals('http://localhost/rest/v10/Foo/bar/link/test',$Bean->getRequest()->getURL());
$this->assertEquals('GET',$Bean->getRequest()->getMethod());

$this->assertEquals($Bean,$Bean->getRelated('test',true));
$this->assertEquals('http://localhost/rest/v10/Foo/bar/link/test/count',$Bean->getRequest()->getURL());
$this->assertEquals('GET',$Bean->getRequest()->getMethod());
}

/**
Expand All @@ -177,6 +181,13 @@ public function testFilterRelated(){
$this->assertEquals('http://localhost/rest/v10/Foo/bar/link/test',$Bean->getRequest()->getURL());
$this->assertEquals('GET',$Bean->getRequest()->getMethod());
$this->assertArrayHasKey('filter',$Bean->getRequest()->getBody());

$Filter = $Bean->filterRelated('test',true);
$this->assertInstanceOf('Sugarcrm\\REST\\Endpoint\\Data\\FilterData',$Filter);
$this->assertEquals($Bean,$Filter->execute());
$this->assertEquals('http://localhost/rest/v10/Foo/bar/link/test/count',$Bean->getRequest()->getURL());
$this->assertEquals('GET',$Bean->getRequest()->getMethod());
$this->assertArrayHasKey('filter',$Bean->getRequest()->getBody());
}

/**
Expand Down

0 comments on commit fb59d3f

Please sign in to comment.