Skip to content

Commit

Permalink
Merge pull request #98 from creative-commoners/pulls/2/php81
Browse files Browse the repository at this point in the history
ENH PHP 8.1 compatibility
  • Loading branch information
GuySartorelli authored Apr 26, 2022
2 parents 94bad49 + 5bff584 commit fa77f23
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 53 deletions.
15 changes: 9 additions & 6 deletions src/DataFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ abstract class DataFormatter
*/
protected function sanitiseClassName($className)
{
return str_replace('\\', '-', $className);
return str_replace('\\', '-', $className ?? '');
}

/**
Expand All @@ -123,7 +123,7 @@ public static function for_extension($extension)
arsort($sortedClasses);
foreach ($sortedClasses as $className => $priority) {
$formatter = new $className();
if (in_array($extension, $formatter->supportedExtensions())) {
if (in_array($extension, $formatter->supportedExtensions() ?? [])) {
return $formatter;
}
}
Expand Down Expand Up @@ -163,7 +163,7 @@ public static function for_mimetype($mimeType)
arsort($sortedClasses);
foreach ($sortedClasses as $className => $priority) {
$formatter = new $className();
if (in_array($mimeType, $formatter->supportedMimeTypes())) {
if (in_array($mimeType, $formatter->supportedMimeTypes() ?? [])) {
return $formatter;
}
}
Expand Down Expand Up @@ -329,7 +329,10 @@ protected function getFieldsForObj($obj)
$dbFields = array_merge($dbFields, ['ID' => 'Int']);

if (is_array($this->removeFields)) {
$dbFields = array_diff_key($dbFields, array_combine($this->removeFields, $this->removeFields));
$dbFields = array_diff_key(
$dbFields ?? [],
array_combine($this->removeFields ?? [], $this->removeFields ?? [])
);
}

return $dbFields;
Expand Down Expand Up @@ -418,7 +421,7 @@ public function getRealFieldName($className, $field)
public function getFieldAlias($className, $field)
{
$apiMapping = $this->getApiMapping($className);
$apiMapping = array_flip($apiMapping);
$apiMapping = array_flip($apiMapping ?? []);
return $this->getMappedKey($apiMapping, $field);
}

Expand Down Expand Up @@ -448,7 +451,7 @@ protected function getApiMapping($className)
protected function getMappedKey($map, $key)
{
if (is_array($map)) {
if (array_key_exists($key, $map)) {
if (array_key_exists($key, $map ?? [])) {
return $map[$key];
} else {
return $key;
Expand Down
2 changes: 1 addition & 1 deletion src/DataFormatter/FormEncodedDataFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function supportedMimeTypes()
public function convertStringToArray($strData)
{
$postArray = array();
parse_str($strData, $postArray);
parse_str($strData ?? '', $postArray);
return $postArray;
//TODO: It would be nice to implement this function in Convert.php
//return Convert::querystr2array($strData);
Expand Down
14 changes: 7 additions & 7 deletions src/DataFormatter/JSONDataFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function convertDataObjectToJSONObject(DataObjectInterface $obj, $fields

foreach ($this->getFieldsForObj($obj) as $fieldName => $fieldType) {
// Field filtering
if ($fields && !in_array($fieldName, $fields)) {
if ($fields && !in_array($fieldName, $fields ?? [])) {
continue;
}

Expand All @@ -102,10 +102,10 @@ public function convertDataObjectToJSONObject(DataObjectInterface $obj, $fields
}

// Field filtering
if ($fields && !in_array($relName, $fields)) {
if ($fields && !in_array($relName, $fields ?? [])) {
continue;
}
if ($this->customRelations && !in_array($relName, $this->customRelations)) {
if ($this->customRelations && !in_array($relName, $this->customRelations ?? [])) {
continue;
}
if ($obj->$relName() && (!$obj->$relName()->exists() || !$obj->$relName()->canView())) {
Expand All @@ -129,18 +129,18 @@ public function convertDataObjectToJSONObject(DataObjectInterface $obj, $fields
$relClass = RestfulServer::parseRelationClass($relClass);

//remove dot notation from relation names
$parts = explode('.', $relClass);
$parts = explode('.', $relClass ?? '');
$relClass = array_shift($parts);

if (!singleton($relClass)->stat('api_access')) {
continue;
}

// Field filtering
if ($fields && !in_array($relName, $fields)) {
if ($fields && !in_array($relName, $fields ?? [])) {
continue;
}
if ($this->customRelations && !in_array($relName, $this->customRelations)) {
if ($this->customRelations && !in_array($relName, $this->customRelations ?? [])) {
continue;
}

Expand Down Expand Up @@ -195,7 +195,7 @@ public function convertDataObjectSet(SS_List $set, $fields = null)
*/
public function convertStringToArray($strData)
{
return json_decode($strData, true);
return json_decode($strData ?? '', true);
}

