@@ -56,9 +56,9 @@ discard block |
||
| 56 | 56 | */ |
| 57 | 57 | private function extractTypes() { |
| 58 | 58 | $baseClass = new \ReflectionClass(NOSQLBase::class); |
| 59 | - if(null !== $baseClass) { |
|
| 59 | + if (null !== $baseClass) { |
|
| 60 | 60 | $types = []; |
| 61 | - foreach($baseClass->getConstants() as $constant) { |
|
| 61 | + foreach ($baseClass->getConstants() as $constant) { |
|
| 62 | 62 | $types[] = $constant; |
| 63 | 63 | } |
| 64 | 64 | $this->setTypes($types); |
@@ -80,10 +80,10 @@ discard block |
||
| 80 | 80 | public function getDomains() { |
| 81 | 81 | $domains = []; |
| 82 | 82 | $storedDomains = $this->cache->getDataFromFile(CONFIG_DIR . DIRECTORY_SEPARATOR . 'domains.json', Cache::JSON, TRUE); |
| 83 | - if(!empty($storedDomains)) { |
|
| 84 | - foreach($storedDomains as $domain => $data) { |
|
| 83 | + if (!empty($storedDomains)) { |
|
| 84 | + foreach ($storedDomains as $domain => $data) { |
|
| 85 | 85 | $domainLabel = str_replace(['@', '/'], '', $domain); |
| 86 | - if('ROOT' !== $domainLabel) { |
|
| 86 | + if ('ROOT' !== $domainLabel) { |
|
| 87 | 87 | $domains[] = $domainLabel; |
| 88 | 88 | } |
| 89 | 89 | } |
@@ -98,7 +98,7 @@ discard block |
||
| 98 | 98 | public function getCollections($module) { |
| 99 | 99 | $collections = []; |
| 100 | 100 | $schemaFilename = CORE_DIR . DIRECTORY_SEPARATOR . $module . DIRECTORY_SEPARATOR . 'Config' . DIRECTORY_SEPARATOR . 'schema.json'; |
| 101 | - if(file_exists($schemaFilename)) { |
|
| 101 | + if (file_exists($schemaFilename)) { |
|
| 102 | 102 | $collections = $this->cache->getDataFromFile($schemaFilename, Cache::JSON, TRUE); |
| 103 | 103 | } |
| 104 | 104 | return $collections; |
@@ -121,10 +121,10 @@ discard block |
||
| 121 | 121 | '@NOSQL/generator/api.base.php.twig' => CORE_DIR . DIRECTORY_SEPARATOR . $module . DIRECTORY_SEPARATOR . 'Api' . DIRECTORY_SEPARATOR . 'Base', |
| 122 | 122 | '@NOSQL/generator/dto.php.twig' => CORE_DIR . DIRECTORY_SEPARATOR . $module . DIRECTORY_SEPARATOR . 'Dto' . DIRECTORY_SEPARATOR . 'Models', |
| 123 | 123 | ]; |
| 124 | - foreach($collections as $raw) { |
|
| 124 | + foreach ($collections as $raw) { |
|
| 125 | 125 | $collection = new CollectionDto(false); |
| 126 | 126 | $collection->fromArray($raw); |
| 127 | - foreach($files as $template => $path) { |
|
| 127 | + foreach ($files as $template => $path) { |
|
| 128 | 128 | GeneratorHelper::createDir($path); |
| 129 | 129 | $templateDump = $tpl->dump($template, [ |
| 130 | 130 | 'domain' => $module, |
@@ -132,7 +132,7 @@ discard block |
||
| 132 | 132 | 'properties' => $collection->properties, |
| 133 | 133 | ]); |
| 134 | 134 | $force = false; |
| 135 | - if(false !== strpos($template, 'dto') || false !== strpos(strtolower($template), 'base')) { |
|
| 135 | + if (false !== strpos($template, 'dto') || false !== strpos(strtolower($template), 'base')) { |
|
| 136 | 136 | $force = true; |
| 137 | 137 | } |
| 138 | 138 | $this->writeTemplateToFile($templateDump, $path . DIRECTORY_SEPARATOR . $collection->name . '.php', $force); |
@@ -171,7 +171,7 @@ discard block |
||
| 171 | 171 | $db = ParserService::getInstance()->createConnection($module); |
| 172 | 172 | $collections = $this->getCollections($module); |
| 173 | 173 | $success = true; |
| 174 | - foreach($collections as $raw) { |
|
| 174 | + foreach ($collections as $raw) { |
|
| 175 | 175 | $jsonSchema = $this->parseCollection($raw); |
| 176 | 176 | try { |
| 177 | 177 | /** @var BSONDocument $result */ |
@@ -182,8 +182,8 @@ discard block |
||
| 182 | 182 | ]); |
| 183 | 183 | $response = $result->getArrayCopy(); |
| 184 | 184 | $success = array_key_exists('ok', $response) && $response['ok'] > 0; |
| 185 | - } catch(\Exception $exception) { |
|
| 186 | - if($exception->getCode() !== 48) { |
|
| 185 | + } catch (\Exception $exception) { |
|
| 186 | + if ($exception->getCode() !== 48) { |
|
| 187 | 187 | $success = false; |
| 188 | 188 | } |
| 189 | 189 | } |
@@ -214,10 +214,10 @@ discard block |
||
| 214 | 214 | $property = new StringPropertyDto(false); |
| 215 | 215 | break; |
| 216 | 216 | } |
| 217 | - if(array_key_exists('type', $rawProperty)) { |
|
| 217 | + if (array_key_exists('type', $rawProperty)) { |
|
| 218 | 218 | $property->bsonType = $rawProperty['type']; |
| 219 | 219 | } |
| 220 | - if(array_key_exists('description', $rawProperty)) { |
|
| 220 | + if (array_key_exists('description', $rawProperty)) { |
|
| 221 | 221 | $property->description = $rawProperty['description']; |
| 222 | 222 | } |
| 223 | 223 | if (array_key_exists('required', $rawProperty) && $rawProperty['required']) { |
@@ -235,7 +235,7 @@ discard block |
||
| 235 | 235 | public function getValidations() { |
| 236 | 236 | $fieldTypes = new \ReflectionClass(Field::class); |
| 237 | 237 | $validations = []; |
| 238 | - foreach($fieldTypes->getConstants() as $validation) { |
|
| 238 | + foreach ($fieldTypes->getConstants() as $validation) { |
|
| 239 | 239 | $validations[] = $validation; |
| 240 | 240 | } |
| 241 | 241 | return $validations; |
@@ -33,7 +33,7 @@ discard block |
||
| 33 | 33 | public static function parseForm(CollectionDto $collectionDto) { |
| 34 | 34 | $form = new Form(false); |
| 35 | 35 | $form->addField(self::generateId()); |
| 36 | - foreach($collectionDto->properties as $property) { |
|
| 36 | + foreach ($collectionDto->properties as $property) { |
|
| 37 | 37 | $values = null; |
| 38 | 38 | $data = []; |
| 39 | 39 | $url = null; |
@@ -58,7 +58,7 @@ discard block |
||
| 58 | 58 | case NOSQLBase::NOSQL_TYPE_ENUM: |
| 59 | 59 | $type = Field::COMBO_TYPE; |
| 60 | 60 | $enumValues = explode('|', $property->enum); |
| 61 | - foreach($enumValues as $value) { |
|
| 61 | + foreach ($enumValues as $value) { |
|
| 62 | 62 | $data[] = [ |
| 63 | 63 | $property->name => $value, |
| 64 | 64 | 'Label' => t($value), |
@@ -21,7 +21,7 @@ discard block |
||
| 21 | 21 | $con = NOSQLParserTrait::initConnection($model, $con); |
| 22 | 22 | $collection = $con->selectCollection($model->getSchema()->name); |
| 23 | 23 | $result = $collection->findOne(['_id' => new ObjectId($pk)]); |
| 24 | - if(null !== $result) { |
|
| 24 | + if (null !== $result) { |
|
| 25 | 25 | $model->feed($result->getArrayCopy()); |
| 26 | 26 | } else { |
| 27 | 27 | throw new ApiException(t('Document not found'), 404); |
@@ -44,12 +44,12 @@ discard block |
||
| 44 | 44 | $filters = []; |
| 45 | 45 | $resultSet->count = $collection->countDocuments($filters); |
| 46 | 46 | $nosqlOptions = [ |
| 47 | - 'limit' => (integer)(array_key_exists(Api::API_LIMIT_FIELD, $criteria) ? $criteria[Api::API_LIMIT_FIELD] : Config::getParam('pagination.limit', 50)), |
|
| 47 | + 'limit' => (integer) (array_key_exists(Api::API_LIMIT_FIELD, $criteria) ? $criteria[Api::API_LIMIT_FIELD] : Config::getParam('pagination.limit', 50)), |
|
| 48 | 48 | ]; |
| 49 | 49 | $results = $collection->find($filters, $nosqlOptions); |
| 50 | 50 | /** @var $result */ |
| 51 | 51 | $items = $results->toArray(); |
| 52 | - foreach($items as $item) { |
|
| 52 | + foreach ($items as $item) { |
|
| 53 | 53 | $model->feed($item->getArrayCopy(), true); |
| 54 | 54 | $resultSet->items[] = $model->getDtoCopy(true); |
| 55 | 55 | } |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | */ |
| 44 | 44 | public function getDtoCopy($cleanPk = false) { |
| 45 | 45 | $copy = clone $this->dto; |
| 46 | - if($cleanPk) { |
|
| 46 | + if ($cleanPk) { |
|
| 47 | 47 | $this->dto->resetPk(); |
| 48 | 48 | } |
| 49 | 49 | return $copy; |
@@ -59,7 +59,7 @@ discard block |
||
| 59 | 59 | */ |
| 60 | 60 | public function save(Database $con = null) { |
| 61 | 61 | $saved = false; |
| 62 | - if(null === $con) { |
|
| 62 | + if (null === $con) { |
|
| 63 | 63 | $con = ParserService::getInstance()->createConnection($this->getDomain()); |
| 64 | 64 | } |
| 65 | 65 | $collection = $con->selectCollection($this->getSchema()->name); |
@@ -67,7 +67,7 @@ discard block |
||
| 67 | 67 | $isInsert = $isUpdate = false; |
| 68 | 68 | $this->prepareData(); |
| 69 | 69 | $this->dto->setLastUpdate(); |
| 70 | - if($this->isNew()) { |
|
| 70 | + if ($this->isNew()) { |
|
| 71 | 71 | $this->preInsert($con); |
| 72 | 72 | $isInsert = true; |
| 73 | 73 | } elseif ($this->isModified()) { |
@@ -75,19 +75,19 @@ discard block |
||
| 75 | 75 | $isUpdate = true; |
| 76 | 76 | } |
| 77 | 77 | $result = $collection->insertOne($this->toArray()); |
| 78 | - if($result->getInsertedCount() > 0) { |
|
| 78 | + if ($result->getInsertedCount() > 0) { |
|
| 79 | 79 | $id = $result->getInsertedId(); |
| 80 | 80 | $this->dto->setPk($id->jsonSerialize()['$oid']); |
| 81 | - if($isInsert) { |
|
| 81 | + if ($isInsert) { |
|
| 82 | 82 | $this->postInsert($con); |
| 83 | - } elseif($isUpdate) { |
|
| 83 | + } elseif ($isUpdate) { |
|
| 84 | 84 | $this->postUpdate($con); |
| 85 | 85 | } |
| 86 | 86 | $saved = true; |
| 87 | 87 | $this->countAction(); |
| 88 | 88 | } |
| 89 | - } catch(\Exception $exception) { |
|
| 90 | - if($exception instanceof NOSQLValidationException) { |
|
| 89 | + } catch (\Exception $exception) { |
|
| 90 | + if ($exception instanceof NOSQLValidationException) { |
|
| 91 | 91 | throw $exception; |
| 92 | 92 | } else { |
| 93 | 93 | Logger::log($exception->getMessage(), LOG_CRIT, $this->toArray()); |
@@ -102,7 +102,7 @@ discard block |
||
| 102 | 102 | */ |
| 103 | 103 | public function update(Database $con = null) { |
| 104 | 104 | $updated = false; |
| 105 | - if(null === $con) { |
|
| 105 | + if (null === $con) { |
|
| 106 | 106 | $con = ParserService::getInstance()->createConnection($this->getDomain()); |
| 107 | 107 | } |
| 108 | 108 | $collection = $con->selectCollection($this->getSchema()->name); |
@@ -116,8 +116,8 @@ discard block |
||
| 116 | 116 | $this->postUpdate($con); |
| 117 | 117 | $updated = true; |
| 118 | 118 | $this->countAction(); |
| 119 | - } catch(\Exception $exception) { |
|
| 120 | - if($exception instanceof NOSQLValidationException) { |
|
| 119 | + } catch (\Exception $exception) { |
|
| 120 | + if ($exception instanceof NOSQLValidationException) { |
|
| 121 | 121 | throw $exception; |
| 122 | 122 | } else { |
| 123 | 123 | Logger::log($exception->getMessage(), LOG_CRIT, $this->toArray()); |
@@ -133,7 +133,7 @@ discard block |
||
| 133 | 133 | */ |
| 134 | 134 | public function bulkInsert(array $data, Database $con = null) { |
| 135 | 135 | $inserts = 0; |
| 136 | - if(null === $con) { |
|
| 136 | + if (null === $con) { |
|
| 137 | 137 | $con = ParserService::getInstance()->createConnection($this->getDomain()); |
| 138 | 138 | } |
| 139 | 139 | $collection = $con->selectCollection($this->getSchema()->name); |
@@ -143,7 +143,7 @@ discard block |
||
| 143 | 143 | $ids = $result->getInsertedIds(); |
| 144 | 144 | $inserts = $this->parseInsertedDtos($con, $ids, $dtos); |
| 145 | 145 | $this->setActionCount($inserts); |
| 146 | - } catch(\Exception $exception) { |
|
| 146 | + } catch (\Exception $exception) { |
|
| 147 | 147 | Logger::log($exception->getMessage(), LOG_CRIT, $this->toArray()); |
| 148 | 148 | } |
| 149 | 149 | return $inserts; |
@@ -155,7 +155,7 @@ discard block |
||
| 155 | 155 | */ |
| 156 | 156 | public function delete(Database $con = null) { |
| 157 | 157 | $deleted = false; |
| 158 | - if(null === $con) { |
|
| 158 | + if (null === $con) { |
|
| 159 | 159 | $con = ParserService::getInstance()->createConnection($this->getDomain()); |
| 160 | 160 | } |
| 161 | 161 | $collection = $con->selectCollection($this->getSchema()->name); |
@@ -166,7 +166,7 @@ discard block |
||
| 166 | 166 | $deleted = true; |
| 167 | 167 | $this->dto = null; |
| 168 | 168 | $this->countAction(); |
| 169 | - } catch(\Exception $exception) { |
|
| 169 | + } catch (\Exception $exception) { |
|
| 170 | 170 | Logger::log($exception->getMessage(), LOG_CRIT, $this->toArray()); |
| 171 | 171 | } |
| 172 | 172 | return $deleted; |
@@ -79,7 +79,7 @@ discard block |
||
| 79 | 79 | * @param string $property |
| 80 | 80 | */ |
| 81 | 81 | public function addChanges($property) { |
| 82 | - if(!in_array($property, $this->changes)) { |
|
| 82 | + if (!in_array($property, $this->changes)) { |
|
| 83 | 83 | $this->changes[] = $property; |
| 84 | 84 | } |
| 85 | 85 | } |
@@ -111,7 +111,7 @@ discard block |
||
| 111 | 111 | * @throws \NOSQL\Exceptions\NOSQLValidationException |
| 112 | 112 | */ |
| 113 | 113 | public static function invokeHook(NOSQLActiveRecord $model, NOSQLModelDto $dto, $hook, Database $con = null) { |
| 114 | - if(method_exists($model, $hook)) { |
|
| 114 | + if (method_exists($model, $hook)) { |
|
| 115 | 115 | $con = self::initConnection($model, $con); |
| 116 | 116 | $model->feed($dto->toArray()); |
| 117 | 117 | $model->$hook($con); |
@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | public function __get($name) |
| 25 | 25 | { |
| 26 | 26 | $value = null; |
| 27 | - if(null !== $this->dto && property_exists($this->dto, $name)) { |
|
| 27 | + if (null !== $this->dto && property_exists($this->dto, $name)) { |
|
| 28 | 28 | $value = $this->dto->$name; |
| 29 | 29 | } |
| 30 | 30 | return $value; |
@@ -36,7 +36,7 @@ discard block |
||
| 36 | 36 | */ |
| 37 | 37 | public function __set($name, $value) |
| 38 | 38 | { |
| 39 | - if(null !== $this->dto && property_exists($this->dto, $name)) { |
|
| 39 | + if (null !== $this->dto && property_exists($this->dto, $name)) { |
|
| 40 | 40 | $this->dto->$name = $value; |
| 41 | 41 | $this->addChanges($name); |
| 42 | 42 | } |
@@ -45,9 +45,9 @@ discard block |
||
| 45 | 45 | |
| 46 | 46 | public function __call($name, $arguments) |
| 47 | 47 | { |
| 48 | - if(preg_match('/^(set|get)/', $name)) { |
|
| 48 | + if (preg_match('/^(set|get)/', $name)) { |
|
| 49 | 49 | $property = str_replace(['set', 'Set', 'get', 'Get'], '', $name); |
| 50 | - if(false !== stripos($name, 'set')) { |
|
| 50 | + if (false !== stripos($name, 'set')) { |
|
| 51 | 51 | $this->$property = $arguments[0]; |
| 52 | 52 | } else { |
| 53 | 53 | return $this->$property; |
@@ -64,25 +64,25 @@ discard block |
||
| 64 | 64 | public function feed(array $data, $withName = false) { |
| 65 | 65 | $name = ''; |
| 66 | 66 | $sep = ''; |
| 67 | - foreach($data as $key => $value) { |
|
| 68 | - if($value instanceof ObjectId) { |
|
| 67 | + foreach ($data as $key => $value) { |
|
| 68 | + if ($value instanceof ObjectId) { |
|
| 69 | 69 | $this->dto->setPk($value->jsonSerialize()['$oid']); |
| 70 | - } elseif($key === '_last_update') { |
|
| 70 | + } elseif ($key === '_last_update') { |
|
| 71 | 71 | $this->dto->setLastUpdate($value instanceof UTCDateTime ? $value : null); |
| 72 | 72 | } else { |
| 73 | - switch(get_class($value)) { |
|
| 73 | + switch (get_class($value)) { |
|
| 74 | 74 | case UTCDateTime::class: |
| 75 | 75 | $value = $value->toDateTime()->format('Y-m-d H:i:s'); |
| 76 | 76 | break; |
| 77 | 77 | } |
| 78 | 78 | $this->$key = $value; |
| 79 | - if(in_array(strtolower($key), ['name', 'label', 'title', 'method'])) { |
|
| 79 | + if (in_array(strtolower($key), ['name', 'label', 'title', 'method'])) { |
|
| 80 | 80 | $name .= $sep . $value; |
| 81 | 81 | $sep = ' '; |
| 82 | 82 | } |
| 83 | 83 | } |
| 84 | 84 | } |
| 85 | - if($withName) { |
|
| 85 | + if ($withName) { |
|
| 86 | 86 | $this->dto->setName($name); |
| 87 | 87 | } |
| 88 | 88 | } |
@@ -56,7 +56,7 @@ |
||
| 56 | 56 | public function getApi() |
| 57 | 57 | { |
| 58 | 58 | $class = explode('\\', get_called_class()); |
| 59 | - return $class[count($class)-1]; |
|
| 59 | + return $class[count($class) - 1]; |
|
| 60 | 60 | } |
| 61 | 61 | |
| 62 | 62 | /** |
@@ -51,7 +51,7 @@ discard block |
||
| 51 | 51 | */ |
| 52 | 52 | public function setPk(string $id) |
| 53 | 53 | { |
| 54 | - if(!empty($this->_id)) { |
|
| 54 | + if (!empty($this->_id)) { |
|
| 55 | 55 | throw new NOSQLValidationException(t('Primary key already defined'), NOSQLValidationException::NOSQL_VALIDATION_ID_ALREADY_DEFINED); |
| 56 | 56 | } |
| 57 | 57 | $this->_id = $id; |
@@ -64,7 +64,7 @@ discard block |
||
| 64 | 64 | public function getLastUpdate($format = null) |
| 65 | 65 | { |
| 66 | 66 | $value = $this->_last_update; |
| 67 | - if(null !== $format) { |
|
| 67 | + if (null !== $format) { |
|
| 68 | 68 | |
| 69 | 69 | } |
| 70 | 70 | return $value; |
@@ -104,11 +104,11 @@ discard block |
||
| 104 | 104 | public function validate($throwException = false) { |
| 105 | 105 | $errors = []; |
| 106 | 106 | $reflection = new \ReflectionClass(get_called_class()); |
| 107 | - foreach($reflection->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) { |
|
| 107 | + foreach ($reflection->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) { |
|
| 108 | 108 | $required = InjectorHelper::checkIsRequired($property->getDocComment()); |
| 109 | 109 | $value = $property->getValue($this); |
| 110 | - if($required && empty($value)) { |
|
| 111 | - if($throwException) { |
|
| 110 | + if ($required && empty($value)) { |
|
| 111 | + if ($throwException) { |
|
| 112 | 112 | throw new NOSQLValidationException(t('Empty value for property ') . $property->getName(), NOSQLValidationException::NOSQL_VALIDATION_REQUIRED); |
| 113 | 113 | } else { |
| 114 | 114 | $errors[] = $property->getName(); |
@@ -123,10 +123,10 @@ discard block |
||
| 123 | 123 | public function toArray() |
| 124 | 124 | { |
| 125 | 125 | $array = parent::toArray(); |
| 126 | - if(null !== $this->getPk()) { |
|
| 126 | + if (null !== $this->getPk()) { |
|
| 127 | 127 | $array['_id'] = $this->getPk(); |
| 128 | 128 | } |
| 129 | - if(null !== $this->getName()) { |
|
| 129 | + if (null !== $this->getName()) { |
|
| 130 | 130 | $array[Api::API_LIST_NAME_FIELD] = $this->getName(); |
| 131 | 131 | } |
| 132 | 132 | $array['_last_update'] = $this->getLastUpdate(\DateTime::ATOM); |
@@ -150,10 +150,10 @@ discard block |
||
| 150 | 150 | if (!is_numeric($value)) { |
| 151 | 151 | $errors[] = $property->getName(); |
| 152 | 152 | } else { |
| 153 | - if(NOSQLBase::NOSQL_TYPE_INTEGER === strtolower($type)) { |
|
| 154 | - $property->setValue($this, (integer)$value); |
|
| 153 | + if (NOSQLBase::NOSQL_TYPE_INTEGER === strtolower($type)) { |
|
| 154 | + $property->setValue($this, (integer) $value); |
|
| 155 | 155 | } else { |
| 156 | - $property->setValue($this, (float)$value); |
|
| 156 | + $property->setValue($this, (float) $value); |
|
| 157 | 157 | } |
| 158 | 158 | } |
| 159 | 159 | break; |
@@ -172,15 +172,15 @@ discard block |
||
| 172 | 172 | if (!in_array($value, [true, false, 0, 1])) { |
| 173 | 173 | $errors[] = $property->getName(); |
| 174 | 174 | } |
| 175 | - $property->setValue($this, (bool)$value); |
|
| 175 | + $property->setValue($this, (bool) $value); |
|
| 176 | 176 | break; |
| 177 | 177 | case NOSQLBase::NOSQL_TYPE_DATE: |
| 178 | 178 | $dateTime = new \DateTime($value, new \DateTimeZone('UTC')); |
| 179 | - if(!$dateTime) { |
|
| 179 | + if (!$dateTime) { |
|
| 180 | 180 | $errors[] = $property->getName(); |
| 181 | 181 | } else { |
| 182 | 182 | $dateTime->setTimezone(new \DateTimeZone(date_default_timezone_get())); |
| 183 | - $property->setValue($this, new UTCDateTime($dateTime->getTimestamp()*1000)); |
|
| 183 | + $property->setValue($this, new UTCDateTime($dateTime->getTimestamp() * 1000)); |
|
| 184 | 184 | } |
| 185 | 185 | break; |
| 186 | 186 | } |