Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+ Option to recover from unknown embedded document type
- Loading branch information
1 parent
cff413e
commit 7f9d4c5
Showing
5 changed files
with
148 additions
and
3 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: peter | ||
* Date: 22.10.18 | ||
* Time: 20:58 | ||
*/ | ||
|
||
namespace Maslosoft\Mangan\Events; | ||
|
||
use Maslosoft\Addendum\Interfaces\AnnotatedInterface; | ||
|
||
/** | ||
* Create event handler for this event | ||
* to try to recover from unknown document state. | ||
* | ||
* NOTE: Event *must* be handled for this to work. | ||
* | ||
* The handler can use `getData` to inspect what | ||
* was passed into `Transformator::toModel()` method, | ||
* adjust the data, or replace with completely new one and | ||
* pass it back to `setData`. | ||
* | ||
* Example use case: | ||
* | ||
* 1. Field was array of languages | ||
* 2. After application refactoring it was decided to change it to embedded array | ||
* 3. Old data exists containing only languages | ||
* | ||
* Solution: | ||
* | ||
* Event handler for UnknownDocumentType adds `_class` field and | ||
* set values properly. | ||
* | ||
* Class UnknownDocumentType | ||
* @package Maslosoft\Mangan\Events | ||
*/ | ||
class UnknownDocumentType extends ModelEvent | ||
{ | ||
const EventName = 'unknownDocumentType'; | ||
private $modelData = []; | ||
|
||
/** | ||
* Parent Model | ||
* @var AnnotatedInterface|null | ||
*/ | ||
public $parent = null; | ||
|
||
/** | ||
* Parent model field name | ||
* @var string | ||
*/ | ||
public $field = ''; | ||
|
||
/** | ||
* Get data from error. This will | ||
* be called also when trying to recover. | ||
* @return array | ||
*/ | ||
public function getData() | ||
{ | ||
return $this->modelData; | ||
} | ||
|
||
/** | ||
* @param array $data | ||
*/ | ||
public function setData($data) | ||
{ | ||
$this->modelData = $data; | ||
} | ||
|
||
|
||
} |
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: peter | ||
* Date: 22.10.18 | ||
* Time: 21:28 | ||
*/ | ||
|
||
namespace Maslosoft\Mangan\Helpers; | ||
|
||
use Maslosoft\Mangan\Events\Event; | ||
use Maslosoft\Mangan\Events\UnknownDocumentType; | ||
use Maslosoft\Mangan\Exceptions\TransformatorException; | ||
|
||
/** | ||
* This class will try to do something on unknown documents | ||
* or will panic. | ||
* | ||
* @package Maslosoft\Mangan\Helpers | ||
*/ | ||
class UnknownDocumentTypePanicker | ||
{ | ||
public static function tryHandle(&$data, $parent, $parentField) | ||
{ | ||
$className = ''; | ||
$handled = false; | ||
if(!empty($parent)) | ||
{ | ||
$event = new UnknownDocumentType(); | ||
$event->setData($data); | ||
$event->parent = $parent; | ||
$event->field = $parentField; | ||
|
||
$handled = Event::handled($parent, UnknownDocumentType::EventName, $event); | ||
if($handled) | ||
{ | ||
$data = $event->getData(); | ||
if(empty($data['_class'])) | ||
{ | ||
$handled = false; | ||
} | ||
else | ||
{ | ||
$className = $data['_class']; | ||
} | ||
} | ||
} | ||
if(!$handled) | ||
{ | ||
throw new TransformatorException('Could not determine document type'); | ||
} | ||
return $className; | ||
} | ||
} |
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