Rename attributes to critiera (easier to understand)

This commit is contained in:
Jani Gyllenberg
2019-01-22 21:38:02 -05:00
parent be37f631ca
commit 462843a373

View File

@@ -29,7 +29,7 @@ class Updater
} }
/** /**
* Parse each record for attributes/values and update/create model * Parse each record for criteria/values and update/create model
* *
* @param string $file * @param string $file
* @return \Illuminate\Support\Collection * @return \Illuminate\Support\Collection
@@ -40,30 +40,30 @@ class Updater
$records = $this->getRecords($file); $records = $this->getRecords($file);
return $records->map(function($record) use ($model) { return $records->map(function($record) use ($model) {
$attributes = $this->getAttributes($record); $criteria = $this->getCriteria($record);
$values = $this->getValues($record); $values = $this->getValues($record);
return $model::updateOrCreate($attributes, $values); return $model::updateOrCreate($criteria, $values);
}); });
} }
/** /**
* Filter record attributes * Filter record criteria
* *
* @param object $record * @param object $record
* @return array * @return array
*/ */
protected function getAttributes(object $record) protected function getCriteria(object $record)
{ {
$attributes = collect($record)->filter(function($value, $key) { $criteria = collect($record)->filter(function($value, $key) {
return $this->isAttribute($key); return $this->isCriteria($key);
}); });
if($attributes->count() == 0) { if($criteria->count() == 0) {
throw new \Exception("No attributes (conditions for a match) detected"); throw new \Exception("No criteria/attributes detected");
} }
return $attributes->mapWithKeys(function($value, $key) { return $criteria->mapWithKeys(function($value, $key) {
return [substr($key, 1) => $value]; return [substr($key, 1) => $value];
})->toArray(); })->toArray();
} }
@@ -77,7 +77,7 @@ class Updater
protected function getValues($record) protected function getValues($record)
{ {
$values = collect($record)->reject(function($value, $key) { $values = collect($record)->reject(function($value, $key) {
if($this->isAttribute($key)) { if($this->isCriteria($key)) {
return true; return true;
} }
@@ -120,12 +120,12 @@ class Updater
} }
/** /**
* Check if column is an attribute * Check if column is criteria for a condition match
* *
* @param string $key * @param string $key
* @return boolean * @return boolean
*/ */
protected function isAttribute($key) protected function isCriteria($key)
{ {
return substr($key, 0, 1) == '_'; return substr($key, 0, 1) == '_';
} }