| 1 |
<?php |
| 2 |
|
| 3 |
namespace Depicter\Document\Models\Traits; |
| 4 |
|
| 5 |
trait HasDataSheetTrait { |
| 6 |
|
| 7 |
/** |
| 8 |
* Current dataSource record (dataSheet) |
| 9 |
* Only available if dataSource is assigned to the section |
| 10 |
* |
| 11 |
* @var array|null |
| 12 |
*/ |
| 13 |
protected $dataSheet = []; |
| 14 |
|
| 15 |
|
| 16 |
/** |
| 17 |
* Retrieves the dataSource record (dataSheet) |
| 18 |
* |
| 19 |
* @return array|null |
| 20 |
*/ |
| 21 |
public function getDataSheet() { |
| 22 |
return $this->dataSheet; |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Whether dataSheet is attached for this class or not |
| 27 |
* |
| 28 |
* @return bool |
| 29 |
*/ |
| 30 |
public function hasDataSheet() { |
| 31 |
return !empty( $this->dataSheet ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Sets dataSheet |
| 36 |
* |
| 37 |
* @param array $dataSheet |
| 38 |
*/ |
| 39 |
public function setDataSheet( $dataSheet ) { |
| 40 |
$this->dataSheet = $dataSheet; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Retrieves the current dataSheet ID if dataSource exits |
| 45 |
* |
| 46 |
* @return string |
| 47 |
*/ |
| 48 |
public function getDataSheetID() { |
| 49 |
if( $dataSheet = $this->getDataSheet() ){ |
| 50 |
if( !empty( $dataSheet['id'] ) ){ |
| 51 |
return $dataSheet['id']; |
| 52 |
} elseif( !empty( $dataSheet['uuid'] ) ){ |
| 53 |
return $dataSheet['uuid']; |
| 54 |
} |
| 55 |
} |
| 56 |
return $this->maybeReplaceDataSheetTags('{{{uuid}}}', '' ); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Retrieves data tag value if exists |
| 61 |
* |
| 62 |
* @param $tag |
| 63 |
* |
| 64 |
* @return mixed|string |
| 65 |
*/ |
| 66 |
public function getDataSheetTagValue( $tag ) { |
| 67 |
return $this->hasDataSheet() ? \Depicter::dataSource()->tagsManager()->convert( $tag, $this->getDataSheet() ) : ''; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Retrieves url of this dataSheet |
| 72 |
* |
| 73 |
* @return string |
| 74 |
*/ |
| 75 |
public function getDataSheetUrl() { |
| 76 |
if( $dataSheet = $this->getDataSheet() ){ |
| 77 |
if( !empty( $dataSheet['url'] ) ){ |
| 78 |
return $dataSheet['url']; |
| 79 |
} |
| 80 |
} |
| 81 |
return $this->maybeReplaceDataSheetTags('{{{url}}}', ''); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Whether section is linked to corresponding dataSheet or not |
| 86 |
* |
| 87 |
* @return false |
| 88 |
*/ |
| 89 |
public function isLinkedToDataSheet(){ |
| 90 |
return $this->maybeReplaceDataSheetTags('{{{linkSlides}}}', false); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Replace dataSheets tags if exits |
| 95 |
* |
| 96 |
* @param $variable |
| 97 |
* @param $default |
| 98 |
* |
| 99 |
* @return array|mixed|string|string[] |
| 100 |
*/ |
| 101 |
protected function maybeReplaceDataSheetTags( $variable, $default = null, $args = [] ){ |
| 102 |
if( $dataSheet = $this->getDataSheet() ){ |
| 103 |
$dataSheet = !empty( $args ) ? array_merge( $dataSheet, $args ) : $dataSheet; |
| 104 |
return \Depicter::dataSource()->tagsManager()->convert( $variable, $dataSheet ); |
| 105 |
} |
| 106 |
if( ! is_null( $default ) ){ |
| 107 |
return $default; |
| 108 |
} |
| 109 |
return $variable; |
| 110 |
} |
| 111 |
} |
| 112 |
|