Skip to content

Commit

Permalink
Introduced trait with custom version compare function, that can handl…
Browse files Browse the repository at this point in the history
…e the Lumen version string.
  • Loading branch information
westphalen committed Jan 21, 2018
1 parent c2941a7 commit 3e86538
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Prettus/Repository/Eloquent/BaseRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Prettus\Repository\Events\RepositoryEntityDeleted;
use Prettus\Repository\Events\RepositoryEntityUpdated;
use Prettus\Repository\Exceptions\RepositoryException;
use Prettus\Repository\Traits\ComparesVersionsTrait;
use Prettus\Validator\Contracts\ValidatorInterface;
use Prettus\Validator\Exceptions\ValidatorException;

Expand All @@ -26,6 +27,7 @@
*/
abstract class BaseRepository implements RepositoryInterface, RepositoryCriteriaInterface
{
use ComparesVersionsTrait;

/**
* @var Application
Expand Down Expand Up @@ -555,7 +557,7 @@ public function create(array $attributes)
// we should pass data that has been casts by the model
// to make sure data type are same because validator may need to use
// this data to compare with data that fetch from database.
if( version_compare($this->app->version(), "5.2.*", ">") ){
if( $this->versionCompare($this->app->version(), "5.2.*", ">") ){
$attributes = $this->model->newInstance()->forceFill($attributes)->makeVisible($this->model->getHidden())->toArray();
}else{
$model = $this->model->newInstance()->forceFill($attributes);
Expand Down Expand Up @@ -593,7 +595,7 @@ public function update(array $attributes, $id)
// we should pass data that has been casts by the model
// to make sure data type are same because validator may need to use
// this data to compare with data that fetch from database.
if( version_compare($this->app->version(), "5.2.*", ">") ){
if( $this->versionCompare($this->app->version(), "5.2.*", ">") ){
$attributes = $this->model->newInstance()->forceFill($attributes)->makeVisible($this->model->getHidden())->toArray();
}else{
$model = $this->model->newInstance()->forceFill($attributes);
Expand Down
32 changes: 32 additions & 0 deletions src/Prettus/Repository/Traits/ComparesVersionsTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Created by PhpStorm.
* User: sune
* Date: 21/01/2018
* Time: 16.11
*/

namespace Prettus\Repository\Traits;

trait ComparesVersionsTrait
{
/**
* Version compare function that can compare both Laravel and Lumen versions.
*
* @param string $frameworkVersion
* @param string $compareVersion
* @param string|null $operator
* @return mixed
*/
public function versionCompare($frameworkVersion, $compareVersion, $operator = null)
{
// Lumen (5.5.2) (Laravel Components 5.5.*)
$lumenPattern = '/Lumen \((\d\.\d\.[\d|\*])\)( \(Laravel Components (\d\.\d\.[\d|\*])\))?/';

if (preg_match($lumenPattern, $frameworkVersion, $matches)) {
$frameworkVersion = isset($matches[3]) ? $matches[3] : $matches[1]; // Prefer Laravel Components version.
}

return version_compare($frameworkVersion, $compareVersion, $operator);
}
}

0 comments on commit 3e86538

Please sign in to comment.