Skip to content
This repository has been archived by the owner on Jun 1, 2021. It is now read-only.

PHP 7.2 compatibility, add support for new Connect field type #17

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

"minimum-stability": "stable",
"require": {
"php": "^7.0",
"php": "^7.0 || ^8.0",
"guzzlehttp/guzzle": "^6.3",
"doctrine/inflector": "^1.2"
},
Expand Down
2 changes: 1 addition & 1 deletion src/Endpoints/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public function search(array $params = [], int $size = 200, int $page = null)
do {
$results = $this->request('post', 'search', ['json' => $params]);
$entries = array_merge($entries, is_object($results)? [$results] : $results);
} while ($allPages && sizeof($results) >= $safeLimit && ++$params['page_number']);
} while ($allPages && is_array($results) && sizeof($results) >= $safeLimit && ++$params['page_number']);

return $entries;
}
Expand Down
File renamed without changes.
File renamed without changes.
41 changes: 21 additions & 20 deletions src/SubResources/CustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,35 +42,36 @@ public function __construct(string $idOrName, $value = null, $resource = null)
$values = [];
$field = CRM::fieldList('customFieldDefinition', $idOrName, true);

switch (sizeof($field)) {
case 1:
if (is_array($field)) {
if (is_array($field)) {
switch (sizeof($field)) {
case 1:
$field = current($field); //gets the first entry directly
}
break;
break;

case 0:
throw new InvalidArg("Custom Field not found: $idOrName");
case 0:
throw new InvalidArg("Custom Field not found: $idOrName");

default: //will only happen on string identifiers (name)
if ($resource) {
//returns the first item found with the resource name in the available list
$field = array_filter($field, function ($f) use ($resource) {
return in_array($resource, $f->available_on);
});
} else {
throw new InvalidArg("There's more than one '$idOrName' field. To distinguish we need the resource name as well.");
}
default: //will only happen on string identifiers (name)
if ($resource) {
//returns the first item found with the resource name in the available list
$field = array_filter($field, function ($f) use ($resource) {
return in_array($resource, $f->available_on);
});
} else {
throw new InvalidArg("There's more than one '$idOrName' field. To distinguish we need the resource name as well.");
}
}
}

$this->custom_field_definition_id = $field->id;
$this->name = $field->name;
$this->type = $field->data_type;
$this->resources = $field->available_on;
$this->options = $field->options ?? [];

//validating $resource and options, if available
if (is_array($value) && $this->type != "MultiSelect") {
throw new InvalidArg("Invalid multiple values for field $name that is not a MultiSelect field.");
if (is_array($value) && $this->type != "MultiSelect" && $this->type != "Connect") {
throw new InvalidArg('Invalid multiple values for field ' . $field->name . ' that is not a supported field type (type: ' . $this->type . ')');
}

if ($resource && !in_array($resource, $this->resources)) {
Expand Down Expand Up @@ -104,7 +105,7 @@ public function __construct(string $idOrName, $value = null, $resource = null)
}
}

if ($this->type == "MultiSelect") {
if ($this->type == "MultiSelect" || $this->type == "Connect") {
$this->value = $values;
} else {
$this->value = $values[0];
Expand All @@ -116,7 +117,7 @@ public function __construct(string $idOrName, $value = null, $resource = null)

public function getValue() {
if (count($this->options) > 0) {
if ($this->type == "MultiSelect") {
if ($this->type == "MultiSelect" || $this->type == "Connect") {
$values = [];
foreach ($this->value as $val) {
$values[] = $this->options[$val];
Expand Down
4 changes: 2 additions & 2 deletions src/Webhooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function create(string $endpoint, $event = self::EV_ALL)
$response[] = $result->id;
}

return (sizeof($response) == 1)? $response[0] : $response;
return (is_array($response) && sizeof($response) == 1) ? $response[0] : $response;
}

public function list(int $id = null): array
Expand All @@ -139,4 +139,4 @@ public function delete(int ...$id)
{
return array_column($this->request('delete', $id), 'id');
}
}
}