Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/Exceptions/Callback/CallbackException.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ class CallbackException extends SdkException
*/
public function __construct($message, $code = 400, Throwable $previous = null)
{
parent::__construct("CallbackException: {$message}", $code, $previous);
parent::__construct("CallbackException: $message", $code, $previous);
}
}
29 changes: 19 additions & 10 deletions src/Foundation/Core/Api/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,46 @@ class Client
{
/**
* @param string $userOrGroupToken
* @param string|null $version
* @param string|null $language
* @return Group
*/
public function group(string $userOrGroupToken): Group
public function group(string $userOrGroupToken, string $version = null, string $language = null): Group
{
return new Group($userOrGroupToken);
return new Group($userOrGroupToken, $version, $language);
}

/**
* @param string $token
* @param string $userOrGroupToken
* @param string|null $version
* @param string|null $language
* @return Message
*/
public function message(string $token): Message
public function message(string $userOrGroupToken, string $version = null, string $language = null): Message
{
return new Message($token);
return new Message($userOrGroupToken, $version, $language);
}

/**
* @param string $userOrServiceToken
* @param string $token
* @param string|null $version
* @param string|null $language
* @return User
*/
public function user(string $userOrServiceToken): User
public function user(string $token, string $version = null, string $language = null): User
{
return new User($userOrServiceToken);
return new User($token, $version, $language);
}

/**
* @param string $userOrServiceToken
* @param string|null $version
* @param string|null $language
* @return Database
*/
public function database(string $userOrServiceToken): Database
public function database(string $userOrServiceToken, string $version = null, string $language = null): Database
{
return new Database($userOrServiceToken);
// dd($userOrServiceToken);
return new Database($userOrServiceToken, $version, $language);
}
}
16 changes: 8 additions & 8 deletions src/Foundation/Core/Api/Languages.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
* Class Languages
* @package Hopex\VkSdk\Foundation\Core\Api
*/
enum Languages: string
class Languages
{
case Russian = 'ru';
case English = 'en';
public const RUSSIAN = 'ru';
public const ENGLISH = 'en';

// TODO: Добавить транслитерацию для остальных языков

// case Belorussian = 'be';
// case Spanish = 'es';
// case Finnish = 'fi';
// case German = 'de';
// case Italian = 'it';
// public const Belorussian = 'be';
// public const Spanish = 'es';
// public const Finnish = 'fi';
// public const German = 'de';
// public const Italian = 'it';
}
19 changes: 13 additions & 6 deletions src/Foundation/Core/Api/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use GuzzleHttp\Exception\RequestException;
use Hopex\VkSdk\Exceptions\Api\ApiException;
use Hopex\VkSdk\Exceptions\Api\HttpStatusCodeException;
use Hopex\VkSdk\Exceptions\Callback\UnknownEventException;
use Hopex\VkSdk\Exceptions\SdkException;
use Hopex\VkSdk\Facades\Format;
use Hopex\VkSdk\Facades\SdkConfig;
Expand All @@ -22,21 +23,27 @@
*/
class Request
{
/** @var string */
/**
* @var string
*/
private string $token;

/** @var string|array|null */
private string|null|array $version;
/**
* @var string|float
*/
private string|float $version;

/** @var string|Repository|Application|mixed */
/**
* @var string
*/
private string $language;

/**
* @param string $token
* @param string|null $version
* @param string|float|null $version
* @param string|null $language
*/
public function __construct(string $token, string $version = null, string $language = null)
public function __construct(string $token, string|float $version = null, string $language = null)
{
$this->token = $token;
$this->version = $version ?? SdkConfig::api('version');
Expand Down
209 changes: 185 additions & 24 deletions src/Foundation/Core/Models/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,28 @@

use Hopex\VkSdk\Exceptions\Api\ApiException;
use Hopex\VkSdk\Foundation\Core\Api\Request;
use Hopex\VkSdk\Foundation\Core\Models\Databse\Fields\CityField;
use Hopex\VkSdk\Foundation\Core\Models\Databse\Fields\CountryField;
use Hopex\VkSdk\Foundation\Core\Models\Database\Fields\Chair\ChairField;
use Hopex\VkSdk\Foundation\Core\Models\Database\Fields\Chair\ChairFieldsCollection;
use Hopex\VkSdk\Foundation\Core\Models\Database\Fields\Chair\ChairRequestFields;
use Hopex\VkSdk\Foundation\Core\Models\Database\Fields\City\CityField;
use Hopex\VkSdk\Foundation\Core\Models\Database\Fields\City\CityFieldsCollection;
use Hopex\VkSdk\Foundation\Core\Models\Database\Fields\City\CityRequestFields;
use Hopex\VkSdk\Foundation\Core\Models\Database\Fields\Country\CountryField;
use Hopex\VkSdk\Foundation\Core\Models\Database\Fields\Country\CountryFieldsCollection;
use Hopex\VkSdk\Foundation\Core\Models\Database\Fields\Country\CountryRequestFields;
use Hopex\VkSdk\Foundation\Core\Models\Database\Fields\Faculty\FacultyFieldsCollection;
use Hopex\VkSdk\Foundation\Core\Models\Database\Fields\Faculty\FacultyRequestFields;
use Hopex\VkSdk\Foundation\Core\Models\Database\Fields\Metro\MetroStationField;
use Hopex\VkSdk\Foundation\Core\Models\Database\Fields\Metro\MetroStationFieldsCollection;
use Hopex\VkSdk\Foundation\Core\Models\Database\Fields\Metro\MetroStationRequestFields;
use Hopex\VkSdk\Foundation\Core\Models\Database\Fields\Region\RegionFieldsCollection;
use Hopex\VkSdk\Foundation\Core\Models\Database\Fields\Region\RegionRequestFields;
use Hopex\VkSdk\Foundation\Core\Models\Database\Fields\School\SchoolClasses\SchoolClassField;
use Hopex\VkSdk\Foundation\Core\Models\Database\Fields\School\SchoolClasses\SchoolClassFieldsCollection;
use Hopex\VkSdk\Foundation\Core\Models\Database\Fields\School\SchoolClasses\SchoolClassRequestFields;
use Hopex\VkSdk\Foundation\Core\Models\Database\Fields\School\SchoolField;
use Hopex\VkSdk\Foundation\Core\Models\Database\Fields\School\SchoolFieldsCollection;
use Hopex\VkSdk\Foundation\Core\Models\Database\Fields\School\SchoolRequestFields;
use Illuminate\Support\Collection;
use Throwable;

Expand All @@ -16,60 +36,201 @@
class Database extends Request
{
private const SCOPE = 'database.';
private const CITY_IDS = 'city_ids';
private const COUNTRY_IDS = 'country_ids';

/**
* @param array $args
* @param CityRequestFields $cityRequestFields
* @return CityFieldsCollection
* @throws ApiException
* @throws Throwable
*/
public function getCities(CityRequestFields $cityRequestFields): CityFieldsCollection
{
return new CityFieldsCollection($this->call(self::SCOPE . 'getCities', get_object_vars($cityRequestFields)));
}

/**
* @param CityRequestFields $cityRequestFields
* @return Collection
* @throws ApiException
* @throws Throwable
*/
public function getCitiesById(array $args = []): Collection
public function getCitiesById(CityRequestFields $cityRequestFields): Collection
{
return collect($this->call(self::SCOPE . 'getCitiesById', $args))
->map(function ($item) {
return new CityField($item);
});
return collect($this->call(self::SCOPE . 'getCitiesById', get_object_vars($cityRequestFields))
->map(function ($city) {
return new CityField($city);
}));
}

/**
* @param int $cityId
* @return Collection
* @return ChairField
* @throws ApiException
* @throws Throwable
*/
public function getCityById(int $cityId): ChairField
{
return $this->getCitiesById((new CityRequestFields())->setCityIds([$cityId]))->first();
}

/**
* @param CountryRequestFields $countryRequestFields
* @return CountryFieldsCollection
* @throws ApiException
* @throws Throwable
*/
public function getCityById(int $cityId): Collection
public function getCountries(CountryRequestFields $countryRequestFields): CountryFieldsCollection
{
return $this->getCitiesById([
self::CITY_IDS => $cityId
])->first();
return new CountryFieldsCollection($this->call(self::SCOPE . 'getCountries', get_object_vars($countryRequestFields)));
}

/**
* @param array $args
* @param CountryRequestFields $countryRequestFields
* @return Collection
* @throws ApiException
* @throws Throwable
*/
public function getCountriesById(array $args = []): Collection
public function getCountriesById(CountryRequestFields $countryRequestFields): Collection
{
return collect($this->call(self::SCOPE . 'getCountriesById', $args))
->map(function ($item) {
return new CountryField($item);
return collect($this->call(self::SCOPE . 'getCountriesById', get_object_vars($countryRequestFields)))
->map(function ($country) {
return new CountryField($country);
});
}

/**
* @param int $countryId
* @return CountryField
* @throws ApiException
* @throws Throwable
*/
public function getCountryById(int $countryId): CountryField
{
return $this->getCountriesById((new CountryRequestFields())->setCountryIds([$countryId]))->first();
}

/**
* @param ChairRequestFields $chairRequestFields
* @return ChairFieldsCollection
* @throws ApiException
* @throws Throwable
*/
public function getChairs(ChairRequestFields $chairRequestFields): ChairFieldsCollection
{
return new ChairFieldsCollection($this->call(self::SCOPE . 'getChairs', get_object_vars($chairRequestFields)));
}

/**
* @param MetroStationRequestFields $metroStationRequestFields
* @return MetroStationFieldsCollection
* @throws ApiException
* @throws Throwable
*/
public function getMetroStations(MetroStationRequestFields $metroStationRequestFields): MetroStationFieldsCollection
{
return new MetroStationFieldsCollection($this->call(self::SCOPE . 'getMetroStations', get_object_vars($metroStationRequestFields)));
}

/**
* @param MetroStationRequestFields $metroStationRequestFields
* @return Collection
* @throws ApiException
* @throws Throwable
*/
public function getCountryById(int $countryId): Collection
public function getMetroStationsById(MetroStationRequestFields $metroStationRequestFields): Collection
{
return collect($this->call(self::SCOPE . 'getMetroStationsById', get_object_vars($metroStationRequestFields))
->map(function ($city) {
return new MetroStationField($city);
}));
}

/**
* @param int $stationId
* @return MetroStationField
* @throws ApiException
* @throws Throwable
*/
public function getMetroStationById(int $stationId): MetroStationField
{
return $this->getCountriesById([
self::COUNTRY_IDS => $countryId
])->first();
return $this->getMetroStationsById((new MetroStationRequestFields())->setStationIds([$stationId]))->first();
}

/**
* @param FacultyRequestFields $facultyRequestFields
* @return FacultyFieldsCollection
* @throws ApiException
* @throws Throwable
*/
public function getFaculties(FacultyRequestFields $facultyRequestFields): FacultyFieldsCollection
{
return new FacultyFieldsCollection($this->call(self::SCOPE . 'getFaculties', get_object_vars($facultyRequestFields)));
}

/**
* @param RegionRequestFields $regionRequestFields
* @return RegionFieldsCollection
* @throws ApiException
* @throws Throwable
*/
public function getRegions(RegionRequestFields $regionRequestFields): RegionFieldsCollection
{
return new RegionFieldsCollection($this->call(self::SCOPE . 'getRegions', get_object_vars($regionRequestFields)));
}

/**
* @param SchoolRequestFields $schoolRequestFields
* @return SchoolFieldsCollection
* @throws ApiException
* @throws Throwable
*/
public function getSchools(SchoolRequestFields $schoolRequestFields): SchoolFieldsCollection
{
return new SchoolFieldsCollection($this->call(self::SCOPE . 'getSchools', get_object_vars($schoolRequestFields)));
}

/**
* @param SchoolRequestFields $schoolRequestFields
* @param int $schoolId
* @return SchoolField
* @throws ApiException
* @throws Throwable
*/
public function getSchoolByIds(SchoolRequestFields $schoolRequestFields, int $schoolId): SchoolField
{
return $this
->getSchools($schoolRequestFields)
->getItems()
->first(function (SchoolField $school) use ($schoolId) {
return $school->getId() == $schoolId;
});
}

/**
* @param SchoolClassRequestFields $schoolClassRequestFields
* @return SchoolClassFieldsCollection
* @throws ApiException
* @throws Throwable
*/
public function getSchoolClasses(SchoolClassRequestFields $schoolClassRequestFields): SchoolClassFieldsCollection
{
return new SchoolClassFieldsCollection($this->call(self::SCOPE . 'getSchoolClasses', get_object_vars($schoolClassRequestFields)));
}

/**
* @param SchoolClassRequestFields $schoolClassRequestFields
* @param int $schoolClassId
* @return SchoolClassField
* @throws ApiException
* @throws Throwable
*/
public function getSchoolClassById(SchoolClassRequestFields $schoolClassRequestFields, int $schoolClassId): SchoolClassField
{
return $this
->getSchoolClasses($schoolClassRequestFields)
->getItems()
->first(function (SchoolClassField $class) use ($schoolClassId) {
return $class->getId() == $schoolClassId;
});
}
}
Loading