Skip to content
Open

0.2 #104

Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
c766ab7
Change scope of entity property to protected
rmrhz Dec 1, 2016
e79d904
Fixes #93
rmrhz Dec 12, 2016
6781a74
Merge branch 'master' of github.com:prjkt/repofuck
rmrhz Dec 12, 2016
ec32781
Merge from master
rmrhz Jan 17, 2017
3bb4946
~ #90 Remove deprecated function 'setDataAndKeys'
rmrhz Jan 17, 2017
30aaf89
~ #90 Rename 'setData' & 'setKeys' to 'data' & 'keys'
rmrhz Jan 17, 2017
9114bfd
Rename 'data' to 'fill', rewrite functionality
rmrhz Jan 17, 2017
ba63166
~ #90 Renamed 'getColumns' to 'columns'
rmrhz Jan 17, 2017
70dd201
~ #90 Remove deprecated funciton 'getKeys'
rmrhz Jan 17, 2017
ab2810d
~ #90 Remove 'setColumns' function
rmrhz Jan 17, 2017
21c206a
~ #90 Altered return type to self
rmrhz Jan 17, 2017
b352115
Added updateOrCreate function
makoru-hikage Jan 24, 2017
75a6199
UpdateOrCreate update...
makoru-hikage Feb 6, 2017
5621569
Update Repofuck.php
makoru-hikage Feb 6, 2017
e2b879b
Update Repofuck.php
makoru-hikage Feb 6, 2017
8af3104
Changed `case null` to `default`
makoru-hikage Feb 7, 2017
c14656c
Update Repofuck.php
makoru-hikage Feb 11, 2017
af2f075
Must set columns through the `columns()` method
makoru-hikage Feb 16, 2017
a8ec97a
Merge pull request #108 from makoru-hikage/master
rmrhz Feb 24, 2017
1db69a4
Merge branch 'master' into 0.2
rmrhz Mar 8, 2017
592b8d0
Merge remote-tracking branch 'upstream/master'
makoru-hikage Mar 29, 2017
c3f1aa6
Remove unnecessary function call
makoru-hikage Mar 29, 2017
2387069
Supplied the missing argument
makoru-hikage Mar 29, 2017
38efadb
Merge branch '0.2' into master
rmrhz Apr 1, 2017
ec0da5d
Merge pull request #120 from makoru-hikage/master
rmrhz Apr 1, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 78 additions & 117 deletions src/Prjkt/Component/Repofuck/Repofuck.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ abstract class Repofuck
*
* @var \Illuminate\Database\Eloquent\Model
*/
public $entity;
protected $entity;

