New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add CakeNumber::fromReadableSize() and Validation::filesize() #850
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -101,6 +101,32 @@ public static function toReadableSize($size) { | |
} | ||
} | ||
|
||
/** | ||
* Converts filesize from human readable string to bytes | ||
* | ||
* @param string $size Size in human readable string like '5MB' | ||
* @param mixed $default Value to be returned when invalid size was used, for example 'Unknown type' | ||
* @return integer Bytes | ||
*/ | ||
public static function fromReadableSize($size, $default = false) { | ||
if (ctype_digit($size)) { | ||
return $size * 1; | ||
} | ||
$units = array('KB', 'MB', 'GB', 'TB', 'PB'); | ||
foreach ($units as $i => $unit) { | ||
if ($unit == substr($size, -2)) { | ||
return $size * pow(1024, $i + 1); | ||
} | ||
} | ||
if (substr($size, -1) == 'B' && ctype_digit(substr($size, 0, strlen($size) - 1))) { | ||
return $size * 1; | ||
} | ||
if ($default !== false) { | ||
return $default; | ||
} | ||
throw new CakeException(__d('cake_dev', 'No unit type.')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add a default return? throwing an exception sounds a bit too much for view related code There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Default return would be a function param |
||
} | ||
|
||
/** | ||
* Formats a number into a percentage string. | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't remove the unit from
$size
?Btw, to simplify a bit, you could get the last 2 chars outside, once size never changes. Also, you could override the
foreach
usingarray_search
+if
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jrbasso its merge din already like this, but this is what you had in mind?: 90c32ad
thx for the pointer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ceeram The array_search portion looks good. But the multiplication point still kind of weird to me. For example, if
$size
is10MB
, you will have'10MB' * pow(1024, 1 + 1)
. Seems it is working, but is kind of weird to do math with 'MB' in the variable.Btw, to avoid a issue in the future, what you think in use
strtoupper
in the unit? It will allow '10mb' as well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right, seems odd to calcaluate with MB in the strings, pow() converts all to numeric, but i think its clearer if i take it out.
Also will make it strtoupper