Skip to content

Added else argument to ifPresent#31

Merged
petrknap merged 1 commit intomainfrom
if-present-else
Jan 22, 2025
Merged

Added else argument to ifPresent#31
petrknap merged 1 commit intomainfrom
if-present-else

Conversation

@petrknap
Copy link
Owner

@petrknap petrknap commented Jan 22, 2025

  • This pull request does not contain any breaking change.
  • I asked the AI to briefly summarize, use and improve the feature(s), and I'm okay with the output:

Could you briefly summarize the following code for me?

OptionalResource::ofFalsable(tmpfile())->ifPresent(function ($tmpFile): void {
    fwrite($tmpFile, 'data');
    fclose($tmpFile);
}, else: fn () => print('tmpfile() failed'));

Certainly! This PHP code snippet demonstrates the use of an OptionalResource to handle a potentially false value returned by tmpfile(). Here's a breakdown:

  1. OptionalResource::ofFalsable(tmpfile()) wraps the result of tmpfile() in an OptionalResource object.
  2. The ifPresent method takes two arguments: a function to execute if the tmpfile() was successful, and an alternative function if it failed.
  3. If the tmpfile() is successful, it writes the string 'data' to the temporary file and then closes it.
  4. If the tmpfile() fails, it prints 'tmpfile() failed'.

So, in short, this code snippet ensures that appropriate actions are taken based on whether tmpfile() succeeds or fails. Let me know if you need further details!

@petrknap petrknap self-assigned this Jan 22, 2025
@petrknap petrknap added the enhancement New feature or request label Jan 22, 2025
@petrknap petrknap merged commit 8c5c811 into main Jan 22, 2025
2 checks passed
@petrknap petrknap deleted the if-present-else branch January 22, 2025 21:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant