Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog][keepachangelog] and this project adheres to [Semantic Versioning][semver].

## Unreleased

### Added

- Store references data in memory after first open

## v3.10.0

### Added
Expand Down
14 changes: 11 additions & 3 deletions sdk/php/src/ReferencesData/StaticReference.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ class StaticReference implements StaticReferenceInterface
*/
protected $file_path;

/**
* Source data.
*
* @var mixed[]|object[]|object|null
*/
protected $data;

/**
* Create a new reference instance.
*
Expand Down Expand Up @@ -68,9 +75,10 @@ public function getFilePath(): string
*/
public function getData(bool $as_array = true, int $options = 0)
{
/** @var mixed[]|object[]|object $data */
$data = \json_decode((string) \file_get_contents($this->file_path), $as_array, 512, $options);
if (! isset($this->data)) {
$this->data = \json_decode((string) \file_get_contents($this->file_path), $as_array, 512, $options);
}

return $data;
return $this->data;
}
}
74 changes: 53 additions & 21 deletions sdk/php/src/StaticReferencesData.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,27 @@
namespace AvtoDev\StaticReferencesData;

use AvtoDev\StaticReferencesData\ReferencesData\StaticReference;
use AvtoDev\StaticReferencesData\ReferencesData\StaticReferenceInterface;

class StaticReferencesData
{
/**
* @var array<string, StaticReferenceInterface>
*/
private static $references;

/**
* Path to files with static data. Should be unique.
*/
private const
CADASTRAL_DISTRICTS_PATH = '/data/cadastral/districts.json',
SUBJECT_CODES_PATH = '/data/subject/codes.json',
VEHICLE_FINE_ARTICLES_PATH = '/data/vehicle/fine/articles.json',
VEHICLE_REGISTRATION_ACTIONS_PATH = '/data/vehicle/registration/actions.json',
VEHICLE_REPAIR_METHODS_PATH = '/data/vehicle/repair/methods.json',
VEHICLE_CATEGORIES_PATH = '/data/vehicle/categories.json',
VEHICLE_TYPES_PATH = '/data/vehicle/types.json';

/**
* Get root references directory path.
*
Expand All @@ -27,72 +45,86 @@ public static function getRootDirectoryPath(?string $additional_path = null): st
/**
* Get cadastral districts reference.
*
* @return StaticReference
* @return StaticReferenceInterface
*/
public static function cadastralDistricts(): StaticReference
public static function cadastralDistricts(): StaticReferenceInterface
{
return new StaticReference(static::getRootDirectoryPath('/data/cadastral/districts.json'));
return static::$references[static::CADASTRAL_DISTRICTS_PATH]
?? static::$references[static::CADASTRAL_DISTRICTS_PATH] =
new StaticReference(static::getRootDirectoryPath(static::CADASTRAL_DISTRICTS_PATH));
}

/**
* Get subject codes reference.
*
* @link <https://bit.ly/2GB3bda>
*
* @return StaticReference
* @return StaticReferenceInterface
*/
public static function subjectCodes(): StaticReference
public static function subjectCodes(): StaticReferenceInterface
{
return new StaticReference(static::getRootDirectoryPath('/data/subject/codes.json'));
return static::$references[static::SUBJECT_CODES_PATH]
?? static::$references[static::SUBJECT_CODES_PATH] =
new StaticReference(static::getRootDirectoryPath(static::SUBJECT_CODES_PATH));
}

/**
* Get vehicle fine articles reference.
*
* @return StaticReference
* @return StaticReferenceInterface
*/
public static function vehicleFineArticles(): StaticReference
public static function vehicleFineArticles(): StaticReferenceInterface
{
return new StaticReference(static::getRootDirectoryPath('/data/vehicle/fine/articles.json'));
return static::$references[static::VEHICLE_FINE_ARTICLES_PATH]
?? static::$references[static::VEHICLE_FINE_ARTICLES_PATH] =
new StaticReference(static::getRootDirectoryPath(static::VEHICLE_FINE_ARTICLES_PATH));
}

/**
* Get vehicle registration actions reference.
*
* @return StaticReference
* @return StaticReferenceInterface
*/
public static function vehicleRegistrationActions(): StaticReference
public static function vehicleRegistrationActions(): StaticReferenceInterface
{
return new StaticReference(static::getRootDirectoryPath('/data/vehicle/registration/actions.json'));
return static::$references[static::VEHICLE_REGISTRATION_ACTIONS_PATH]
?? static::$references[static::VEHICLE_REGISTRATION_ACTIONS_PATH] =
new StaticReference(static::getRootDirectoryPath(static::VEHICLE_REGISTRATION_ACTIONS_PATH));
}

/**
* Get vehicle repair methods reference.
*
* @return StaticReference
* @return StaticReferenceInterface
*/
public static function vehicleRepairMethods(): StaticReference
public static function vehicleRepairMethods(): StaticReferenceInterface
{
return new StaticReference(static::getRootDirectoryPath('/data/vehicle/repair/methods.json'));
return static::$references[static::VEHICLE_REPAIR_METHODS_PATH]
?? static::$references[static::VEHICLE_REPAIR_METHODS_PATH] =
new StaticReference(static::getRootDirectoryPath(static::VEHICLE_REPAIR_METHODS_PATH));
}

/**
* Get vehicle categories reference.
*
* @return StaticReference
* @return StaticReferenceInterface
*/
public static function vehicleCategories(): StaticReference
public static function vehicleCategories(): StaticReferenceInterface
{
return new StaticReference(static::getRootDirectoryPath('/data/vehicle/categories.json'));
return static::$references[static::VEHICLE_CATEGORIES_PATH]
?? static::$references[static::VEHICLE_CATEGORIES_PATH] =
new StaticReference(static::getRootDirectoryPath(static::VEHICLE_CATEGORIES_PATH));
}

/**
* Get vehicle types reference.
*
* @return StaticReference
* @return StaticReferenceInterface
*/
public static function vehicleTypes(): StaticReference
public static function vehicleTypes(): StaticReferenceInterface
{
return new StaticReference(static::getRootDirectoryPath('/data/vehicle/types.json'));
return static::$references[static::VEHICLE_TYPES_PATH]
?? static::$references[static::VEHICLE_TYPES_PATH] =
new StaticReference(static::getRootDirectoryPath(static::VEHICLE_TYPES_PATH));
}
}