| 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 |
return $this->dataSheet['{{{uuid}}}'] ?? ''; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Retrieves data tag value if exists |
| 54 |
* |
| 55 |
* @param $tag |
| 56 |
* |
| 57 |
* @return mixed|string |
| 58 |
*/ |
| 59 |
public function getDataSheetTagValue( $tag ) { |
| 60 |
return $this->dataSheet[ $tag ] ?? ''; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Retrieves url of this dataSheet |
| 65 |
* |
| 66 |
* @return string |
| 67 |
*/ |
| 68 |
public function getDataSheetUrl() { |
| 69 |
return $this->dataSheet[ '{{{url}}}' ] ?? ''; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Whether section is linked to corresponding dataSheet or not |
| 74 |
* |
| 75 |
* @return false |
| 76 |
*/ |
| 77 |
public function isLinkedToDataSheet(){ |
| 78 |
return $this->dataSheet[ '{{{linkSlides}}}' ] ?? false; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Replace dataSheets tags if exits |
| 83 |
* |
| 84 |
* @param $variable |
| 85 |
* |
| 86 |
* @return array|mixed|string|string[] |
| 87 |
*/ |
| 88 |
protected function maybeReplaceDataSheetTags( $variable ){ |
| 89 |
if( $dataSheet = $this->getDataSheet() ){ |
| 90 |
return str_replace( array_keys( $dataSheet ), $dataSheet, $variable ); |
| 91 |
} |
| 92 |
return $variable; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Replace dataSheets tags if exits |
| 97 |
* |
| 98 |
* @param string $variable |
| 99 |
* @param string $tag |
| 100 |
* |
| 101 |
* @return string |
| 102 |
*/ |
| 103 |
protected function maybeReplaceDataSheetTag( $variable, $tag = '' ){ |
| 104 |
if( $dataSheet = $this->getDataSheet() ){ |
| 105 |
if( !empty( $dataSheet[ $tag ] ) ){ |
| 106 |
return str_replace( $tag, $dataSheet[ $tag ], $variable ); |
| 107 |
} |
| 108 |
} |
| 109 |
return $variable; |
| 110 |
} |
| 111 |
} |
| 112 |
|