/**
* Entities container
Expand Down Expand Up @@ -165,11 +165,13 @@ public function register($instance) : bool
* Sets the entity to be chained
*
* @param string $name
* @param Closure $closure
* @throws Prjkt\Component\Repofuck\Exceptions\InstanceNotEntityException
* @return self
*/
public function entity(string $name = null, Closure $closure = null) : self
{
$this->entity = $this->entities->resolve($name);
$entity = $this->entity = $this->entities->resolve($name);

if ( $closure instanceof Closure ) {

Expand Down Expand Up @@ -197,44 +199,18 @@ public function resetEntity(string $name = null) :self
}

/**
* [Deprecated] Set the data and keys for the repository
* Set the fill data and keys for the repository
*
* @param array $parameters
* @return self
*/
public function setDataAndKeys(array $parameters) : self
{
return $this->data($parameters, true);
}

/**
* Set the data and keys for the repository
*
* @param array $parameters
* @param bool $keys [def=false] Set the keys
* @return self
*/
public function data(array $parameters, bool $keys = false) : self
public function fill(array $parameters) : self
{
if ( $keys ) {
$keys = array_keys($parameters);
$this->setKeys($keys);
$this->keys(array_keys($keys));
}

$this->setData($parameters);

return $this;
}

/**
* Set the columns to be queried
*
* @param array $columns
* @return self
*/
public function setColumns(array $columns) : self
{
$this->columns = $columns;
$this->data($parameters);

return $this;
}
Expand All @@ -245,7 +221,7 @@ public function setColumns(array $columns) : self
* @param array
* @return self
*/
public function setData(array $data) : self
public function data(array $data = []) : self
{
$this->data = $data;

Expand All @@ -258,41 +234,23 @@ public function setData(array $data) : self
* @param array
* @return self
*/
public function setKeys(array $keys) : self
public function keys(array $keys = []) : self
{
$this->keys = $keys;

return $this;
}

/**
* Get the columns
*
* @return array
*/
public function getColumns() : array
{
return $this->columns;
}

/**
* Get the data in the repository
* Set the columns for the repository
*
* @return array
* @return $this
*/
public function getData() : array
public function columns(array $columns = []) : self
{
return $this->data;
}
$this->columns = $columns;

/**
* Get the keys in the repository
*
* @return array
*/
public function getKeys() : array
{
return $this->keys;
return $this;
}

/**
Expand Down Expand Up @@ -325,84 +283,31 @@ public function first($params, $value = null)

case ( is_array($params) ):

$params = ! $this->hasValues($params) ? $this->getData() : $params;
$params = ! $this->hasValues($params) ? $this->data : $params;

$entity = $this->entity->where($params)->first($this->getColumns());
$entity = $this->entity->where($params)->first($this->columns);

break;

case ( is_string($params) && ! is_null($value) ):

$entity = $this->entity->where($params, $value)->first();
$entity = $this->entity->where($params, $value)->first($this->columns);

break;
}

return $entity;
}

/**
* Prepares the persistence of a repository or entity
*
* @param \Closure $function
* @return self
*/
public function prepare(Closure $function) : self
{
$return = call_user_func_array($function, [($this)->resetEntity()]);

switch($return)
{
case null:

return $this;

break;

case ( $return instanceof Builder or $return instanceof Model ):

// This will persist the entity throughout the repository for the next operation
$this->entity = $return;

break;

case ( $return instanceof self && $return instanceof $this ):

// Returns a modified persistence of itself where operations are contained
return $return;

break;

case ( $return instanceof self && ! $return instanceof $this ):

// This will persist the repository for the next operation
// It also gives an advantage as the repository contained
$this->repositories->set($return);

return $this->repositories->resolve();

break;

case ( is_array($return) ):

// This will persist the keys and data returned
$this->setDataAndKeys($return);

break;
}

// If there's a repository being persisted, return it, defer to self when there's none
return $this;
}

/**
* Gets an entity by parameters
*
* @param array $columns
* @return \Illuminate\Database\Eloquent\Collection
*/
public function get() : Collection
{
return $this->entity->get();
return $this->entity->get($this->columns);
}

/**
Expand All @@ -426,7 +331,8 @@ public function create() : Model
{
$this->entity = new $this->entity;

$entity = $this->map($this->getData());
$entity = $this->map($this->data);

$entity->save();

return $entity;
Expand All @@ -441,8 +347,63 @@ public function create() : Model
public function update($identifier) : Model
{
$entity = $identifier instanceof Model ? $identifier : $this->first($identifier);
$entity = $this->map($this->getData(), $entity);
$entity = $this->map($this->data, $entity);

$entity->save();

return $entity;
}

/**
* Finds an entity and updates it. It's created when it's non-existent
*
* @param mixed int|string|array
* @return \Illuminate\Database\Eloquent\Model $entity
*/
public function updateOrCreate($identifier) : Model
{
switch ($identifier)
{
case ( is_numeric($identifer) ):

$entity = $this->entity->findOrNew($params, $this->columns);

break;

case ( is_array($identifer) ):

$entity = $this->entity->firstOrNew($params);

break;

default:

$entity = new $this->entity;

break;
}

$entity = $this->map($this->data, $entity);
$entity->save();

return $entity;
}

/**
* Finds an entity and updates it. It's created when it's non-existent
*
* @param mixed int|string|array|\Illuminate\Database\Eloquent\Model $entity
* @return \Illuminate\Database\Eloquent\Model $entity
*/
public function updateOrCreate($identifier) : Model
{
$entity = $identifier;

if (! $entity instanceof Model) {
$entity = $this->first($identifier) ?? new $this->entity;
}

$entity = $this->map($this->data, $entity);
$entity->save();

return $entity;
Expand Down