Skip to content

Commit

Permalink
Finish 1.0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
paulgoodchild committed Feb 24, 2023
2 parents 48b8f7c + 4be74cf commit 285c3a5
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 145 deletions.
5 changes: 1 addition & 4 deletions src/Entities/BankAccounts/BankAccountsIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ public function current() {
return parent::current();
}

/**
* @return RetrievePage
*/
protected function getNewRetriever() {
protected function getNewRetriever() :RetrievePage {
return new RetrievePage();
}
}
7 changes: 3 additions & 4 deletions src/Entities/BankTransactionExplanation/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ class Create extends Base {
/**
* @return BankTransactionExplanationVO|null
*/
public function create() {

$sDescription = $this->getRequestDataItem( 'description' );
if ( empty( $sDescription ) ) {
public function create() :?BankTransactionExplanationVO {
$description = $this->getRequestDataItem( 'description' );
if ( empty( $description ) ) {
$this->setDescription( 'Filler description for Bank Transaction Explanation.' );
}
return $this->sendRequestWithVoResponse();
Expand Down
14 changes: 14 additions & 0 deletions src/Entities/BankTransactionExplanation/Delete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php declare( strict_types=1 );

namespace FernleafSystems\ApiWrappers\Freeagent\Entities\BankTransactionExplanation;

use FernleafSystems\ApiWrappers\Freeagent\Entities\Contacts\Base;

class Delete extends Base {

public const REQUEST_METHOD = 'delete';

public function delete( int $explanationID ) :self {
return $this->setEntityId( $explanationID )->send();
}
}
5 changes: 1 addition & 4 deletions src/Entities/BankTransactions/BankTransactionsIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ public function current() {
return parent::current();
}

/**
* @return RetrievePage
*/
protected function getNewRetriever() {
protected function getNewRetriever() :RetrievePage {
return new RetrievePage();
}
}
5 changes: 1 addition & 4 deletions src/Entities/Bills/BillsIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ public function current() {
return parent::current();
}

/**
* @return RetrievePage
*/
protected function getNewRetriever() {
protected function getNewRetriever() :RetrievePage {
return new RetrievePage();
}
}
81 changes: 28 additions & 53 deletions src/Entities/Common/CommonIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,82 +9,67 @@ abstract class CommonIterator extends AbstractPagedIterator {

use ConnectionConsumer;

const PAGE_LIMIT = RetrievePageBase::PER_PAGE_LIMIT_UPPER;
public const PAGE_LIMIT = RetrievePageBase::PER_PAGE_LIMIT_UPPER;

/**
* @var RetrievePageBase
*/
private $oPageRetriever;
private $pageRetriever;

/**
* @var int
*/
protected $nTotalSize;

/**
* @param $nTimestamp
* @return $this
* @param int $ts
*/
public function filterByDateFrom( $nTimestamp ) {
$this->getRetriever()
->filterByDateFrom( $nTimestamp );
public function filterByDateFrom( $ts ) :self {
$this->getRetriever()->filterByDateFrom( $ts );
return $this;
}

/**
* @param $nTimestamp
* @return $this
* @param int $ts
*/
public function filterByDateTo( $nTimestamp ) {
$this->getRetriever()
->filterByDateTo( $nTimestamp );
public function filterByDateTo( $ts ) :self {
$this->getRetriever()->filterByDateTo( $ts );
return $this;
}

/**
* @param int $nDateTs
* @param int $nRadius
* @return $this
*/
public function filterByDateRange( $nDateTs, $nRadius = 5 ) {
public function filterByDateRange( $nDateTs, $nRadius = 5 ) :self {
$nDaysRadius = 86400*$nRadius;
return $this->filterByDateFrom( $nDateTs - $nDaysRadius )
->filterByDateTo( $nDateTs + $nDaysRadius );
}

/**
* @param $nTimestamp
* @return $this
* @param $ts
*/
public function filterByDateUpdatedSince( $nTimestamp ) {
$this->getRetriever()
->filterByDateUpdatedSince( $nTimestamp );
public function filterByDateUpdatedSince( $ts ) :self {
$this->getRetriever()->filterByDateUpdatedSince( $ts );
return $this;
}

public function filterByView( string $view ) :self {
$this->getRetriever()
->filterByView( $view );
$this->getRetriever()->filterByView( $view );
return $this;
}

/**
* @return $this;
*/
public function filterByView_All() {
$this->getRetriever()
->filterByView_All();
public function filterByView_All() :self {
$this->getRetriever()->filterByView_All();
return $this;
}

/**
* @param string $field
* @param bool $bDescendingOrder
* @return $this
*/
public function orderBy( $field, $bDescendingOrder = false ) {
$sPrefix = $bDescendingOrder ? '-' : '';
$this->getRetriever()->setRequestDataItem( 'sort', $sPrefix.$field );
public function orderBy( $field, bool $descendingOrder = false ) :self {
$this->getRetriever()->setRequestDataItem( 'sort', ( $descendingOrder ? '-' : '' ).$field );
return $this;
}

Expand All @@ -93,38 +78,28 @@ public function orderBy( $field, $bDescendingOrder = false ) {
*/
public function getTotalSize() {
if ( !isset( $this->nTotalSize ) ) {
$oRtr = $this->getRetriever();
$oRtr->retrieve( 1, 1 );
$aCount = $oRtr->getLastApiResponse()
->getHeader( 'X-Total-Count' );
$this->nTotalSize = (int)array_shift( $aCount );
$r = $this->getRetriever();
$r->retrieve( 1, 1 );
$count = $r->getLastApiResponse()->getHeader( 'X-Total-Count' );
$this->nTotalSize = (int)array_shift( $count );
}
return $this->nTotalSize;
}

/**
* @param int $nPage
* @param int $pageNumber
* @return EntityVO[]|array
*/
public function getPage( $nPage ) {
return $this->getRetriever()->retrieve( $nPage, $this->getPageSize() );
public function getPage( $pageNumber ) {
return $this->getRetriever()->retrieve( $pageNumber, $this->getPageSize() );
}

/**
* @return RetrievePageBase
*/
protected function getRetriever() {
if ( !$this->oPageRetriever instanceof RetrievePageBase ) {
$this->oPageRetriever = $this->getNewRetriever()
->setConnection( $this->getConnection() );
}
return $this->oPageRetriever;
public function getRetriever() :RetrievePageBase {
return $this->pageRetriever ??
$this->pageRetriever = $this->getNewRetriever()->setConnection( $this->getConnection() );
}

/**
* @return RetrievePageBase
*/
abstract protected function getNewRetriever();
abstract protected function getNewRetriever() :RetrievePageBase;

/**
* @return int
Expand Down
99 changes: 42 additions & 57 deletions src/Entities/Common/RetrievePageBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,104 +7,89 @@

abstract class RetrievePageBase extends Api {

const PER_PAGE_LIMIT_LOWER = 1;
const PER_PAGE_LIMIT_UPPER = 100;
public const PER_PAGE_LIMIT_LOWER = 1;
public const PER_PAGE_LIMIT_UPPER = 100;

/**
* @param int $nPage
* @param int $nPerPage
* @param int $page
* @param int $perPage
* @return EntityVO[]|array
*/
public function retrieve( $nPage, $nPerPage = self::PER_PAGE_LIMIT_UPPER ) {
$aResults = [];
public function retrieve( $page, $perPage = self::PER_PAGE_LIMIT_UPPER ) :array {
try {
$this->setPage( $nPage )
->setPerPage( $nPerPage )
->send();
$aResults = array_map(
function ( $aResultItem ) {
return $this->getVO()
->applyFromArray( $aResultItem );
},
$this->getCoreResponseData()
$results = array_map(
fn( $result ) => $this->getVO()->applyFromArray( $result ),
$this->setPage( $page )
->setPerPage( $perPage )
->send()
->getCoreResponseData()
);
}
catch ( \Exception $oE ) {
catch ( \Exception $e ) {
$results = [];
}

return $aResults;
return $results;
}

/**
* @param int $nPage
* @return $this
* @param int $page
*/
public function setPage( $nPage = 1 ) {
return $this->setRequestDataItem( 'page', max( 1, $nPage + 1 ) ); //because pages start at 0
public function setPage( $page = 1 ) :self {
return $this->setRequestDataItem( 'page', max( 1, $page + 1 ) ); //because pages start at 0
}

/**
* @param int $nPerPage
* @return $this
* @param int $perPage
*/
public function setPerPage( $nPerPage = 25 ) {
$nPerPage = min( max( (int)$nPerPage, self::PER_PAGE_LIMIT_LOWER ), self::PER_PAGE_LIMIT_UPPER );
return $this->setRequestDataItem( 'per_page', $nPerPage );
public function setPerPage( $perPage = 25 ) :self {
return $this->setRequestDataItem(
'per_page',
min( max( (int)$perPage, self::PER_PAGE_LIMIT_LOWER ), self::PER_PAGE_LIMIT_UPPER )
);
}

/**
* @param string $sViewFilter
* @return $this
* @param string $viewFilter
*/
public function filterByView( $sViewFilter ) {
return $this->setRequestDataItem( 'view', $sViewFilter );
public function filterByView( $viewFilter ) :self {
return $this->setRequestDataItem( 'view', $viewFilter );
}

/**
* @return $this
*/
public function filterByView_All() {
public function filterByView_All() :self {
return $this->filterByView( 'all' );
}

/**
* @param int $nTimestamp Unix Timestamp
* @return $this
* @param int $ts Unix Timestamp
*/
public function filterByDateFrom( $nTimestamp ) {
return $this->setRequestDataItem( 'from_date', $this->convertToStdDateFormat( $nTimestamp ) );
public function filterByDateFrom( $ts ) :self {
return $this->setRequestDataItem( 'from_date', $this->convertToStdDateFormat( $ts ) );
}

/**
* @param int $nTimestamp Unix Timestamp
* @return $this
* @param int $ts Unix Timestamp
*/
public function filterByDateTo( $nTimestamp ) {
return $this->setRequestDataItem( 'to_date', $this->convertToStdDateFormat( $nTimestamp ) );
public function filterByDateTo( $ts ) :self {
return $this->setRequestDataItem( 'to_date', $this->convertToStdDateFormat( $ts ) );
}

/**
* @param int $nTimestamp Unix Timestamp
* @return $this
* @param int $ts Unix Timestamp
*/
public function filterByDateUpdatedSince( $nTimestamp ) {
return $this->setRequestDataItem( 'updated_since', $this->convertToStdDateFormat( $nTimestamp ) );
public function filterByDateUpdatedSince( $ts ) :self {
return $this->setRequestDataItem( 'updated_since', $this->convertToStdDateFormat( $ts ) );
}

/**
* @param int $nDate
* @param int $nRadius
* @return $this
* @param int $date
* @param int $radius
*/
public function filterByDateRange( $nDate, $nRadius = 5 ) {
$nDaysRadius = 86400*$nRadius;
return $this->filterByDateFrom( $nDate - $nDaysRadius )
->filterByDateTo( $nDate + $nDaysRadius );
public function filterByDateRange( $date, $radius = 5 ) :self {
$daysRadius = 86400*$radius;
return $this->filterByDateFrom( $date - $daysRadius )
->filterByDateTo( $date + $daysRadius );
}

/**
* @return string
*/
protected function getRequestDataPayloadKey() :string {
return $this->getApiEndpoint(); // we don't truncate 's'
}
Expand Down
5 changes: 1 addition & 4 deletions src/Entities/Contacts/ContactsIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,7 @@ public function current() {
return parent::current();
}

/**
* @return RetrievePage
*/
protected function getNewRetriever() {
protected function getNewRetriever() :RetrievePage {
return new RetrievePage();
}
}
Loading

0 comments on commit 285c3a5

Please sign in to comment.