Skip to content

Commit

Permalink
Merge pull request #151 from VentureCraft/develop
Browse files Browse the repository at this point in the history
PSR2 updates.
  • Loading branch information
duellsy committed Jun 29, 2015
2 parents 2c09f7c + 8a42901 commit 2626d8b
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 133 deletions.
1 change: 1 addition & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
preset: psr2
25 changes: 13 additions & 12 deletions src/Venturecraft/Revisionable/FieldFormatter.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
<?php namespace Venturecraft\Revisionable;
<?php

namespace Venturecraft\Revisionable;

/**
* FieldFormatter
* FieldFormatter.
*
* Allows formatting of fields
*
* (c) Venture Craft <http://www.venturecraft.com.au>
*/

/**
* Class FieldFormatter
* @package Venturecraft\Revisionable
*/
class FieldFormatter
{

/**
* Format the value according to the provided formats
* Format the value according to the provided formats.
*
* @param $key
* @param $value
* @param $formats
*
* @return string formated value
* @return string formatted value
*/
public static function format($key, $value, $formats)
{

foreach ($formats as $pkey => $format) {
$parts = explode(':', $format);
if (sizeof($parts) === 1) {
Expand All @@ -40,11 +44,10 @@ public static function format($key, $value, $formats)
}

return $value;

}

/**
* Check if a field is empty
* Check if a field is empty.
*
* @param $value
* @param array $options
Expand All @@ -59,7 +62,7 @@ public static function isEmpty($value, $options = array())
}

/**
* Boolean
* Boolean.
*
* @param $value
* @param array $options The false / true values to return
Expand All @@ -68,7 +71,6 @@ public static function isEmpty($value, $options = array())
*/
public static function boolean($value, $options = null)
{

if (!is_null($options)) {
$options = explode('|', $options);
}
Expand All @@ -81,7 +83,7 @@ public static function boolean($value, $options = null)
}

/**
* Format the string response, default is to just return the string
* Format the string response, default is to just return the string.
*
* @param $value
* @param $format
Expand All @@ -96,5 +98,4 @@ public static function string($value, $format = null)

return sprintf($format, $value);
}

}
92 changes: 55 additions & 37 deletions src/Venturecraft/Revisionable/Revision.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,43 @@
<?php namespace Venturecraft\Revisionable;
<?php

namespace Venturecraft\Revisionable;

use Illuminate\Support\Facades\Log;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Support\Facades\Log;

/**
* Revision
* Revision.
*
* Base model to allow for revision history on
* any model that extends this model
*
* (c) Venture Craft <http://www.venturecraft.com.au>
*/

class Revision extends Eloquent
{

/**
* @var string
*/
public $table = 'revisions';

/**
* @var array
*/
protected $revisionFormattedFields = array();

/**
* @param array $attributes
*/
public function __construct(array $attributes = array())
{
parent::__construct($attributes);
}

/**
* Revisionable
* Revisionable.
*
* Grab the revision history for the model that is calling
*
* @return array revision history
*/
public function revisionable()
Expand All @@ -36,8 +47,10 @@ public function revisionable()

/**
* Field Name
*
* Returns the field that was updated, in the case that it's a foreighn key
* denoted by a suffic of "_id", then "_id" is simply stripped
*
* @return string field
*/
public function fieldName()
Expand All @@ -52,9 +65,14 @@ public function fieldName()
}

/**
* Format field name
* Allow overrides for field names
**/
* Format field name.
*
* Allow overrides for field names.
*
* @param $key
*
* @return bool
*/
private function formatFieldName($key)
{
$related_model = $this->revisionable_type;
Expand All @@ -69,66 +87,67 @@ private function formatFieldName($key)
}

/**
* Old Value
* Old Value.
*
* Grab the old value of the field, if it was a foreign key
* attempt to get an identifying name for the model
* attempt to get an identifying name for the model.
*
* @return string old value
*/
public function oldValue()
{
return $this->getValue('old');

}


/**
* New Value
* New Value.
*
* Grab the new value of the field, if it was a foreign key
* attempt to get an identifying name for the model
* attempt to get an identifying name for the model.
*
* @return string old value
*/
public function newValue()
{
return $this->getValue('new');

}


/**
* Resposible for actually doing the grunt work for getting the
* old or new value for the revision
* Responsible for actually doing the grunt work for getting the
* old or new value for the revision.
*
* @param string $which old or new
*
* @return string value
*/
private function getValue($which = 'new')
{

$which_value = $which . '_value';

// First find the main model that was updated
$main_model = $this->revisionable_type;
// Load it, WITH the related model
if ( class_exists($main_model) ) {

if (class_exists($main_model)) {
$main_model = new $main_model;

try {
if (strpos($this->key, '_id')) {

$related_model = str_replace('_id', '', $this->key);

// Now we can find out the namespace of of related model
if (! method_exists($main_model, $related_model)) {
if (!method_exists($main_model, $related_model)) {
$related_model = camel_case($related_model); // for cases like published_status_id
if (! method_exists($main_model, $related_model)) {
if (!method_exists($main_model, $related_model)) {
throw new \Exception('Relation ' . $related_model . ' does not exist for ' . $main_model);
}
}
$related_class = $main_model->$related_model()->getRelated();

// Finally, now that we know the namespace of the related model
// we can load it, to find the information we so desire
$item = $related_class::find($this->$which_value);
$item = $related_class::find($this->$which_value);

if (is_null($this->$which_value) || $this->$which_value == '') {
$item = new $related_class;
Expand All @@ -150,7 +169,6 @@ private function getValue($which = 'new')

return $this->format($this->key, $item->identifiableName());
}

} catch (\Exception $e) {
// Just a failsafe, in the case the data setup isn't as expected
// Nothing to do here.
Expand All @@ -164,21 +182,21 @@ private function getValue($which = 'new')
if (method_exists($main_model, $mutator)) {
return $this->format($this->key, $main_model->$mutator($this->$which_value));
}

}

return $this->format($this->key, $this->$which_value);

}

/**
* User Responsible
* User Responsible.
*
* @return User user responsible for the change
*/
public function userResponsible()
{
if (class_exists($class = '\Cartalyst\Sentry\Facades\Laravel\Sentry')
|| class_exists($class = '\Cartalyst\Sentinel\Laravel\Facades\Sentinel')) {
|| class_exists($class = '\Cartalyst\Sentinel\Laravel\Facades\Sentinel')
) {
return $class::findUserById($this->user_id);
} else {
$user_model = app('config')->get('auth.model');
Expand All @@ -187,17 +205,17 @@ public function userResponsible()
}
}


/**
* Returns the object we have the history of
* @return Object or false
*
* @return Object|false
*/
public function historyOf()
{
if(class_exists($class = $this->revisionable_type))
{
if (class_exists($class = $this->revisionable_type)) {
return $class::find($this->revisionable_id);
}

return false;
}

Expand All @@ -209,17 +227,17 @@ public function historyOf()
)
*/
/**
* Format the value according to the $revisionFormattedFields array
* Format the value according to the $revisionFormattedFields array.
*
* @param $key
* @param $value
*
* @return string formated value
* @return string formatted value
*/
public function format($key, $value)
{
$related_model = $this->revisionable_type;
$related_model = new $related_model;
$related_model = $this->revisionable_type;
$related_model = new $related_model;
$revisionFormattedFields = $related_model->getRevisionFormattedFields();

if (isset($revisionFormattedFields[$key])) {
Expand Down
Loading

0 comments on commit 2626d8b

Please sign in to comment.