Skip to content

Commit

Permalink
2.0.3 - return something sensible when passed null
Browse files Browse the repository at this point in the history
  • Loading branch information
marionnewlevant committed Feb 14, 2018
1 parent 8255a87 commit bbdf05c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 21 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Agnostic Fetch Changelog

## 2.0.3 - 2018-02-14
### Added
- return something sensible when passed `null`

## 2.0.2 - 2018-02-13
### Fixed
- typo in documentation and update urls (`agnostic`, not `agnositic`)
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "marionnewlevant/agnostic-fetch",
"description": "uniform syntax for fetching elements whether or not they have been eager loaded",
"type": "craft-plugin",
"version": "2.0.2",
"version": "2.0.3",
"keywords": [
"craft", "craftcms", "craft-plugin", "agnostic-fetch", "eager"
],
Expand Down
8 changes: 0 additions & 8 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@
use Craft;

/**
* Craft plugins are very much like little applications in and of themselves. We’ve made
* it as simple as we can, but the training wheels are off. A little prior knowledge is
* going to be required to write a plugin.
*
* For the purposes of the plugin docs, we’re going to assume that you know PHP and SQL,
* as well as some semi-advanced concepts like object-oriented programming and PHP namespaces.
*
* https://craftcms.com/docs/plugins/introduction
*
* @author Marion Newlevant
* @package AgnosticFetch
Expand Down
39 changes: 27 additions & 12 deletions src/helpers/AgnosticFetchHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,56 +10,71 @@ public static function isEager($stuff)

public static function all($stuff)
{
if (is_null($stuff))
{
return [];
}
if (is_array($stuff))
{
return ($stuff);
} else {
return $stuff->all();
}
return $stuff->all();
}

public static function one($stuff)
{
if (is_null($stuff))
{
return null;
}
if (is_array($stuff))
{
return (count($stuff) ? $stuff[0] : null);
} else {
return $stuff->one();
}
return $stuff->one();
}

public static function nth($stuff, $n=0)
{
if (is_null($stuff))
{
return null;
}
if (is_array($stuff))
{
$count = count($stuff);
return (0 <= $n && $n < $count ? $stuff[$n] : null);
} else {
return $stuff->nth($n);
}
return $stuff->nth($n);
}

public static function ids($stuff)
{
if (is_null($stuff))
{
return [];
}
if (is_array($stuff))
{
$result = array();
$result = [];
foreach ($stuff as $thing) {
$result[] = $thing->id;
}
return $result;
} else {
return $stuff->ids();
}
return $stuff->ids();
}

public static function count($stuff)
{
if (is_null($stuff))
{
return 0;
}
if (is_array($stuff))
{
return count($stuff);
} else {
return $stuff->count();
}
return $stuff->count();
}
}
}

0 comments on commit bbdf05c

Please sign in to comment.