Skip to content

Commit

Permalink
Merge with array decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
tkivelip committed Jan 25, 2022
1 parent 16832e7 commit 5db4e38
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 1 deletion.
143 changes: 143 additions & 0 deletions src/ArrayDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php

namespace mindtwo\LaravelDecorator;

use ArrayAccess;
use mindtwo\LaravelDecorator\Exceptions\DecoratorException;
use ReflectionMethod;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;

abstract class ArrayDecorator implements ArrayAccess
{
/**
* @var array
*/
protected $data;

/**
* MiteEntryDecorator constructor.
*
* @param array $data
*/
public function __construct(array $data)
{
$this->data = $data;
}

/**
* ArrayAccess set.
*
* @param mixed $offset
* @param mixed $value
*/
public function offsetSet($offset, $value)
{
$this->data[$offset] = $value;
}

/**
* ArrayAccess unset.
*
* @param mixed $offset
*/
public function offsetUnset($offset)
{
unset($this->data[$offset]);
}

/**
* ArrayAccess exists.
*
* @param mixed $offset
*
* @return bool
*/
public function offsetExists($offset)
{
return isset($this->data[$offset]);
}

/**
* ArrayAccess get.
*
* @param mixed $offset
*
* @return mixed
*/
public function offsetGet($offset)
{
return $this->$offset;
}

/**
* Determinates if a string represents a valid mutator method.
*
* @param string $method
*
* @throws \ReflectionException
*
* @return bool
*/
protected function isMutatorMethod(string $method): bool
{
if (! method_exists($this, $method)) {
return false;
}

$reflection = new ReflectionMethod($this, $method);
if (! $reflection->isPublic()) {
return false;
}

return true;
}

/**
* Magic getter.
*
* @param string $name
*
* @return mixed
*/
public function __get($name)
{
$method = Str::camel($name);

if ($this->isMutatorMethod($method)) {
return $this->$method();
}

return $this->data[$name] ?? null;
}

/**
* Static make method.
*
* @param array $data
*
* @return self
*/
public static function make(array $data)
{
return new static($data);
}

/**
* Create a decorated collection.
*
* @param array|Collection $data
*
* @return Collection
*/
public static function makeCollection($data): Collection
{
if (! is_array($data) && ! is_a($data, Collection::class)) {
throw new DecoratorException('Datatype must be array or Collection');
}

return Collection::make($data)->map(function ($item) {
return static::make($item);
});
}
}
2 changes: 1 addition & 1 deletion src/Decorator.php → src/ModelDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Str;

abstract class Decorator
abstract class ModelDecorator
{
/**
* The model which will be decorated.
Expand Down

0 comments on commit 5db4e38

Please sign in to comment.