diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..05a23c2 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2018 izica + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/PhpCollection.php b/PhpCollection.php new file mode 100644 index 0000000..b5e7f33 --- /dev/null +++ b/PhpCollection.php @@ -0,0 +1,200 @@ +arCollection = $data; + } + + /** + * @param $data + * @return PhpCollection + */ + public static function collect($data) + { + return new PhpCollection($data); + } + + /** + * @param $key + * @return PhpCollection + */ + public function pluck($key) + { + $arData = []; + foreach ($this->arCollection as $arItem) { + $arData[] = $arItem[$key]; + } + return new PhpCollection($arData); + } + + /** + * @param $keys + * @return PhpCollection + */ + public function only($keys){ + $fnMap = function ($arItem) use ($keys) { + $arNewItem = []; + foreach ($keys as $key){ + $arNewItem[$key] = $arItem[$key]; + } + return $arNewItem; + }; + + return $this->map($fnMap); + } + + /** + * @param $keys + * @return PhpCollection + */ + public function exclude($keys){ + $arKeys = []; + foreach ($keys as $key){ + $arKeys[$key] = $key; + } + + $fnMap = function ($arItem) use ($arKeys) { + $arNewItem = []; + foreach ($arItem as $sKey => $anyValue){ + if(!isset($arKeys[$sKey])){ + $arNewItem[$sKey] = $arItem[$sKey]; + } + } + return $arNewItem; + }; + + return $this->map($fnMap); + } + + /** + * @param string $glue + * @param string $serializer + * @return string + */ + public function implode($glue = ', ', $serializer = '') + { + $arData = []; + if ($serializer === '') { + foreach ($this->arCollection as $arItem) { + $arData[] = $arItem; + } + } else { + foreach ($this->arCollection as $arItem) { + $arData[] = $serializer($arItem); + } + } + + return implode($glue, $arData); + } + + /** + * @param string $glue + * @return string + */ + public function join($glue = ', ') + { + return $this->implode($glue); + } + + /** + * @param bool $function + * @return PhpCollection + */ + public function filter($function = false) + { + if ($function === false) { + $function = function ($item) { + return true; + }; + } + $arData = []; + foreach ($this->arCollection as $arItem) { + if ($function($arItem)) { + $arData[] = $arItem; + } + } + return new PhpCollection($arData); + } + + /** + * @param bool $function + * @return PhpCollection + */ + public function map($function = false) + { + if ($function === false) { + $function = function ($item) { + return $item; + }; + } + $arData = []; + foreach ($this->arCollection as $arItem) { + $arData[] = $function($arItem); + } + return new PhpCollection($arData); + } + + /** + * @param $key + * @return PhpCollection + */ + public function keyBy($key) + { + $arData = []; + foreach ($this->arCollection as $arItem) { + $arData[$arItem[$key]] = $arItem; + } + return new PhpCollection($arData); + } + + /** + * @param $key + * @return PhpCollection + */ + public function groupBy($key) + { + $arData = []; + foreach ($this->arCollection as $arItem) { + $arData[$arItem[$key]][] = $arItem; + } + return new PhpCollection($arData); + } + + /** + * @return array + */ + public function all() + { + return $this->toArray(); + } + + /** + * @return array + */ + public function toArray() + { + return $this->arCollection; + } + + /** + * @return string + */ + public function toJson() + { + return json_encode($this->arCollection); + } +} diff --git a/README.md b/README.md index 9a9fe06..c0f7113 100644 --- a/README.md +++ b/README.md @@ -1 +1,25 @@ -# php-collection \ No newline at end of file +# Php collection + +inspired by Illuminate\Support\Collection and Lodash + +## Description (TODO) + +```php + $arProductIds = PhpCollection::collect($arProducts)->pluck('id')->all(); + +``` + +* collect($array) -static +* implode($glue, $serializer) +* join -implode alias +* pluck($key) +* only($keys) +* exclude($keys) +* filter(function($item)) +* map(function($item)) +* keyBy($key) +* groupBy($key) +* toArray() +* all -toArray alias +* toJson() +* TODO \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..6352bf5 --- /dev/null +++ b/composer.json @@ -0,0 +1,20 @@ +{ + "name": "izica/php-collection", + "description": "Tools and utilities for working with arrays", + "authors": [ + { + "name": "Golovarchuk Artyom", + "email": "artemiztomska@gmail.com" + } + ], + "keywords": ["php", "array", "arrays", "collection", "collections", "tools", "utility", "tool", "utilities"], + "license": "MIT", + "require": { + "php": ">=5.6.0" + }, + "autoload": { + "classmap": [ + "PhpCollection.php" + ] + } +}