1<?php 2/* 3 * vim:set softtabstop=4 shiftwidth=4 expandtab: 4 * 5 * LICENSE: GNU Affero General Public License, version 3 (AGPL-3.0-or-later) 6 * Copyright 2001 - 2020 Ampache.org 7 * 8 * This program is free software: you can redistribute it and/or modify 9 * it under the terms of the GNU Affero General Public License as published by 10 * the Free Software Foundation, either version 3 of the License, or 11 * (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU Affero General Public License for more details. 17 * 18 * You should have received a copy of the GNU Affero General Public License 19 * along with this program. If not, see <https://www.gnu.org/licenses/>. 20 */ 21 22namespace Ampache\Repository\Model; 23 24use Ampache\Module\Util\ObjectTypeToClassNameMapper; 25use Ampache\Repository\Repository; 26 27/** 28 * Description of Model 29 * 30 * @author raziel 31 */ 32abstract class DatabaseObject 33{ 34 protected $id; 35 //private $originalData; 36 37 /** 38 * Stores relation between SQL field name and class name so we can initialize objects the right way 39 * @var array 40 */ 41 protected $fieldClassRelations = array(); 42 43 public function __construct() 44 { 45 $this->remapCamelcase(); 46 $this->initializeChildObjects(); 47 //$this->originalData = get_object_vars($this); 48 } 49 50 public function getId() 51 { 52 return $this->id; 53 } 54 55 //protected function isPropertyDirty($property) 56 //{ 57 // return $this->originalData->$property !== $this->$property; 58 //} 59 60 /** 61 * @return boolean 62 */ 63 public function isDirty() 64 { 65 return true; 66 } 67 68 /** 69 * Get all changed properties 70 * TODO: we get all properties for now...need more logic here... 71 * @return array 72 */ 73 public function getDirtyProperties() 74 { 75 $properties = get_object_vars($this); 76 unset($properties['id']); 77 unset($properties['fieldClassRelations']); 78 79 return $this->fromCamelCase($properties); 80 } 81 82 /** 83 * Convert the object properties to camelCase. 84 * This works in constructor because the properties are here from 85 * fetch_object before the constructor get called. 86 */ 87 protected function remapCamelcase() 88 { 89 foreach (get_object_vars($this) as $key => $val) { 90 if (strpos($key, '_') !== false) { 91 $camelCaseKey = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $key)))); 92 $this->$camelCaseKey = $val; 93 unset($this->$key); 94 } 95 } 96 } 97 98 /** 99 * @param $properties 100 * @return array 101 */ 102 protected function fromCamelCase($properties) 103 { 104 $data = array(); 105 foreach ($properties as $propertie => $value) { 106 $newPropertyKey = strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $propertie)); 107 $data[$newPropertyKey] = $value; 108 } 109 110 return $data; 111 } 112 113 /** 114 * Adds child Objects based of the Model Information 115 * TODO: Someday we might need lazy loading, but for now it should be ok. 116 */ 117 public function initializeChildObjects() 118 { 119 foreach ($this->fieldClassRelations as $field => $repositoryName) { 120 if (class_exists($repositoryName)) { 121 /* @var Repository $repository */ 122 $class_name = ObjectTypeToClassNameMapper::map($repositoryName); 123 $repository = new $class_name(); 124 $this->$field = $repository->findById($this->$field); 125 } 126 } 127 } 128} 129