1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-09 06:36:52 +02:00

Standardize Entries API fetch. #486

BREAKING CHANGES

- method fetch() removed and instead of it should be used fetchSingle() and fetchCollection()
- methods fetchSingle() and fetchCollection() return an instance of Atomastic Arrays.
This commit is contained in:
Awilum
2020-12-04 20:49:37 +03:00
parent a9f1c97f32
commit e8786ca33a
3 changed files with 34 additions and 41 deletions

View File

@@ -73,9 +73,9 @@ flextype()->get('/api/entries', function (Request $request, Response $response)
}
if ($filter === null) {
$response_data['data'] = flextype('entries')->fetchSingle($id);
$response_data['data'] = flextype('entries')->fetchSingle($id)->toArray();
} else {
$response_data['data'] = flextype('entries')->fetchCollection($id, $filter);
$response_data['data'] = flextype('entries')->fetchCollection($id, $filter)->toArray();
}
// Set response code

View File

@@ -54,32 +54,15 @@ class Entries
$this->storage = arrays($this->storage)->set($key, $value)->toArray();
}
/**
* Fetch entry(entries)
*
* @param string $id Unique identifier of the entry(entries).
* @param bool $collection Set TRUE if collection of entries need to be returned.
* @param array $params Parameters array.
*
* @access public
*/
public function fetch(string $id, bool $collection = false, array $params = []): Arrays
{
if ($collection) {
return $this->fetchCollection($id, $params);
}
return $this->fetchSingle($id);
}
/**
* Fetch single entry.
*
* @param string $id Unique identifier of the entry.
* @param string $id Unique identifier of the entry.
* @param array $options Options array.
*
* @access public
*/
public function fetchSingle(string $id): Arrays
public function fetchSingle(string $id, array $options = []): Arrays
{
// Store data
$this->storage['fetch']['id'] = $id;
@@ -99,6 +82,9 @@ class Entries
// Run event: onEntryAfterCacheInitialized
flextype('emitter')->emit('onEntryAfterCacheInitialized');
// Apply filter for fetch data
$this->storage['fetch']['data'] = filter($this->storage['fetch']['data'], $options);
// Return entry from cache
return arrays($this->storage['fetch']['data']);
}
@@ -129,6 +115,9 @@ class Entries
flextype('cache')->set($entryCacheID, $this->storage['fetch']['data']);
}
// Apply filter for fetch data
$this->storage['fetch']['data'] = filter($this->storage['fetch']['data'], $options);
// Return entry data
return arrays($this->storage['fetch']['data']);
}
@@ -140,22 +129,23 @@ class Entries
/**
* Fetch entries collection.
*
* @param string $id Unique identifier of the entry(entries).
* @param array $params Params array.
* @param string $id Unique identifier of the entries collecton.
* @param array $options Options array.
*
* @access public
*/
public function fetchCollection(string $id, array $params = []): Arrays
public function fetchCollection(string $id, array $options = []): Arrays
{
// Store data
$this->storage['fetch']['id'] = $id;
$this->storage['fetch']['data'] = [];
$this->storage['fetch']['id'] = $id;
$this->storage['fetch']['data'] = [];
$this->storage['fetch']['options'] = $options;
// Run event: onEntriesInitialized
flextype('emitter')->emit('onEntriesInitialized');
// Find entries
$entries = find($this->getDirectoryLocation($this->storage['fetch']['id']), $params);
$entries = find($this->getDirectoryLocation($this->storage['fetch']['id']), $options);
if ($entries->hasResults()) {
foreach ($entries as $currenEntry) {
@@ -169,11 +159,14 @@ class Entries
->trim('/')
->toString();
$data[$currentEntryID] = $this->fetchSingle($currentEntryID);
$data[$currentEntryID] = $this->fetchSingle($currentEntryID)->toArray();
}
// Restore fetch id
$this->storage['fetch']['id'] = $id;
$this->storage['fetch']['data'] = filter($data, $params);
// Apply filter for fetch data
$this->storage['fetch']['data'] = filter($data, $options);
// Run event: onEntriesAfterInitialized
flextype('emitter')->emit('onEntriesAfterInitialized');
@@ -186,8 +179,8 @@ class Entries
/**
* Move entry
*
* @param string $id Unique identifier of the entry(entries).
* @param string $newID New Unique identifier of the entry(entries).
* @param string $id Unique identifier of the entry.
* @param string $newID New Unique identifier of the entry.
*
* @return bool True on success, false on failure.
*
@@ -214,7 +207,7 @@ class Entries
/**
* Update entry
*
* @param string $id Unique identifier of the entry(entries).
* @param string $id Unique identifier of the entry.
* @param array $data Data to update for the entry.
*
* @return bool True on success, false on failure.
@@ -243,9 +236,9 @@ class Entries
}
/**
* Create entry
* Create entry.
*
* @param string $id Unique identifier of the entry(entries).
* @param string $id Unique identifier of the entry.
* @param array $data Data to create for the entry.
*
* @return bool True on success, false on failure.
@@ -281,9 +274,9 @@ class Entries
}
/**
* Delete entry
* Delete entry.
*
* @param string $id Unique identifier of the entry(entries).
* @param string $id Unique identifier of the entry.
*
* @return bool True on success, false on failure.
*
@@ -303,10 +296,10 @@ class Entries
}
/**
* Copy entry(s)
* Copy entry.
*
* @param string $id Unique identifier of the entry(entries).
* @param string $newID New Unique identifier of the entry(entries).
* @param string $id Unique identifier of the entry.
* @param string $newID New Unique identifier of the entry.
*
* @return bool|null True on success, false on failure.
*

View File

@@ -12,6 +12,6 @@ use Thunder\Shortcode\Shortcode\ShortcodeInterface;
// Shortcode: [entries_fetch id="entry-id" field="field-name" default="default-value"]
if (flextype('registry')->get('flextype.settings.shortcode.shortcodes.entries.enabled')) {
flextype('shortcode')->addHandler('entries_fetch', static function (ShortcodeInterface $s) {
return arrays(flextype('entries')->fetch($s->getParameter('id')))->get($s->getParameter('field'), $s->getParameter('default'));
return arrays(flextype('entries')->fetchSingle($s->getParameter('id')))->get($s->getParameter('field'), $s->getParameter('default'));
});
}