Skip to content
This repository has been archived by the owner on Jan 14, 2020. It is now read-only.

Commit

Permalink
Merge pull request #113 from apility/develop
Browse files Browse the repository at this point in the history
Merge develop into master
  • Loading branch information
olavoyen authored Dec 2, 2019
2 parents 3466be0 + 5feda37 commit 82828de
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 12 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ require_once(Netflex\SDK::bootstrap);
composer run-script tests
```

## Updating snapshots

When an existing snapshot test has been updated, or its behaviour has been modified, the snapshot will have to be updated to prevent the tests from failing.

To update the snapshots, run the following command.

```bash
composer run-script tests:update-snapshots
```

<hr>

Copyright &copy; 2019 **[Apility AS](https://apility.no)**
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"spatie/phpunit-snapshot-assertions": "^2.1"
},
"scripts": {
"tests": "vendor/bin/phpunit --testdox --colors=always"
"tests": "vendor/bin/phpunit --testdox --colors=always",
"tests:update-snapshots": "vendor/bin/phpunit --colors=always -d --update-snapshots"
}
}
3 changes: 3 additions & 0 deletions src/cli.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

global $_mode;

$_GET['_path'] = '';
$_mode = 'cli';

NF::setRoot(realpath(__DIR__ . '/../../../../') . '/');
NF::init();
Expand Down
62 changes: 52 additions & 10 deletions src/model/Structure.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ abstract class Structure implements ArrayAccess, Serializable, JsonSerializable

use FieldMapping;

/**
* Allows fetching of unpublished entries
*
* @var bool
*/
protected static $_fetch_unpublished = true;

protected static $_booted = false;
protected static $_hooks = [];
protected static $_existing_hooks = [
Expand Down Expand Up @@ -75,6 +82,33 @@ public function __construct($attributes = [])
static::bootUnlessBooted();
}

public function getPublishedAttribute ($published) {
global $_mode;

if ($published && $this->use_time) {
$start = null;

try {
$start = Carbon::parse($this->start);
} catch (Exception $ex) {
$start = Carbon::parse(0);
}

try {
$stop = $this->stop ? $this->stop : PHP_INT_MAX;
$stop = Carbon::parse($stop);
} catch (Exception $ex) {
$stop = Carbon::parse(PHP_INT_MAX);
}

$now = Carbon::now();

return $now->gte($start) && $now->lte($stop);
}

return (bool) $published;
}

public function save()
{
static::performHookOn($this, 'saving');
Expand Down Expand Up @@ -273,7 +307,7 @@ public static function all()
$cacheKey = 'entry/' . $entry['id'];
NF::$cache->save($cacheKey, $entry);
return static::generateObject($entry);
}, $response))->values();
}, $response))->filter()->values();
}

public static function find($id)
Expand Down Expand Up @@ -386,63 +420,63 @@ public static function resolveOrFail($slug)
public static function query(...$args)
{
$structureId = (new static)->directory;
$query = new StructureQuery($structureId, new static);
$query = new StructureQuery($structureId, new static, static::$_fetch_unpublished);

return call_user_func_array([$query, 'query'], $args);
}

public static function count()
{
$structureId = (new static)->directory;
$query = new StructureQuery($structureId, new static);
$query = new StructureQuery($structureId, new static, static::$_fetch_unpublished);

return call_user_func_array([$query, 'count'], []);
}

public static function where(...$args)
{
$structureId = (new static)->directory;
$query = new StructureQuery($structureId, new static);
$query = new StructureQuery($structureId, new static, static::$_fetch_unpublished);

return call_user_func_array([$query, 'where'], $args);
}

public static function pluck(...$args)
{
$structureId = (new static)->directory;
$query = new StructureQuery($structureId, new static);
$query = new StructureQuery($structureId, new static, static::$_fetch_unpublished);

return call_user_func_array([$query, 'pluck'], $args);
}

public static function whereBetween(...$args)
{
$structureId = (new static)->directory;
$query = new StructureQuery($structureId, new static);
$query = new StructureQuery($structureId, new static, static::$_fetch_unpublished);

return call_user_func_array([$query, 'whereBetween'], $args);
}

public static function orderBy(...$args)
{
$structureId = (new static)->directory;
$query = new StructureQuery($structureId, new static);
$query = new StructureQuery($structureId, new static, static::$_fetch_unpublished);

return call_user_func_array([$query, 'orderBy'], $args);
}

public static function first()
{
$structureId = (new static)->directory;
$query = new StructureQuery($structureId, new static);
$query = new StructureQuery($structureId, new static, static::$_fetch_unpublished);

return call_user_func_array([$query, 'firstOrFail'], []);
}

public static function paginate(...$args)
{
$structureId = (new static)->directory;
$query = new StructureQuery($structureId, new static);
$query = new StructureQuery($structureId, new static, static::$_fetch_unpublished);

return call_user_func_array([$query, 'paginate'], $args);
}
Expand All @@ -462,7 +496,15 @@ public static function generateObject($data)
return $revision;
}

return new static($data);
$entry = new static($data);

if ($entry) {
if (!$_mode && static::$_fetch_unpublished === false || static::$_fetch_unpublished === false && $_mode === 'cli') {
return $entry->published ? $entry : null;
}

return $entry;
}
}

private static function bootUnlessBooted()
Expand Down
6 changes: 5 additions & 1 deletion src/model/StructureQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@ class StructureQuery
private $_firstPageSize = null;
private $_fieldsModified = false;

public function __construct($directory, $class)
public function __construct($directory, $class, $fetchUnpublished = true)
{
$this->_class = $class;
$this->_directory = $directory;
$this->_search = new ElasticSearch();
$this->_search->directory($this->_directory);
$this->_search->limit(10000);

if (!$fetchUnpublished) {
$this->_search->where('published', true);
}
}

private function isArgsArray($args)
Expand Down

0 comments on commit 82828de

Please sign in to comment.