Skip to content

Commit

Permalink
Added documentation for data mappers and clear session
Browse files Browse the repository at this point in the history
  • Loading branch information
hollodotme committed Oct 6, 2016
1 parent 71e70d2 commit ac54cb5
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,59 @@ if ( $session->isSomeStringValueSet() )
$session->unsetSomeStringValue();
```

### Data mapping

```php
<?php declare(strict_types=1);

namespace MyVendor\MyProject;

use IceHawk\Session\Interfaces\MapsSessionData;

# Create a data mapper class
final class MyDataMapper implements MapsSessionData
{
public function toSessionData( $value )
{
return base64_encode( $value );
}

public function fromSessionData( $sessionData )
{
return base64_decode( $sessionData );
}
}

$session = new Session( $_SESSION );

# Add the data mapper for all keys in the registry
$session->addDataMapper( new MyDataMapper() );

# Add the data mapper for one specific key in the registry
$session->addDataMapper( new MyDataMapper(), [Session::KEY_SOME_STRING_VALUE] );

# Add the data mapper for multiple keys in the registry
$session->addDataMapper( new MyDataMapper(), [Session::KEY_SOME_STRING_VALUE, Session::KEY_SOME_OTHER_VALUE] );
```

- The data mapper's `toSessionData()` is called when the `AbstractSesion::set()` method gets invoked.
- The data mapper's `fromSessionData()` is called when the `AbstractSesion::get()` method gets invoked.

### Clear all session data

```php
<?php declare(strict_types=1);

namespace MyVendor\MyProject;

$session = new Session( $_SESSION );

# ... put some data to the session ...

# Clear the session data
$session->clear();
```

## Contributing

Contributions are welcome! Please see our [Contribution guide](./CONTRIBUTING.md).

0 comments on commit ac54cb5

Please sign in to comment.