Skip to content

Commit

Permalink
Add support for odata paging using LazyCollection over @odata.nextLink (
Browse files Browse the repository at this point in the history
#142)

* Add support for OData paging using LazyCollection over @odata.nextLink

* Drop support for Laravel 5.8 for use of LazyCollection.

* Add support for setting odata.maxpagesize through new pageSize method on Builder.
  • Loading branch information
anderly authored Sep 8, 2023
1 parent 7bcbfa2 commit c34498a
Show file tree
Hide file tree
Showing 11 changed files with 628 additions and 173 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"php": "^7.3 || ^8.0",
"guzzlehttp/guzzle": "^7.0",
"nesbot/carbon": "^2.0",
"illuminate/support": "^5.8 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0"
"illuminate/support": "^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0"
},
"require-dev": {
"phpunit/phpunit": "^9.0"
Expand Down
2 changes: 1 addition & 1 deletion src/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

class Constants
{
const SDK_VERSION = '0.5.2';
const SDK_VERSION = '0.6.7';

// ODATA Versions to be used when accessing the Web API (see: https://msdn.microsoft.com/en-us/library/gg334391.aspx)
const MAX_ODATA_VERSION = '4.0';
Expand Down
125 changes: 68 additions & 57 deletions src/Entity.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<?php

/**
* Copyright (c) Saint Systems, LLC. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
*
* OData Entity File
* PHP version 7
*
* @category Library
* @package SaintSystems.OData
* @copyright 2017 Saint Systems, LLC
* @license https://opensource.org/licenses/MIT MIT License
* @version GIT: 0.1.0
*/
* Copyright (c) Saint Systems, LLC. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
*
* OData Entity File
* PHP version 7
*
* @category Library
* @package SaintSystems.OData
* @copyright 2017 Saint Systems, LLC
* @license https://opensource.org/licenses/MIT MIT License
* @version GIT: 0.1.0
*/

namespace SaintSystems\OData;

// use Closure;
Expand All @@ -28,13 +30,13 @@
use SaintSystems\OData\Exception\MassAssignmentException;

/**
* Entity class
*
* @package SaintSystems.OData
* @copyright 2017 Saint Systems, LLC
* @license https://opensource.org/licenses/MIT MIT License
* @version Release: 0.1.0
*/
* Entity class
*
* @package SaintSystems.OData
* @copyright 2017 Saint Systems, LLC
* @license https://opensource.org/licenses/MIT MIT License
* @version Release: 0.1.0
*/
class Entity implements ArrayAccess, Arrayable
{
/**
Expand Down Expand Up @@ -124,7 +126,7 @@ class Entity implements ArrayAccess, Arrayable
*
* @var array
*/
protected $guarded = [];//['*'];
protected $guarded = []; //['*'];

/**
* The properties that should be mutated to dates.
Expand Down Expand Up @@ -186,12 +188,12 @@ class Entity implements ArrayAccess, Arrayable
private $entity;

/**
* Construct a new Entity
*
* @param array $properties A list of properties to set
*
* @return Entity
*/
* Construct a new Entity
*
* @param array $properties A list of properties to set
*
* @return Entity
*/
function __construct($properties = array())
{
$this->bootIfNotBooted();
Expand All @@ -210,7 +212,7 @@ function __construct($properties = array())
*/
protected function bootIfNotBooted()
{
if (! isset(static::$booted[static::class])) {
if (!isset(static::$booted[static::class])) {
static::$booted[static::class] = true;

// $this->fireModelEvent('booting', false);
Expand Down Expand Up @@ -241,7 +243,7 @@ protected static function bootTraits()
$class = static::class;

foreach (class_uses_recursive($class) as $trait) {
if (method_exists($class, $method = 'boot'.class_basename($trait))) {
if (method_exists($class, $method = 'boot' . class_basename($trait))) {
forward_static_call([$class, $method]);
}
}
Expand Down Expand Up @@ -307,7 +309,7 @@ public function forceFill(array $properties)
*/
protected function fillableFromArray(array $properties)
{
if (count($this->getFillable()) > 0 && ! static::$unguarded) {
if (count($this->getFillable()) > 0 && !static::$unguarded) {
return array_intersect_key($properties, array_flip($this->getFillable()));
}

Expand Down Expand Up @@ -463,7 +465,7 @@ public function makeVisible($properties)
{
$this->hidden = array_diff($this->hidden, (array) $properties);

if (! empty($this->visible)) {
if (!empty($this->visible)) {
$this->addVisible($properties);
}

Expand Down Expand Up @@ -699,7 +701,7 @@ public function isFillable($key)
return false;
}

return empty($this->getFillable()) && ! Str::startsWith($key, '_');
return empty($this->getFillable()) && !Str::startsWith($key, '_');
}

/**
Expand All @@ -724,10 +726,10 @@ public function totallyGuarded()
}

/**
* Gets the property dictionary of the Entity
*
* @return array The list of properties
*/
* Gets the property dictionary of the Entity
*
* @return array The list of properties
*/
public function getProperties()
{
return $this->properties;
Expand Down Expand Up @@ -865,7 +867,7 @@ public function offsetUnset($offset): void
*/
public function __isset($key)
{
return ! is_null($this->getProperty($key));
return !is_null($this->getProperty($key));
}

/**
Expand Down Expand Up @@ -977,7 +979,7 @@ protected function castProperty($key, $value)
case 'array':
case 'json':
return $this->fromJson($value);
//case 'collection':
//case 'collection':
//return new BaseCollection($this->fromJson($value));
case 'date':
return $this->asDate($value);
Expand All @@ -1004,7 +1006,7 @@ public function setProperty($key, $value)
// which simply lets the developers tweak the property as it is set on
// the entity, such as "json_encoding" a listing of data for storage.
if ($this->hasSetMutator($key)) {
$method = 'set'.Str::studly($key).'Property';
$method = 'set' . Str::studly($key) . 'Property';

return $this->{$method}($value);
}
Expand All @@ -1016,7 +1018,7 @@ public function setProperty($key, $value)
$value = $this->fromDateTime($value);
}

if ($this->isJsonCastable($key) && ! is_null($value)) {
if ($this->isJsonCastable($key) && !is_null($value)) {
$value = $this->asJson($value);
}

Expand All @@ -1040,7 +1042,7 @@ public function setProperty($key, $value)
*/
public function hasSetMutator($key)
{
return method_exists($this, 'set'.Str::studly($key).'Property');
return method_exists($this, 'set' . Str::studly($key) . 'Property');
}

/**
Expand Down Expand Up @@ -1086,7 +1088,8 @@ protected function asDateTime($value)
// when checking the field. We will just return the DateTime right away.
if ($value instanceof DateTimeInterface) {
return Date::parse(
$value->format('Y-m-d H:i:s.u'), $value->getTimezone()
$value->format('Y-m-d H:i:s.u'),
$value->getTimezone()
);
}
// If this value is an integer, we will assume it is a UNIX timestamp's value
Expand Down Expand Up @@ -1165,7 +1168,7 @@ protected function serializeDate(DateTimeInterface $date)
*/
protected function getDateFormat()
{
return $this->dateFormat;// ?: $this->getConnection()->getQueryGrammar()->getDateFormat();
return $this->dateFormat; // ?: $this->getConnection()->getQueryGrammar()->getDateFormat();
}

/**
Expand Down Expand Up @@ -1201,7 +1204,7 @@ protected function asJson($value)
*/
public function fromJson($value, $asObject = false)
{
return json_decode($value, ! $asObject);
return json_decode($value, !$asObject);
}

/**
Expand Down Expand Up @@ -1313,7 +1316,7 @@ public function propertiesToArray()
protected function addDatePropertiesToArray(array $properties)
{
foreach ($this->getDates() as $key) {
if (! isset($properties[$key])) {
if (!isset($properties[$key])) {
continue;
}

Expand All @@ -1338,15 +1341,16 @@ protected function addMutatedPropertiesToArray(array $properties, array $mutated
// We want to spin through all the mutated properties for this model and call
// the mutator for the properties. We cache off every mutated properties so
// we don't have to constantly check on properties that actually change.
if (! array_key_exists($key, $properties)) {
if (!array_key_exists($key, $properties)) {
continue;
}

// Next, we will call the mutator for this properties so that we can get these
// mutated property's actual values. After we finish mutating each of the
// properties we will return this final array of the mutated properties.
$properties[$key] = $this->mutatePropertyForArray(
$key, $properties[$key]
$key,
$properties[$key]
);
}

Expand All @@ -1363,22 +1367,25 @@ protected function addMutatedPropertiesToArray(array $properties, array $mutated
protected function addCastPropertiesToArray(array $properties, array $mutatedProperties)
{
foreach ($this->getCasts() as $key => $value) {
if (! array_key_exists($key, $properties) || in_array($key, $mutatedProperties)) {
if (!array_key_exists($key, $properties) || in_array($key, $mutatedProperties)) {
continue;
}

// Here we will cast the property. Then, if the cast is a date or datetime cast
// then we will serialize the date for the array. This will convert the dates
// to strings based on the date format specified for these Entity models.
$properties[$key] = $this->castProperty(
$key, $properties[$key]
$key,
$properties[$key]
);

// If the property cast was a date or a datetime, we will serialize the date as
// a string. This allows the developers to customize how dates are serialized
// into an array without affecting how they are persisted into the storage.
if ($properties[$key] &&
($value === 'date' || $value === 'datetime')) {
if (
$properties[$key] &&
($value === 'date' || $value === 'datetime')
) {
$properties[$key] = $this->serializeDate($properties[$key]);
}
}
Expand All @@ -1403,7 +1410,7 @@ protected function getArrayableProperties()
*/
protected function getArrayableAppends()
{
if (! count($this->appends)) {
if (!count($this->appends)) {
return [];
}

Expand Down Expand Up @@ -1493,7 +1500,7 @@ protected function getArrayableItems(array $values)
*/
public function getProperty($key)
{
if (! $key) {
if (!$key) {
return;
}

Expand All @@ -1504,8 +1511,10 @@ public function getProperty($key)
// If the property exists in the properties array or has a "get" mutator we will
// get the property's value. Otherwise, we will proceed as if the developers
// are asking for a relationship's value. This covers both types of values.
if (array_key_exists($key, $this->properties) ||
$this->hasGetMutator($key)) {
if (
array_key_exists($key, $this->properties) ||
$this->hasGetMutator($key)
) {
return $this->getPropertyValue($key);
}

Expand Down Expand Up @@ -1547,8 +1556,10 @@ public function getPropertyValue($key)
// If the property is listed as a date, we will convert it to a DateTime
// instance on retrieval, which makes it quite convenient to work with
// date fields without having to create a mutator for each property.
if (in_array($key, $this->getDates()) &&
! is_null($value)) {
if (
in_array($key, $this->getDates()) &&
!is_null($value)
) {
return $this->asDateTime($value);
}

Expand Down Expand Up @@ -1576,7 +1587,7 @@ protected function getPropertyFromArray($key)
*/
public function hasGetMutator($key)
{
return method_exists($this, 'get'.Str::studly($key).'Property');
return method_exists($this, 'get' . Str::studly($key) . 'Property');
//return method_exists($this, 'get_'.$key);
}

Expand All @@ -1589,7 +1600,7 @@ public function hasGetMutator($key)
*/
protected function mutateProperty($key, $value)
{
return $this->{'get'.Str::studly($key).'Property'}($value);
return $this->{'get' . Str::studly($key) . 'Property'}($value);
// return $this->{'get_'.$key}($value);
}

Expand Down
Loading

0 comments on commit c34498a

Please sign in to comment.