Skip to content

Commit

Permalink
Merge pull request #178 from VentureCraft/develop
Browse files Browse the repository at this point in the history
Datetime formatter
  • Loading branch information
duellsy committed Nov 9, 2015
2 parents 4109e65 + b9b4f98 commit 75af7ad
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 8 deletions.
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ In cases where you want to have control over the format of the output of the val
protected $revisionFormattedFields = array(
'title' => 'string:<strong>%s</strong>',
'public' => 'boolean:No|Yes',
'modified' => 'datetime:m/d/Y g:i A',
'deleted_at' => 'isEmpty:Active|Deleted'
);
```
Expand Down Expand Up @@ -224,6 +225,13 @@ Booleans by default will display as a 0 or a 1, which is pretty bland and won't
boolean:No|Yes
```

### DateTime
DateTime by default will display as Y-m-d H:i:s. Prefix the value with `datetime:` and then add your datetime format, e.g.,

```
datetime:m/d/Y g:i A
```

### Is Empty
This piggy backs off boolean, but instead of testing for a true or false value, it checks if the value is either null or an empty string.

Expand Down
15 changes: 15 additions & 0 deletions src/Venturecraft/Revisionable/FieldFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,19 @@ public static function string($value, $format = null)

return sprintf($format, $value);
}

/**
* Format the datetime
*
* @param string $value
* @param string $format
*
* @return formatted datetime
*/
public static function datetime($value, $format = 'Y-m-d H:i:s')
{
$datetime = new \DateTime($value);

return $datetime->format($format);
}
}
8 changes: 4 additions & 4 deletions src/Venturecraft/Revisionable/Revision.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ 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
* Returns the field that was updated, in the case that it's a foreign key
* denoted by a suffix of "_id", then "_id" is simply stripped
*
* @return string field
*/
Expand Down Expand Up @@ -170,7 +170,7 @@ 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
// Just a fail-safe, in the case the data setup isn't as expected
// Nothing to do here.
Log::info('Revisionable: ' . $e);
}
Expand Down Expand Up @@ -220,7 +220,7 @@ public function historyOf()
}

/*
* Egzamples:
* Examples:
array(
'public' => 'boolean:Yes|No',
'minimum' => 'string:Min: %s'
Expand Down
6 changes: 4 additions & 2 deletions src/Venturecraft/Revisionable/Revisionable.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ public function getRevisionFormattedFieldNames()

/**
* Identifiable Name
* When displaying revision history, when a foreigh key is updated
* When displaying revision history, when a foreign key is updated
* instead of displaying the ID, you can choose to display a string
* of your choice, just override this method in your model
* By default, it will fall back to the models ID.
Expand All @@ -337,10 +337,11 @@ public function identifiableName()

/**
* Revision Unknown String
* When displaying revision history, when a foreigh key is updated
* When displaying revision history, when a foreign key is updated
* instead of displaying the ID, you can choose to display a string
* of your choice, just override this method in your model
* By default, it will fall back to the models ID.
*
* @return string an identifying name for the model
*/
public function getRevisionNullString()
Expand All @@ -353,6 +354,7 @@ public function getRevisionNullString()
* When displaying revision history, if the revisions value
* cant be figured out, this is used instead.
* It can be overridden.
*
* @return string an identifying name for the model
*/
public function getRevisionUnknownString()
Expand Down
5 changes: 3 additions & 2 deletions src/Venturecraft/Revisionable/RevisionableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ public function getRevisionFormattedFieldNames()

/**
* Identifiable Name
* When displaying revision history, when a foreigh key is updated
* When displaying revision history, when a foreign key is updated
* instead of displaying the ID, you can choose to display a string
* of your choice, just override this method in your model
* By default, it will fall back to the models ID.
Expand All @@ -383,7 +383,7 @@ public function identifiableName()

/**
* Revision Unknown String
* When displaying revision history, when a foreigh key is updated
* When displaying revision history, when a foreign key is updated
* instead of displaying the ID, you can choose to display a string
* of your choice, just override this method in your model
* By default, it will fall back to the models ID.
Expand All @@ -400,6 +400,7 @@ public function getRevisionNullString()
* When displaying revision history, if the revisions value
* cant be figured out, this is used instead.
* It can be overridden.
*
* @return string an identifying name for the model
*/
public function getRevisionUnknownString()
Expand Down

0 comments on commit 75af7ad

Please sign in to comment.