PluginProbe
Depicter — Popup & Slider Builder / 1.5.2
Depicter — Popup & Slider Builder v1.5.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.5.2, at app/src/Document/Mapper.php

71 lines 1.3 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 = isset( $dataObject->document ) ? $dataObject->document : $dataObject;
44 $this->document = $mapper->map( $documentObject, new Document() );
45
46 if( $documentId ){
47 $this->document->setDocumentID( $documentId );
48 }
49
50 return $this;
51 }
52
53 /**
54 * Retrieves the mapped Document object
55 *
56 * @return Document
57 */
58 public function get()
59 {
60 return $this->document;
61 }
62
63 /**
64 * @return string
65 */
66 public function render()
67 {
68 return $this->get()->render();
69 }
70 }
71