Skip to content

Commit

Permalink
Adding global data mappers does not reserve the '*' key anymore, rais…
Browse files Browse the repository at this point in the history
…ed composer dev-dependency of tm/tooly-composer-script to ^1.0, Fixed author tags, Added changelog
  • Loading branch information
hollodotme committed Jul 6, 2016
1 parent e67af87 commit ffd3620
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 42 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ vagrant

# composer
vendor/
composer.lock

# locally generated docs
build/api/
Expand Down
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Change Log
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/) and [Keep a CHANGELOG](http://keepachangelog.com).

## [1.0.1] - 2016-07-06

### Added

- Changelog

### Changed

- Adding global data mappers does not reserve the '*' key anymore
- raised composer dev-dependency of `tm/tooly-composer-script` to `^1.0`
- Fixed author tags

## 1.0.0 - 2016-07-05

- First release

[1.0.1]: https://github.com/icehawk/session/compare/v1.0.0...v1.0.1
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"php": ">=7.0"
},
"require-dev": {
"tm/tooly-composer-script": "^0.1.1"
"tm/tooly-composer-script": "^1.0"
},
"autoload": {
"psr-4": {
Expand Down
82 changes: 82 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 53 additions & 35 deletions src/AbstractSession.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* @author h.woltersdorf
* @author hollodotme
*/

namespace IceHawk\Session;
Expand All @@ -17,35 +17,54 @@ abstract class AbstractSession
private $sessionData;

/** @var array|MapsSessionData[] */
private $dataMappers;
private $keyDataMappers;

/** @var array|MapsSessionData[] */
private $globalDataMappers;

public function __construct( array &$sessionData )
{
$this->sessionData = &$sessionData;
$this->dataMappers = [ ];
$this->sessionData = &$sessionData;
$this->keyDataMappers = [ ];
$this->globalDataMappers = [ ];
}

final public function addDataMapper( MapsSessionData $dataMapper, array $keys = [ ] )
{
if ( empty($keys) )
{
$keys = [ '*' ];
$this->addGlobalDataMapper( $dataMapper );
}

foreach ( array_unique( $keys ) as $key )
else
{
if ( isset($this->dataMappers[ $key ]) )
foreach ( array_unique( $keys ) as $key )
{
if ( !in_array( $dataMapper, $this->dataMappers[ $key ] ) )
{
$this->dataMappers[ $key ][] = $dataMapper;
}
$this->addKeyDataMapper( $dataMapper, $key );
}
else
}
}

private function addGlobalDataMapper( MapsSessionData $dataMapper )
{
if ( !in_array( $dataMapper, $this->globalDataMappers ) )
{
$this->globalDataMappers[] = $dataMapper;
}
}

private function addKeyDataMapper( MapsSessionData $dataMapper, string $key )
{
if ( isset($this->keyDataMappers[ $key ]) )
{
if ( !in_array( $dataMapper, $this->keyDataMappers[ $key ] ) )
{
$this->dataMappers[ $key ] = [ $dataMapper ];
$this->keyDataMappers[ $key ][] = $dataMapper;
}
}
else
{
$this->keyDataMappers[ $key ] = [ $dataMapper ];
}
}

final protected function set( string $key, $value )
Expand All @@ -55,27 +74,19 @@ final protected function set( string $key, $value )

private function mapValueToSessionData( string $key, $value )
{
$mappers = $this->getDataMappersForKey( $key );
$keyDataMappers = $this->keyDataMappers[ $key ] ?? [ ];

foreach ( $mappers as $mapper )
foreach ( $keyDataMappers as $keyDataMapper )
{
$value = $mapper->toSessionData( $value );
$value = $keyDataMapper->toSessionData( $value );
}

return $value;
}

/**
* @param string $key
*
* @return array|MapsSessionData[]
*/
private function getDataMappersForKey( string $key ) : array
{
$mappers = $this->dataMappers[ $key ] ?? [ ];
$mappers = array_merge( $mappers, $this->dataMappers['*'] ?? [ ] );
foreach ( $this->globalDataMappers as $globalDataMapper )
{
$value = $globalDataMapper->toSessionData( $value );
}

return $mappers;
return $value;
}

final protected function get( string $key )
Expand All @@ -87,13 +98,20 @@ private function mapValueFromSessionData( string $key )
{
if ( $this->isset( $key ) )
{
$value = $this->sessionData[ $key ];
$mappers = $this->getDataMappersForKey( $key );
$value = $this->sessionData[ $key ];
$globalDataMappers = array_reverse( $this->globalDataMappers );
$keyDataMappers = array_reverse( $this->keyDataMappers[ $key ] ?? [ ] );

/** @var MapsSessionData $globalDataMapper */
foreach ( $globalDataMappers as $globalDataMapper )
{
$value = $globalDataMapper->fromSessionData( $value );
}

/** @var MapsSessionData $mapper */
foreach ( array_reverse( $mappers ) as $mapper )
/** @var MapsSessionData $keyDataMapper */
foreach ( $keyDataMappers as $keyDataMapper )
{
$value = $mapper->fromSessionData( $value );
$value = $keyDataMapper->fromSessionData( $value );
}

return $value;
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/SessionException.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* @author h.woltersdorf
* @author hollodotme
*/

namespace IceHawk\Session\Exceptions;
Expand Down
2 changes: 1 addition & 1 deletion src/Interfaces/MapsSessionData.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* @author h.woltersdorf
* @author hollodotme
*/

namespace IceHawk\Session\Interfaces;
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/AbstractSessionTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* @author h.woltersdorf
* @author hollodotme
*/

namespace IceHawk\Session\Tests\Unit;
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Fixtures/DataMapper.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* @author h.woltersdorf
* @author hollodotme
*/

namespace IceHawk\Session\Tests\Unit\Fixtures;
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Fixtures/Session.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* @author h.woltersdorf
* @author hollodotme
*/

namespace IceHawk\Session\Tests\Unit\Fixtures;
Expand Down

0 comments on commit ffd3620

Please sign in to comment.