Skip to content

Commit

Permalink
Merge pull request #3 from mateusjatenee/remove-collections-requirement
Browse files Browse the repository at this point in the history
Remove collections requirement
  • Loading branch information
mateusjatenee authored May 23, 2017
2 parents 388b289 + c54e8e0 commit 6a098c7
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 49 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
],
"require": {
"php": ">=7.0",
"illuminate/support": "~5.2",
"nesbot/carbon": "^1.22"
},
"autoload": {
Expand All @@ -34,6 +33,7 @@
},
"require-dev": {
"orchestra/testbench": "~3.2",
"phpunit/phpunit": "^6.1"
"phpunit/phpunit": "^6.1",
"illuminate/support": "~5.2"
}
}
28 changes: 17 additions & 11 deletions src/FeedItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

use BadMethodCallException;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Mateusjatenee\JsonFeed\Traits\ArrayHelpers;

class FeedItem
{
use ArrayHelpers;

/**
* @var array
*/
Expand Down Expand Up @@ -46,23 +48,27 @@ class FeedItem
public function __construct($object, array $attachments = [])
{
$this->object = $object;
$this->attachments = new Collection($attachments);
$this->attachments = $this->makeArray($attachments);
}

/**
* Builds the structure of the feed item
*
* @return \Illuminate\Support\Collection
* @return array
*/
public function build()
{
return (new Collection($this->acceptedProperties))->flatMap(function ($property) {
$method = 'get' . studly_case($property);

return [$property => $this->$method()];
})->reject(function ($value, $property) {
return empty($value);
});
return array_filter(
$this->collapse(
array_map(function ($property) {
$method = 'get' . studly_case($property);

return [
$property => $this->$method(),
];
}, $this->acceptedProperties)
)
);
}

/**
Expand All @@ -72,7 +78,7 @@ public function build()
*/
public function toArray()
{
return $this->build()->toArray();
return $this->build();
}

/**
Expand Down
40 changes: 16 additions & 24 deletions src/JsonFeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
use Illuminate\Support\Collection;
use Mateusjatenee\JsonFeed\Exceptions\IncorrectFeedStructureException;
use Mateusjatenee\JsonFeed\FeedItem;
use Mateusjatenee\JsonFeed\Traits\ArrayHelpers;

class JsonFeed
{
use ArrayHelpers;

/**
* @var array
*/
Expand Down Expand Up @@ -47,8 +50,8 @@ class JsonFeed
*/
public function __construct($properties = [], $items = [])
{
$this->properties = $this->makeCollection($properties);
$this->items = $this->makeCollection($items);
$this->properties = $this->makeArray($properties);
$this->items = $this->makeArray($items);
}

/**
Expand All @@ -73,16 +76,14 @@ public function build()
if (!$this->hasCorrectStructure()) {
$missingProperties = array_diff(
$this->requiredProperties,
$this->filterProperties($this->requiredProperties)->keys()->all()
array_keys($this->filterProperties($this->requiredProperties))
);

throw (new IncorrectFeedStructureException)->setProperties($missingProperties);
}

return $this
->filterProperties()
->put('version', $this->getVersion())
->put('items', $this->buildItems()->all());
->filterProperties() + ['version' => $this->getVersion(), 'items' => $this->buildItems()];
}

/**
Expand All @@ -92,12 +93,12 @@ public function build()
*/
public function toArray()
{
return $this->build()->toArray();
return $this->build();
}

public function toJson()
{
return $this->build()->toJson();
return json_encode($this->build());
}

/**
Expand All @@ -118,7 +119,7 @@ public function getAcceptedProperties()
*/
public function setItems($items)
{
$this->items = $this->makeCollection($items);
$this->items = $this->makeArray($items);

return $this;
}
Expand All @@ -139,7 +140,7 @@ public function getItems()
*/
public function setConfig($config)
{
$this->properties = $this->makeCollection($config);
$this->properties = $this->makeArray($config);

return $this;
}
Expand All @@ -161,7 +162,7 @@ public function getConfig()
*/
protected function hasCorrectStructure()
{
return $this->filterProperties($this->requiredProperties)->count() === count($this->requiredProperties);
return count($this->filterProperties($this->requiredProperties)) === count($this->requiredProperties);
}

/**
Expand All @@ -172,7 +173,7 @@ protected function hasCorrectStructure()
*/
protected function filterProperties($array = null)
{
return $this->properties->intersectByKeys(array_flip($array ?? $this->acceptedProperties));
return array_intersect_key($this->properties, array_flip($array ?? $this->acceptedProperties));
}

/**
Expand All @@ -183,7 +184,7 @@ protected function filterProperties($array = null)
*/
protected function getProperty($property)
{
return $this->properties->get($property);
return $this->properties[$property] ?? null;
}

/**
Expand All @@ -196,23 +197,14 @@ public function getVersion()
return $this->version;
}

/**
* @param $items
* @return mixed
*/
protected function makeCollection($items)
{
return $items instanceof Collection ? $items : new Collection($items);
}

/**
* @return mixed
*/
protected function buildItems()
{
return $this->items->map(function ($item) {
return array_map(function ($item) {
return FeedItem::setItem($item)->toArray();
});
}, $this->items);
}

/**
Expand Down
5 changes: 0 additions & 5 deletions src/JsonFeedServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Mateusjatenee\JsonFeed;

use Illuminate\Support\Collection;
use Illuminate\Support\ServiceProvider;
use Mateusjatenee\JsonFeed\JsonFeed;

Expand All @@ -22,9 +21,5 @@ public function register()
$this->app->singleton('jsonFeed', function ($app) use ($config) {
return new JsonFeed($config);
});

Collection::macro('intersectByKeys', function ($array) {
return new static(array_intersect_key($this->items, $array));
});
}
}
40 changes: 40 additions & 0 deletions src/Traits/ArrayHelpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Mateusjatenee\JsonFeed\Traits;

use Illuminate\Support\Collection;

trait ArrayHelpers
{
/**
* @param $items
* @return mixed
*/
protected function makeArray($items)
{
return $items instanceof Collection ? $items->toArray() : $items;
}

/**
* Collapse an array of arrays into a single array.
*
* @param array $array
* @return array
*/
protected function collapse($array)
{
$results = [];

foreach ($array as $values) {
if ($values instanceof Collection) {
$values = $values->all();
} elseif (!is_array($values)) {
continue;
}

$results = array_merge($results, $values);
}

return $results;
}
}
15 changes: 8 additions & 7 deletions tests/JsonFeedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,35 +80,36 @@ public function it_automatically_gets_properties()
}

