PluginProbe
Depicter — Popup & Slider Builder / 1.6.2
Depicter — Popup & Slider Builder v1.6.2
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / app / src / Document / Mapper.php

Mapper.php in Depicter — Popup & Slider Builder 1.6.2, at app/src/Document/Mapper.php

81 lines 1.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Depicter\Document;
3
4
5 use Depicter\Document\Models\Common\Styles\Base as Style;
6 use Depicter\Document\Models\Document;
7 use TypeRocket\Utility\Data;
8
9 /**
10 * Maps JSON document data to document object
11 *
12 * @package Depicter\Document
13 */
14 class Mapper
15 {
16 /**
17 * @var Document
18 */
19 protected $document;
20
21 /**
22 * Hydrate the class with the provided $data.
23 *
24 * @param array|object $data
25 *
26 * @param int $documentId
27 *
28 * @return mixed Mapped object is returned.
29 * @throws \JsonMapper_Exception
30 */
31 public function hydrate( $data, $documentId = 0 )
32 {
33 // Maybe convert to object
34 $dataObject = Data::cast( $data, 'object');
35
36 // Hydrate the data to Document class
37 $mapper = new \JsonMapper();
38 $mapper->undefinedPropertyHandler = function( $class, $propName, $jsonValue ){
39 if( $class instanceof Style ){
40 $class->{$propName} = $jsonValue;
41 }
42 };
43 $documentObject = $dataObject->document ?? $dataObject;
44
45 $baseMapper = function ($class, $jsonValue) {
46 if ( is_array( $jsonValue ) && empty( $jsonValue ) ) {
47 return null;
48 } else {
49 return '\Depicter\Document\Models\Common\Styles\Base';
50 }
51 };
52
53 $mapper->classMap['\Depicter\Document\Models\Common\Styles\Base'] = $baseMapper;
54 $this->document = $mapper->map( $documentObject, new Document() );
55
56 if( $documentId ){
57 $this->document->setDocumentID( $documentId );
58 }
59
60 return $this;
61 }
62
63 /**
64 * Retrieves the mapped Document object
65 *
66 * @return Document
67 */
68 public function get()
69 {
70 return $this->document;
71 }
72
73 /**
74 * @return string
75 */
76 public function render()
77 {
78 return $this->get()->render();
79 }
80 }
81