@@ -5,25 +5,25 @@ discard block |
||
| 5 | 5 | * @version 1.0 |
| 6 | 6 | * Autogenerated autoloader [2019-01-03 15:23:58] |
| 7 | 7 | */ |
| 8 | -if(!defined("BASE_DIR")) |
|
| 9 | -define("BASE_DIR", dirname( dirname(__DIR__) ) ); |
|
| 8 | +if (!defined("BASE_DIR")) |
|
| 9 | +define("BASE_DIR", dirname(dirname(__DIR__))); |
|
| 10 | 10 | |
| 11 | 11 | require_once __DIR__ . DIRECTORY_SEPARATOR . 'Config' . DIRECTORY_SEPARATOR . 'config.php'; |
| 12 | 12 | |
| 13 | -if(!function_exists("NOSQL_Autoloader")) { |
|
| 13 | +if (!function_exists("NOSQL_Autoloader")) { |
|
| 14 | 14 | // autoloader |
| 15 | - function NOSQL_Autoloader( $class ) { |
|
| 15 | + function NOSQL_Autoloader($class) { |
|
| 16 | 16 | // it only autoload class into the Rain scope |
| 17 | 17 | if (false !== preg_match('/^\\\?NOSQL/', $class)) { |
| 18 | 18 | // Change order src |
| 19 | 19 | $class = preg_replace('/^\\\?NOSQL/', 'NOSQL', $class); |
| 20 | 20 | // transform the namespace in path |
| 21 | - $path = str_replace("\\", DIRECTORY_SEPARATOR, $class ); |
|
| 21 | + $path = str_replace("\\", DIRECTORY_SEPARATOR, $class); |
|
| 22 | 22 | // filepath |
| 23 | 23 | $abs_path = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . $path . ".php"; |
| 24 | 24 | |
| 25 | 25 | // require the file |
| 26 | - if(file_exists($abs_path)) { |
|
| 26 | + if (file_exists($abs_path)) { |
|
| 27 | 27 | require_once $abs_path; |
| 28 | 28 | } |
| 29 | 29 | } |
@@ -31,5 +31,5 @@ discard block |
||
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | // register the autoloader |
| 34 | - spl_autoload_register( "NOSQL_Autoloader" ); |
|
| 34 | + spl_autoload_register("NOSQL_Autoloader"); |
|
| 35 | 35 | } |
@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | $code = 200; |
| 58 | 58 | try { |
| 59 | 59 | $this->srv->setCollections($module, $this->getRequest()->getRawData()); |
| 60 | - } catch(\Exception $exception) { |
|
| 60 | + } catch (\Exception $exception) { |
|
| 61 | 61 | $success = false; |
| 62 | 62 | $code = 400; |
| 63 | 63 | Logger::log($exception->getMessage(), LOG_WARNING); |
@@ -75,7 +75,7 @@ discard block |
||
| 75 | 75 | $code = 200; |
| 76 | 76 | try { |
| 77 | 77 | $success = $this->srv->syncCollections($module); |
| 78 | - } catch(\Exception $exception) { |
|
| 78 | + } catch (\Exception $exception) { |
|
| 79 | 79 | $success = false; |
| 80 | 80 | $code = 400; |
| 81 | 81 | Logger::log($exception->getMessage(), LOG_WARNING); |
@@ -23,7 +23,7 @@ discard block |
||
| 23 | 23 | public function __get($name) |
| 24 | 24 | { |
| 25 | 25 | $value = null; |
| 26 | - if(null !== $this->dto && property_exists($this->dto, $name)) { |
|
| 26 | + if (null !== $this->dto && property_exists($this->dto, $name)) { |
|
| 27 | 27 | $value = $this->dto->$name; |
| 28 | 28 | } |
| 29 | 29 | return $value; |
@@ -35,7 +35,7 @@ discard block |
||
| 35 | 35 | */ |
| 36 | 36 | public function __set($name, $value) |
| 37 | 37 | { |
| 38 | - if(null !== $this->dto && property_exists($this->dto, $name)) { |
|
| 38 | + if (null !== $this->dto && property_exists($this->dto, $name)) { |
|
| 39 | 39 | $this->dto->$name = $value; |
| 40 | 40 | $this->addChanges($name); |
| 41 | 41 | } |
@@ -44,9 +44,9 @@ discard block |
||
| 44 | 44 | |
| 45 | 45 | public function __call($name, $arguments) |
| 46 | 46 | { |
| 47 | - if(preg_match('/^(set|get)/', $name)) { |
|
| 47 | + if (preg_match('/^(set|get)/', $name)) { |
|
| 48 | 48 | $property = str_replace(['set', 'Set', 'get', 'Get'], '', $name); |
| 49 | - if(false !== stripos($name, 'set')) { |
|
| 49 | + if (false !== stripos($name, 'set')) { |
|
| 50 | 50 | $this->$property = $arguments[0]; |
| 51 | 51 | } else { |
| 52 | 52 | return $this->$property; |
@@ -60,10 +60,10 @@ discard block |
||
| 60 | 60 | * @throws \NOSQL\Exceptions\NOSQLValidationException |
| 61 | 61 | */ |
| 62 | 62 | public function feed(array $data) { |
| 63 | - foreach($data as $key => $value) { |
|
| 64 | - if($value instanceof ObjectId) { |
|
| 63 | + foreach ($data as $key => $value) { |
|
| 64 | + if ($value instanceof ObjectId) { |
|
| 65 | 65 | $this->dto->setPk($value->jsonSerialize()['$oid']); |
| 66 | - } elseif($key === '_last_update') { |
|
| 66 | + } elseif ($key === '_last_update') { |
|
| 67 | 67 | $this->dto->setLastUpdate(\DateTime::createFromFormat(\DateTime::ATOM, $value)); |
| 68 | 68 | } else { |
| 69 | 69 | $this->$key = $value; |
@@ -69,17 +69,17 @@ |
||
| 69 | 69 | * @throws \PSFS\base\exception\GeneratorException |
| 70 | 70 | */ |
| 71 | 71 | protected function hydrate() { |
| 72 | - if(empty($this->domain)) { |
|
| 72 | + if (empty($this->domain)) { |
|
| 73 | 73 | throw new NOSQLParserException(t('Domain not defined'), NOSQLParserException::NOSQL_PARSER_DOMAIN_NOT_DEFINED); |
| 74 | 74 | } |
| 75 | 75 | $schemaFilename = CORE_DIR . DIRECTORY_SEPARATOR . $this->domain . DIRECTORY_SEPARATOR . 'Config' . DIRECTORY_SEPARATOR . 'schema.json'; |
| 76 | - if(file_exists($schemaFilename)) { |
|
| 76 | + if (file_exists($schemaFilename)) { |
|
| 77 | 77 | $schema = Cache::getInstance()->getDataFromFile($schemaFilename, Cache::JSON, true); |
| 78 | 78 | $class = get_called_class(); |
| 79 | 79 | $this->schema = new CollectionDto(false); |
| 80 | - foreach($schema as $collection) { |
|
| 80 | + foreach ($schema as $collection) { |
|
| 81 | 81 | $collectionName = $collection['name']; |
| 82 | - if(false !== strpos($class, $collectionName)) { |
|
| 82 | + if (false !== strpos($class, $collectionName)) { |
|
| 83 | 83 | $this->schema->fromArray($collection); |
| 84 | 84 | break; |
| 85 | 85 | } |
@@ -36,7 +36,7 @@ discard block |
||
| 36 | 36 | */ |
| 37 | 37 | public function setPk(string $id) |
| 38 | 38 | { |
| 39 | - if(!empty($this->_id)) { |
|
| 39 | + if (!empty($this->_id)) { |
|
| 40 | 40 | throw new NOSQLValidationException(t('Primary key already defined'), NOSQLValidationException::NOSQL_VALIDATION_ID_ALREADY_DEFINED); |
| 41 | 41 | } |
| 42 | 42 | $this->_id = $id; |
@@ -74,11 +74,11 @@ discard block |
||
| 74 | 74 | public function validate($throwException = false) { |
| 75 | 75 | $errors = []; |
| 76 | 76 | $reflection = new \ReflectionClass(get_called_class()); |
| 77 | - foreach($reflection->getProperties() as $property) { |
|
| 77 | + foreach ($reflection->getProperties() as $property) { |
|
| 78 | 78 | $required = InjectorHelper::checkIsRequired($property->getDocComment()); |
| 79 | 79 | $value = $property->getValue($this); |
| 80 | - if($required && empty($value)) { |
|
| 81 | - if($throwException) { |
|
| 80 | + if ($required && empty($value)) { |
|
| 81 | + if ($throwException) { |
|
| 82 | 82 | throw new NOSQLValidationException(t('Empty value for property ') . $property->getName(), NOSQLValidationException::NOSQL_VALIDATION_REQUIRED); |
| 83 | 83 | } else { |
| 84 | 84 | $errors[] = $property->getName(); |
@@ -93,7 +93,7 @@ discard block |
||
| 93 | 93 | public function toArray() |
| 94 | 94 | { |
| 95 | 95 | $array = parent::toArray(); |
| 96 | - if(null !== $this->getPk()) { |
|
| 96 | + if (null !== $this->getPk()) { |
|
| 97 | 97 | $array['_id'] = $this->getPk(); |
| 98 | 98 | } |
| 99 | 99 | $array['_last_update'] = $this->getLastUpdate(\DateTime::ATOM); |
@@ -75,7 +75,7 @@ discard block |
||
| 75 | 75 | * @param string $property |
| 76 | 76 | */ |
| 77 | 77 | public function addChanges($property) { |
| 78 | - if(!in_array($property, $this->changes)) { |
|
| 78 | + if (!in_array($property, $this->changes)) { |
|
| 79 | 79 | $this->changes[] = $property; |
| 80 | 80 | } |
| 81 | 81 | } |
@@ -88,7 +88,7 @@ discard block |
||
| 88 | 88 | * @throws \NOSQL\Exceptions\NOSQLValidationException |
| 89 | 89 | */ |
| 90 | 90 | public static function invokeHook(NOSQLActiveRecord $model, NOSQLModelDto $dto, $hook, Database $con = null) { |
| 91 | - if(method_exists($model, $hook)) { |
|
| 91 | + if (method_exists($model, $hook)) { |
|
| 92 | 92 | $con = self::initConnection($con, $model); |
| 93 | 93 | $model->feed($dto->toArray()); |
| 94 | 94 | $model->$hook($con); |
@@ -21,7 +21,7 @@ discard block |
||
| 21 | 21 | $con = NOSQLParserTrait::initConnection($con, $model); |
| 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); |
@@ -47,7 +47,7 @@ discard block |
||
| 47 | 47 | $resultSet->count = $collection->countDocuments($criteria, $nosqlOptions); |
| 48 | 48 | $results = $collection->find($criteria, $nosqlOptions); |
| 49 | 49 | /** @var $result */ |
| 50 | - foreach($results->toArray() as $result) { |
|
| 50 | + foreach ($results->toArray() as $result) { |
|
| 51 | 51 | $model->feed($result->getArrayCopy()); |
| 52 | 52 | $resultSet->items[] = $model->getDtoCopy(true); |
| 53 | 53 | } |
@@ -42,7 +42,7 @@ discard block |
||
| 42 | 42 | */ |
| 43 | 43 | public function getDtoCopy($cleanPk = false) { |
| 44 | 44 | $copy = clone $this->dto; |
| 45 | - if($cleanPk) { |
|
| 45 | + if ($cleanPk) { |
|
| 46 | 46 | $this->dto->resetPk(); |
| 47 | 47 | } |
| 48 | 48 | return $copy; |
@@ -54,14 +54,14 @@ discard block |
||
| 54 | 54 | */ |
| 55 | 55 | public function save(Database $con = null) { |
| 56 | 56 | $saved = false; |
| 57 | - if(null === $con) { |
|
| 57 | + if (null === $con) { |
|
| 58 | 58 | $con = ParserService::getInstance()->createConnection($this->getDomain()); |
| 59 | 59 | } |
| 60 | 60 | $collection = $con->selectCollection($this->getSchema()->name); |
| 61 | 61 | try { |
| 62 | 62 | $isInsert = $isUpdate = false; |
| 63 | 63 | $this->dto->setLastUpdate(new \DateTime()); |
| 64 | - if($this->isNew()) { |
|
| 64 | + if ($this->isNew()) { |
|
| 65 | 65 | $this->preInsert($con); |
| 66 | 66 | $isInsert = true; |
| 67 | 67 | } elseif ($this->isModified()) { |
@@ -69,17 +69,17 @@ discard block |
||
| 69 | 69 | $isUpdate = true; |
| 70 | 70 | } |
| 71 | 71 | $result = $collection->insertOne($this->toArray()); |
| 72 | - if($result->getInsertedCount() > 0) { |
|
| 72 | + if ($result->getInsertedCount() > 0) { |
|
| 73 | 73 | $id = $result->getInsertedId(); |
| 74 | 74 | $this->dto->setPk($id->jsonSerialize()['$oid']); |
| 75 | - if($isInsert) { |
|
| 75 | + if ($isInsert) { |
|
| 76 | 76 | $this->postInsert($con); |
| 77 | - } elseif($isUpdate) { |
|
| 77 | + } elseif ($isUpdate) { |
|
| 78 | 78 | $this->postUpdate($con); |
| 79 | 79 | } |
| 80 | 80 | $saved = true; |
| 81 | 81 | } |
| 82 | - } catch(\Exception $exception) { |
|
| 82 | + } catch (\Exception $exception) { |
|
| 83 | 83 | Logger::log($exception, LOG_CRIT, $this->toArray()); |
| 84 | 84 | } |
| 85 | 85 | return $saved; |
@@ -91,7 +91,7 @@ discard block |
||
| 91 | 91 | */ |
| 92 | 92 | public function update(Database $con = null) { |
| 93 | 93 | $updated = false; |
| 94 | - if(null === $con) { |
|
| 94 | + if (null === $con) { |
|
| 95 | 95 | $con = ParserService::getInstance()->createConnection($this->getDomain()); |
| 96 | 96 | } |
| 97 | 97 | $collection = $con->selectCollection($this->getSchema()->name); |
@@ -103,7 +103,7 @@ discard block |
||
| 103 | 103 | $collection->findOneAndReplace(['_id' => new ObjectId($this->dto->getPk())], $data); |
| 104 | 104 | $this->postUpdate($con); |
| 105 | 105 | $updated = true; |
| 106 | - } catch(\Exception $exception) { |
|
| 106 | + } catch (\Exception $exception) { |
|
| 107 | 107 | Logger::log($exception, LOG_CRIT, $this->toArray()); |
| 108 | 108 | } |
| 109 | 109 | return $updated; |
@@ -116,7 +116,7 @@ discard block |
||
| 116 | 116 | */ |
| 117 | 117 | public function bulkInsert(array $data, Database $con = null) { |
| 118 | 118 | $inserts = 0; |
| 119 | - if(null === $con) { |
|
| 119 | + if (null === $con) { |
|
| 120 | 120 | $con = ParserService::getInstance()->createConnection($this->getDomain()); |
| 121 | 121 | } |
| 122 | 122 | $collection = $con->selectCollection($this->getSchema()->name); |
@@ -125,7 +125,7 @@ discard block |
||
| 125 | 125 | $result = $collection->insertMany($data); |
| 126 | 126 | $ids = $result->getInsertedIds(); |
| 127 | 127 | $inserts = $this->parseInsertedDtos($con, $ids, $dtos); |
| 128 | - } catch(\Exception $exception) { |
|
| 128 | + } catch (\Exception $exception) { |
|
| 129 | 129 | Logger::log($exception, LOG_CRIT, $this->toArray()); |
| 130 | 130 | } |
| 131 | 131 | return $inserts; |
@@ -137,7 +137,7 @@ discard block |
||
| 137 | 137 | */ |
| 138 | 138 | public function delete(Database $con = null) { |
| 139 | 139 | $deleted = false; |
| 140 | - if(null === $con) { |
|
| 140 | + if (null === $con) { |
|
| 141 | 141 | $con = ParserService::getInstance()->createConnection($this->getDomain()); |
| 142 | 142 | } |
| 143 | 143 | $collection = $con->selectCollection($this->getSchema()->name); |
@@ -147,7 +147,7 @@ discard block |
||
| 147 | 147 | $this->postDelete($con); |
| 148 | 148 | $deleted = true; |
| 149 | 149 | $this->dto = null; |
| 150 | - } catch(\Exception $exception) { |
|
| 150 | + } catch (\Exception $exception) { |
|
| 151 | 151 | Logger::log($exception, LOG_CRIT, $this->toArray()); |
| 152 | 152 | } |
| 153 | 153 | return $deleted; |
@@ -27,7 +27,7 @@ discard block |
||
| 27 | 27 | $dns .= '@' . Config::getParam('nosql.host', 'localhost', $lowerDomain); |
| 28 | 28 | |
| 29 | 29 | $database = Config::getParam('nosql.database', 'nosql', $lowerDomain); |
| 30 | - if(null !== Config::getParam('nosql.replicaset')) { |
|
| 30 | + if (null !== Config::getParam('nosql.replicaset')) { |
|
| 31 | 31 | $dns .= '/' . $database . '?ssl=true&replicaSet=' . Config::getParam('nosql.replicaset', null, $lowerDomain); |
| 32 | 32 | $dns .= '&authSource=admin&serverSelectionTryOnce=false&serverSelectionTimeoutMS=15000&retryWrites=true'; |
| 33 | 33 | } else { |
@@ -48,7 +48,7 @@ discard block |
||
| 48 | 48 | */ |
| 49 | 49 | public function checkAndSave($domain, $collection, NOSQLModelDto $dto) { |
| 50 | 50 | $errors = $dto->validate(); |
| 51 | - if(empty($errors)) { |
|
| 51 | + if (empty($errors)) { |
|
| 52 | 52 | |
| 53 | 53 | } else { |
| 54 | 54 | throw new ApiException(t('Dto not valid'), 400); |
@@ -64,7 +64,7 @@ discard block |
||
| 64 | 64 | public function hydrateModelFromRequest(array $data, $className) { |
| 65 | 65 | $model = null; |
| 66 | 66 | $reflectionClass = new \ReflectionClass($className); |
| 67 | - if($reflectionClass->isSubclassOf(NOSQLBase::class)) { |
|
| 67 | + if ($reflectionClass->isSubclassOf(NOSQLBase::class)) { |
|
| 68 | 68 | /** @var NOSQLActiveRecord $modelName */ |
| 69 | 69 | $modelName = $className::MODEL_CLASS; |
| 70 | 70 | $model = $modelName::fromArray($data); |