-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
266 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
<?php | ||
|
||
/** | ||
* Class PhpCollection | ||
*/ | ||
class PhpCollection | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private $arCollection = []; | ||
|
||
/** | ||
* PhpCollection constructor. | ||
* @param $data | ||
*/ | ||
public function __construct($data) | ||
{ | ||
$this->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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,25 @@ | ||
# php-collection | ||
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
] | ||
} | ||
} |