| @@ -1,0 +1,90 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * @package VikBooking | |
| 4 | + * @subpackage core | |
| 5 | + * @author E4J s.r.l. | |
| 6 | + * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved. | |
| 7 | + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL | |
| 8 | + * @link https://vikwp.com | |
| 9 | + */ | |
| 10 | + | |
| 11 | +// No direct access | |
| 12 | +defined('ABSPATH') or die('No script kiddies please!'); | |
| 13 | + | |
| 14 | +/** | |
| 15 | + * Backup export rule abstraction. | |
| 16 | + * | |
| 17 | + * @since 1.5 | |
| 18 | + */ | |
| 19 | +abstract class VBOBackupExportRule implements JsonSerializable | |
| 20 | +{ | |
| 21 | + /** | |
| 22 | + * The instance used to manage the archive. | |
| 23 | + * | |
| 24 | + * @var VBOBackupExportArchive | |
| 25 | + */ | |
| 26 | + protected $archive; | |
| 27 | + | |
| 28 | + /** | |
| 29 | + * Class constructor. | |
| 30 | + * Children classes cannot overwrite this method. | |
| 31 | + * @see setup() | |
| 32 | + * | |
| 33 | + * @param VBOBackupExportArchive $archive The archive manager. | |
| 34 | + * @param mixed $data The rule setup data. | |
| 35 | + */ | |
| 36 | + final public function __construct(VBOBackupExportArchive $archive, $data = null) | |
| 37 | + { | |
| 38 | + // save a reference to the archive | |
| 39 | + $this->archive = $archive; | |
| 40 | + // set up the rule data | |
| 41 | + $this->setup($data); | |
| 42 | + } | |
| 43 | + | |
| 44 | + /** | |
| 45 | + * Returns the rule identifier. | |
| 46 | + * | |
| 47 | + * @return string | |
| 48 | + */ | |
| 49 | + public function getRule() | |
| 50 | + { | |
| 51 | + // remove the class prefix | |
| 52 | + $rule = preg_replace("/^VBOBackupExportRule/", '', get_class($this)); | |
| 53 | + // place an underscore between each camelCase | |
| 54 | + return strtolower(preg_replace("/([a-z])([A-Z])/", '$1_$2', $rule)); | |
| 55 | + } | |
| 56 | + | |
| 57 | + /** | |
| 58 | + * Returns the rules instructions. | |
| 59 | + * | |
| 60 | + * @return mixed | |
| 61 | + */ | |
| 62 | + abstract public function getData(); | |
| 63 | + | |
| 64 | + /** | |
| 65 | + * Configures the rule to work according to the specified data. | |
| 66 | + * | |
| 67 | + * @param mixed $data The rule setup data. | |
| 68 | + * | |
| 69 | + * @return void | |
| 70 | + */ | |
| 71 | + abstract protected function setup($data); | |
| 72 | + | |
| 73 | + /** | |
| 74 | + * Creates a standard object, containing all the supported properties, | |
| 75 | + * to be used when this class is passed to "json_encode()". | |
| 76 | + * | |
| 77 | + * @return object | |
| 78 | + * | |
| 79 | + * @see JsonSerializable | |
| 80 | + */ | |
| 81 | + public function jsonSerialize() | |
| 82 | + { | |
| 83 | + $rule = new stdClass; | |
| 84 | + $rule->role = $this->getRule(); | |
| 85 | + $rule->data = $this->getData(); | |
| 86 | + $rule->dateCreated = JFactory::getDate()->toSql(); | |
| 87 | + | |
| 88 | + return $rule; | |
| 89 | + } | |
| 90 | +} | |