public static function cast(FieldType\DBField $dbfield)
Expand Down
20 changes: 10 additions & 10 deletions src/DataFormatter/XMLDataFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function convertDataObjectWithoutHeader(DataObject $obj, $fields = null,
$xml = "<$className href=\"$objHref.xml\">\n";
foreach ($this->getFieldsForObj($obj) as $fieldName => $fieldType) {
// Field filtering
if ($fields && !in_array($fieldName, $fields)) {
if ($fields && !in_array($fieldName, $fields ?? [])) {
continue;
}
$fieldValue = $obj->obj($fieldName)->forTemplate();
Expand All @@ -134,7 +134,7 @@ public function convertDataObjectWithoutHeader(DataObject $obj, $fields = null,
} else {
if ('HTMLText' == $fieldType) {
// Escape HTML values using CDATA
$fieldValue = sprintf('<![CDATA[%s]]>', str_replace(']]>', ']]]]><![CDATA[>', $fieldValue));
$fieldValue = sprintf('<![CDATA[%s]]>', str_replace(']]>', ']]]]><![CDATA[>', $fieldValue ?? ''));
} else {
$fieldValue = Convert::raw2xml($fieldValue);
}
Expand All @@ -150,10 +150,10 @@ public function convertDataObjectWithoutHeader(DataObject $obj, $fields = null,
}

// Field filtering
if ($fields && !in_array($relName, $fields)) {
if ($fields && !in_array($relName, $fields ?? [])) {
continue;
}
if ($this->customRelations && !in_array($relName, $this->customRelations)) {
if ($this->customRelations && !in_array($relName, $this->customRelations ?? [])) {
continue;
}

Expand All @@ -169,7 +169,7 @@ public function convertDataObjectWithoutHeader(DataObject $obj, $fields = null,

foreach ($obj->hasMany() as $relName => $relClass) {
//remove dot notation from relation names
$parts = explode('.', $relClass);
$parts = explode('.', $relClass ?? '');
$relClass = array_shift($parts);
if (!singleton($relClass)->stat('api_access')) {
continue;
Expand All @@ -178,10 +178,10 @@ public function convertDataObjectWithoutHeader(DataObject $obj, $fields = null,
$relClass = $this->sanitiseClassName($relClass);

// Field filtering
if ($fields && !in_array($relName, $fields)) {
if ($fields && !in_array($relName, $fields ?? [])) {
continue;
}
if ($this->customRelations && !in_array($relName, $this->customRelations)) {
if ($this->customRelations && !in_array($relName, $this->customRelations ?? [])) {
continue;
}

Expand All @@ -200,7 +200,7 @@ public function convertDataObjectWithoutHeader(DataObject $obj, $fields = null,
$relClass = RestfulServer::parseRelationClass($relClass);

//remove dot notation from relation names
$parts = explode('.', $relClass);
$parts = explode('.', $relClass ?? '');
$relClass = array_shift($parts);
if (!singleton($relClass)->stat('api_access')) {
continue;
Expand All @@ -209,10 +209,10 @@ public function convertDataObjectWithoutHeader(DataObject $obj, $fields = null,
$relClass = $this->sanitiseClassName($relClass);

// Field filtering
if ($fields && !in_array($relName, $fields)) {
if ($fields && !in_array($relName, $fields ?? [])) {
continue;
}
if ($this->customRelations && !in_array($relName, $this->customRelations)) {
if ($this->customRelations && !in_array($relName, $this->customRelations ?? [])) {
continue;
}

Expand Down
36 changes: 18 additions & 18 deletions src/RestfulServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function init()
*/
protected function sanitiseClassName($className)
{
return str_replace('\\', '-', $className);
return str_replace('\\', '-', $className ?? '');
}

/**
Expand All @@ -150,7 +150,7 @@ protected function sanitiseClassName($className)
*/
protected function unsanitiseClassName($className)
{
return str_replace('-', '\\', $className);
return str_replace('-', '\\', $className ?? '');
}

/**
Expand All @@ -163,13 +163,13 @@ public static function parseRelationClass($class)
{
// detect many many through syntax
if (is_array($class)
&& array_key_exists('through', $class)
&& array_key_exists('to', $class)
&& array_key_exists('through', $class ?? [])
&& array_key_exists('to', $class ?? [])
) {
$toRelation = $class['to'];

$hasOne = Config::inst()->get($class['through'], 'has_one');
if (empty($hasOne) || !is_array($hasOne) || !array_key_exists($toRelation, $hasOne)) {
if (empty($hasOne) || !is_array($hasOne) || !array_key_exists($toRelation, $hasOne ?? [])) {
return $class;
}

Expand All @@ -190,14 +190,14 @@ public function index(HTTPRequest $request)
$relation = $request->param('Relation') ?: null;

// Check input formats
if (!class_exists($className)) {
if (!class_exists($className ?? '')) {
return $this->notFound();
}
if ($id && !is_numeric($id)) {
return $this->notFound();
}
if ($relation
&& !preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $relation)
&& !preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $relation ?? '')
) {
return $this->notFound();
}
Expand Down Expand Up @@ -326,7 +326,7 @@ protected function getHandler($className, $id, $relationName)
$this->getResponse()->addHeader('Content-Type', $responseFormatter->getOutputContentType());

$rawFields = $this->request->getVar('fields');
$realFields = $responseFormatter->getRealFields($className, explode(',', $rawFields));
$realFields = $responseFormatter->getRealFields($className, explode(',', $rawFields ?? ''));
$fields = $rawFields ? $realFields : null;

if ($obj instanceof SS_List) {
Expand Down Expand Up @@ -391,7 +391,7 @@ protected function getDataFormatter($includeAcceptHeader = false, $className = n
{
$extension = $this->request->getExtension();
$contentTypeWithEncoding = $this->request->getHeader('Content-Type');
preg_match('/([^;]*)/', $contentTypeWithEncoding, $contentTypeMatches);
preg_match('/([^;]*)/', $contentTypeWithEncoding ?? '', $contentTypeMatches);
$contentType = $contentTypeMatches[0];
$accept = $this->request->getHeader('Accept');
$mimetypes = $this->request->getAcceptMimetypes();
Expand All @@ -402,7 +402,7 @@ protected function getDataFormatter($includeAcceptHeader = false, $className = n
// get formatter
if (!empty($extension)) {
$formatter = DataFormatter::for_extension($extension);
} elseif ($includeAcceptHeader && !empty($accept) && strpos($accept, '*/*') === false) {
} elseif ($includeAcceptHeader && !empty($accept) && strpos($accept ?? '', '*/*') === false) {
$formatter = DataFormatter::for_mimetypes($mimetypes);
if (!$formatter) {
$formatter = DataFormatter::for_extension($this->config()->default_extension);
Expand All @@ -419,11 +419,11 @@ protected function getDataFormatter($includeAcceptHeader = false, $className = n

// set custom fields
if ($customAddFields = $this->request->getVar('add_fields')) {
$customAddFields = $formatter->getRealFields($className, explode(',', $customAddFields));
$customAddFields = $formatter->getRealFields($className, explode(',', $customAddFields ?? ''));
$formatter->setCustomAddFields($customAddFields);
}
if ($customFields = $this->request->getVar('fields')) {
$customFields = $formatter->getRealFields($className, explode(',', $customFields));
$customFields = $formatter->getRealFields($className, explode(',', $customFields ?? ''));
$formatter->setCustomFields($customFields);
}
$formatter->setCustomRelations($this->getAllowedRelations($className));
Expand Down Expand Up @@ -537,7 +537,7 @@ protected function putHandler($className, $id)
// or else we'll use the default (XML)
$types = $responseFormatter->supportedExtensions();
$type = '';
if (count($types)) {
if (count($types ?? [])) {
$type = ".{$types[0]}";
}

Expand Down Expand Up @@ -581,7 +581,7 @@ protected function postHandler($className, $id, $relation)
}

if (!Config::inst()->get($className, 'allowed_actions') ||
!in_array($relation, Config::inst()->get($className, 'allowed_actions'))) {
!in_array($relation, Config::inst()->get($className, 'allowed_actions') ?? [])) {
return $this->permissionFailure();
}

Expand Down Expand Up @@ -622,7 +622,7 @@ protected function postHandler($className, $id, $relation)
// or else we'll use the default (XML)
$types = $responseFormatter->supportedExtensions();
$type = '';
if (count($types)) {
if (count($types ?? [])) {
$type = ".{$types[0]}";
}

Expand Down Expand Up @@ -675,11 +675,11 @@ protected function updateDataObject($obj, $formatter)
}

// @todo Disallow editing of certain keys in database
$data = array_diff_key($data, ['ID', 'Created']);
$data = array_diff_key($data ?? [], ['ID', 'Created']);

$apiAccess = singleton($className)->config()->api_access;
if (is_array($apiAccess) && isset($apiAccess['edit'])) {
$data = array_intersect_key($data, array_combine($apiAccess['edit'], $apiAccess['edit']));
$data = array_intersect_key($data ?? [], array_combine($apiAccess['edit'] ?? [], $apiAccess['edit'] ?? []));
}

$obj->update($data);
Expand Down Expand Up @@ -878,7 +878,7 @@ protected function getAllowedRelations($class, $member = null)
$relClass = static::parseRelationClass($relClass);

//remove dot notation from relation names
$parts = explode('.', $relClass);
$parts = explode('.', $relClass ?? '');
$relClass = array_shift($parts);
if (Config::inst()->get($relClass, 'api_access')) {
$allowedRelations[] = $relName;
Expand Down
Loading

0 comments on commit fa77f23

Please sign in to comment.