diff --git a/composer.json b/composer.json index cbea0cf..f4047b6 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,6 @@ ], "require": { "php": ">=7.0", - "illuminate/support": "~5.2", "nesbot/carbon": "^1.22" }, "autoload": { @@ -34,6 +33,7 @@ }, "require-dev": { "orchestra/testbench": "~3.2", - "phpunit/phpunit": "^6.1" + "phpunit/phpunit": "^6.1", + "illuminate/support": "~5.2" } } diff --git a/src/FeedItem.php b/src/FeedItem.php index e8859cc..80dfd66 100644 --- a/src/FeedItem.php +++ b/src/FeedItem.php @@ -4,10 +4,12 @@ use BadMethodCallException; use Carbon\Carbon; -use Illuminate\Support\Collection; +use Mateusjatenee\JsonFeed\Traits\ArrayHelpers; class FeedItem { + use ArrayHelpers; + /** * @var array */ @@ -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) + ) + ); } /** @@ -72,7 +78,7 @@ public function build() */ public function toArray() { - return $this->build()->toArray(); + return $this->build(); } /** diff --git a/src/JsonFeed.php b/src/JsonFeed.php index c11b0ed..34e3852 100644 --- a/src/JsonFeed.php +++ b/src/JsonFeed.php @@ -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 */ @@ -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); } /** @@ -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()]; } /** @@ -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()); } /** @@ -118,7 +119,7 @@ public function getAcceptedProperties() */ public function setItems($items) { - $this->items = $this->makeCollection($items); + $this->items = $this->makeArray($items); return $this; } @@ -139,7 +140,7 @@ public function getItems() */ public function setConfig($config) { - $this->properties = $this->makeCollection($config); + $this->properties = $this->makeArray($config); return $this; } @@ -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); } /** @@ -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)); } /** @@ -183,7 +184,7 @@ protected function filterProperties($array = null) */ protected function getProperty($property) { - return $this->properties->get($property); + return $this->properties[$property] ?? null; } /** @@ -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); } /** diff --git a/src/JsonFeedServiceProvider.php b/src/JsonFeedServiceProvider.php index 84130ab..0c654b7 100644 --- a/src/JsonFeedServiceProvider.php +++ b/src/JsonFeedServiceProvider.php @@ -2,7 +2,6 @@ namespace Mateusjatenee\JsonFeed; -use Illuminate\Support\Collection; use Illuminate\Support\ServiceProvider; use Mateusjatenee\JsonFeed\JsonFeed; @@ -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)); - }); } } diff --git a/src/Traits/ArrayHelpers.php b/src/Traits/ArrayHelpers.php new file mode 100644 index 0000000..03304ef --- /dev/null +++ b/src/Traits/ArrayHelpers.php @@ -0,0 +1,40 @@ +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; + } +} diff --git a/tests/JsonFeedTest.php b/tests/JsonFeedTest.php index 288abcb..4d52d30 100644 --- a/tests/JsonFeedTest.php +++ b/tests/JsonFeedTest.php @@ -80,10 +80,12 @@ 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 */ @@ -91,8 +93,7 @@ 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 */ @@ -100,7 +101,7 @@ 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 */ @@ -108,7 +109,7 @@ 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 */