alexdodonov /
mezon-service
| 1 | <?php |
||
| 2 | namespace Mezon\Service; |
||
| 3 | |||
| 4 | /** |
||
| 5 | * Class DbServiceModel |
||
| 6 | * |
||
| 7 | * @package Service |
||
| 8 | * @subpackage DbServiceModel |
||
| 9 | * @author Dodonov A.A. |
||
| 10 | * @version v.1.0 (2019/10/18) |
||
| 11 | * @copyright Copyright (c) 2019, aeon.org |
||
| 12 | */ |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Default DB model for the service |
||
| 16 | * |
||
| 17 | * @author Dodonov A.A. |
||
| 18 | */ |
||
| 19 | class DbServiceModel extends \Mezon\Service\ServiceModel |
||
| 20 | { |
||
| 21 | |||
| 22 | use \Mezon\PdoCrud\ConnectionTrait; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Table name |
||
| 26 | */ |
||
| 27 | private $tableName = ''; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Fields algorithms |
||
| 31 | */ |
||
| 32 | private $fieldsSet = false; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Entity name |
||
| 36 | */ |
||
| 37 | private $entityName = false; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Constructor |
||
| 41 | * |
||
| 42 | * @param string|array $fields |
||
| 43 | * fields of the model |
||
| 44 | * @param string $tableName |
||
| 45 | * name of the table |
||
| 46 | * @param string $entityName |
||
| 47 | * name of the entity |
||
| 48 | */ |
||
| 49 | public function __construct($fields = '*', string $tableName = '', string $entityName = '') |
||
| 50 | { |
||
| 51 | $this->setTableName($tableName); |
||
| 52 | |||
| 53 | $this->entityName = $entityName; |
||
| 54 | |||
| 55 | if (is_string($fields)) { |
||
| 56 | $this->fieldsSet = new \Mezon\FieldsSet( |
||
| 57 | [ |
||
| 58 | '*' => [ |
||
| 59 | 'type' => 'string', |
||
| 60 | 'title' => 'All fields' |
||
| 61 | ] |
||
| 62 | ]); |
||
| 63 | } elseif (is_array($fields)) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 64 | $this->fieldsSet = new \Mezon\FieldsSet($fields); |
||
| 65 | } elseif ($fields instanceof \Mezon\FieldsSet) { |
||
| 66 | $this->fieldsSet = $fields; |
||
| 67 | } else { |
||
| 68 | throw (new \Exception('Invalid fields description', - 1)); |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Method sets table name |
||
| 74 | * |
||
| 75 | * @param string $tableName |
||
| 76 | * Table name |
||
| 77 | */ |
||
| 78 | protected function setTableName(string $tableName = ''): void |
||
| 79 | { |
||
| 80 | if (strpos($tableName, '-') !== false && strpos($tableName, '`') === false) { |
||
| 81 | $tableName = "`$tableName`"; |
||
| 82 | } |
||
| 83 | |||
| 84 | $this->tableName = $tableName; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Method returns table name |
||
| 89 | * |
||
| 90 | * @return string table name |
||
| 91 | */ |
||
| 92 | public function getTableName(): string |
||
| 93 | { |
||
| 94 | return $this->tableName; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Method returns list of all fields as string |
||
| 99 | * |
||
| 100 | * @return string list of all fields as string |
||
| 101 | */ |
||
| 102 | public function getFieldsNames(): string |
||
| 103 | { |
||
| 104 | return implode(', ', $this->fieldsSet->getFieldsNames()); |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Method returns true if the field exists |
||
| 109 | * |
||
| 110 | * @param string $fieldName |
||
| 111 | * Field name |
||
| 112 | * @return bool |
||
| 113 | */ |
||
| 114 | public function hasField(string $fieldName): bool |
||
| 115 | { |
||
| 116 | return $this->fieldsSet->hasField($fieldName); |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Method returns true if the custom field exists |
||
| 121 | * |
||
| 122 | * @return bool |
||
| 123 | */ |
||
| 124 | public function hasCustomFields(): bool |
||
| 125 | { |
||
| 126 | return $this->fieldsSet->hasCustomFields(); |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Method validates if the field $field exists |
||
| 131 | * |
||
| 132 | * @param string $field |
||
| 133 | * Field name |
||
| 134 | */ |
||
| 135 | public function validateFieldExistance(string $field) |
||
| 136 | { |
||
| 137 | return $this->fieldsSet->validateFieldExistance($field); |
||
|
0 ignored issues
–
show
Are you sure the usage of
$this->fieldsSet->validateFieldExistance($field) targeting Mezon\FieldsSet::validateFieldExistance() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. Loading history...
|
|||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Method returns fields list |
||
| 142 | * |
||
| 143 | * @return array Fields list |
||
| 144 | */ |
||
| 145 | public function getFields(): array |
||
| 146 | { |
||
| 147 | return $this->fieldsSet->getFieldsNames(); |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Method returns entity name |
||
| 152 | * |
||
| 153 | * @return string Entity name |
||
| 154 | */ |
||
| 155 | public function getEntityName(): string |
||
| 156 | { |
||
| 157 | return $this->entityName; |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Method returns field type |
||
| 162 | * |
||
| 163 | * @param string $fieldName |
||
| 164 | * field name |
||
| 165 | * @return string field type |
||
| 166 | */ |
||
| 167 | public function getFieldType(string $fieldName): string |
||
| 168 | { |
||
| 169 | return $this->fieldsSet->getFieldType($fieldName); |
||
| 170 | } |
||
| 171 | } |
||
| 172 |