fluent-booking
/
vendor
/
wpfluent
/
framework
/
src
/
WPFluent
/
Database
/
Orm
/
ResourceAbleTrait.php
ResourceAbleTrait.php in Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution 1.5.21, at vendor/wpfluent/framework/src/WPFluent/Database/Orm/ResourceAbleTrait.php
| 1 | <?php |
| 2 | |
| 3 | namespace FluentBooking\Framework\Database\Orm; |
| 4 | |
| 5 | use BadMethodCallException; |
| 6 | use FluentBooking\Framework\Support\Collection; |
| 7 | |
| 8 | trait ResourceAbleTrait |
| 9 | { |
| 10 | /** |
| 11 | * Transforms a resource |
| 12 | * |
| 13 | * @param array $excludes |
| 14 | * @return mixed |
| 15 | */ |
| 16 | public function toResource($excludes = []) |
| 17 | { |
| 18 | if ($this instanceof Model) { |
| 19 | throw new BadMethodCallException( |
| 20 | 'Implement ' . __FUNCTION__ .' method in you ' . get_class($this) . ' Model.' |
| 21 | ); |
| 22 | } |
| 23 | |
| 24 | $items = []; |
| 25 | |
| 26 | foreach ($this->all() as $item) { |
| 27 | $items[] = $item->toResource($excludes); |
| 28 | } |
| 29 | |
| 30 | $collection = new Collection($items); |
| 31 | |
| 32 | if ($this instanceof Collection) { |
| 33 | return $collection; |
| 34 | } |
| 35 | |
| 36 | return $this->setCollection($collection); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Filter the resource to exclude properties |
| 41 | * |
| 42 | * @param array $resource |
| 43 | * @param array $excludes |
| 44 | * @return array |
| 45 | */ |
| 46 | public function filterResource($resource, $excludes) |
| 47 | { |
| 48 | if ($excludes) { |
| 49 | foreach ($resource as $key => $value) { |
| 50 | if (in_array($key, $excludes)) { |
| 51 | unset($resource[$key]); |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | return $resource; |
| 57 | } |
| 58 | } |
| 59 |