| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- <?php
- namespace cx_newsletter;
- abstract class database_converter
- implements database_converter_interface {
- public function __construct() {
- $this->target = null;
- }
- protected function check_target() : void {
- if ($this->target === null) {
- throw $this->not_loaded_exception();
- }
- if (!$this->target->is_complete()) {
- throw $this->not_complete_exception();
- }
- }
- protected function not_complete_exception() : \exception {
- $exception = "\n\n";
- $exception .= 'Item in the builder is not complete.';
- $exception .= "\n\n";
- return new \exception($exception);
- }
- protected function check_all_exists(array $target, array $list) : void {
- foreach ($list as $name) {
- if (!array_key_exists($name, $target)) {
- throw $this->not_found_exception($name);
- }
- }
- }
- protected function id_or_null(array $target, string $name = 'id') : ?int {
- if (array_key_exists($name, $target)) {
- return intval($target[$name]);
- }
- return null;
- }
- protected function not_loaded_exception() : \exception {
- $exception = "\n\n";
- $exception .= 'Any content had not being loaded to this converter.';
- $exception .= "\n\n";
- return new \exception($exception);
- }
- protected function not_found_exception(string $name) : \exception {
- $exception = "\n\n";
- $exception .= 'Item with name "'.$name.'" not found.';
- $exception .= "\n\n";
- return new \exception($exception);
- }
- public function load_object(object $target) : self {
- $this->target = $target;
- return $this;
- }
- public function get_object() : object {
- return $this->target;
- }
- protected function is_loaded() : bool {
- return $this->target !== null;
- }
- protected function string_from_date(?\datetime $what) : ?string {
- if ($what === null) {
- return null;
- }
- return $what->format(\datetimeinterface::RFC3339);
- }
- protected function date_or_null(?string $what) : ?\datetime {
- if ($what === null or strtoupper($what) === 'NULL') {
- return null;
- }
-
- $time = \datetime::createfromformat(
- 'Y-m-d H:i:s',
- $what
- );
-
- if ($time === false) {
- return null;
- }
-
- return $time;
- }
- protected \exception $not_loaded_exception;
- protected ?object $target;
- }
|