/** @test */
public function it_automatically_converts_an_array_to_a_collection()
public function it_automatically_converts_a_collection_to_array()
{
$feed = JsonFeed::start([], [new DummyFeedItem]);
$this->assertInstanceOf(Collection::class, $feed->getItems());
$feed = JsonFeed::start(new Collection([]), new Collection([new DummyFeedItem]));

$this->assertInternalType('array', $feed->getItems());
$this->assertInternalType('array', $feed->getConfig());
}

/** @test */
public function it_does_not_add_a_collection_inside_another_collection()
{
$feed = JsonFeed::start([], collect([new DummyFeedItem]));

$this->assertInstanceOf(Collection::class, $feed->getItems());
$this->assertInstanceOf(DummyFeedItem::class, $feed->getItems()->first());
$this->assertInstanceOf(DummyFeedItem::class, $feed->getItems()[0]);
}

/** @test */
public function it_sets_the_config()
{
$feed = app('jsonFeed')->setConfig($config = $this->getJsonFeedConfig());

$this->assertEquals(count($config), $feed->getConfig()->count());
$this->assertEquals(count($config), count($feed->getConfig()));
}

/** @test */
public function it_sets_the_items()
{
$feed = app('jsonFeed')->setItems($items = $this->getArrayOfItems());

$this->assertEquals(count($items), $feed->getItems()->count());
$this->assertEquals(count($items), count($feed->getItems()));
}

/** @test */
Expand Down

0 comments on commit 6a098c7

Please sign in to comment.