Словари с поиском

This commit is contained in:
Artem Vasilev 2022-11-04 16:36:11 +03:00
parent 141e439575
commit 05a93163dc
12 changed files with 451 additions and 0 deletions

View File

@ -0,0 +1,32 @@
<?php
namespace Webmasterskaya\CryptoPro\Dictionary;
abstract class AbstractDictionary implements DictionaryInterface
{
protected const MAP = [];
protected static function getResult($data)
{
$implements = class_implements(static::class) ?: [];
$options = [];
if (!!$implements[TitleAwareInterface::class])
{
$options['title'] = $data['title'] ?? null;
}
if (!!$implements[OIDAwareInterface::class])
{
$options['OID'] = $data['OID'] ?? null;
}
if (!!$implements[RDNAwareInterface::class])
{
$options['RDN'] = $data['RDN'] ?? null;
}
return DictionaryItem::getItem($options);
}
}

View File

@ -0,0 +1,8 @@
<?php
namespace Webmasterskaya\CryptoPro\Dictionary;
interface DescriptionAwareInterface
{
}

View File

@ -0,0 +1,8 @@
<?php
namespace Webmasterskaya\CryptoPro\Dictionary;
interface DictionaryInterface
{
}

View File

@ -0,0 +1,34 @@
<?php
namespace Webmasterskaya\CryptoPro\Dictionary;
abstract class DictionaryItem
{
protected $options;
protected function __construct(array $options)
{
$this->options = $options;
}
public function __get($name)
{
if (!isset($this->options[$name]))
{
user_error("Can't set property: " . __CLASS__ . "->" . $name);
}
return $this->options[$name];
}
public function __set($name, $value)
{
user_error("Can't set property: " . __CLASS__ . "->" . $name);
}
public static function getItem(array $options)
{
return new class($options) extends DictionaryItem {
};
}
}

View File

@ -0,0 +1,104 @@
<?php
namespace Webmasterskaya\CryptoPro\Dictionary;
class IssuerTagsDictionary extends AbstractDictionary implements RDNAwareInterface, TitleAwareInterface, OIDAwareInterface
{
use RDNAwareTrait;
use TitleAwareTrait;
use OIDAwareTrait;
protected const MAP
= [
[
'RDN' => 'UN',
'OID' => '1.2.840.113549.1.9.2',
'title' => 'Неструктурированное имя',
'title_variants' => ['unstructuredName'],
],
[
'RDN' => 'CN',
'OID' => '2.5.4.3',
'title' => 'Удостоверяющий центр',
'title_variants' => ['commonName'],
],
[
'RDN' => 'C',
'OID' => '2.5.4.6',
'title' => 'Страна',
'title_variants' => ['countryName'],
],
[
'RDN' => 'S',
'OID' => '2.5.4.8',
'title' => 'Регион',
'title_variants' => ['ST', 'stateOrProvinceName'],
],
[
'RDN' => 'STREET',
'OID' => '2.5.4.9',
'title' => 'Адрес',
'title_variants' => ['streetAddress'],
],
[
'RDN' => 'O',
'OID' => '2.5.4.10',
'title' => 'Компания',
'title_variants' => ['organizationName'],
],
[
'RDN' => 'OU',
'OID' => '2.5.4.11',
'title' => 'Тип',
'title_variants' => ['organizationalUnitName'],
],
[
'RDN' => 'T',
'OID' => '2.5.4.12',
'title' => 'Должность',
'title_variants' => ['TITLE'],
],
[
'RDN' => 'OGRN',
'OID' => '1.2.643.100.1',
'title' => 'ОГРН',
'title_variants' => ['ОГРН'],
],
[
'RDN' => 'OGRNIP',
'OID' => '1.2.643.100.5',
'title' => 'ОГРНИП',
'title_variants' => ['ОГРНИП'],
],
[
'RDN' => 'SNILS',
'OID' => '1.2.643.100.3',
'title' => 'СНИЛС',
'title_variants' => ['СНИЛС'],
],
[
'RDN' => 'INN',
'OID' => '1.2.643.3.131.1.1',
'title' => 'ИНН',
'title_variants' => ['ИННФЛ', 'ИНН ФЛ'],
],
[
'RDN' => 'INNLE',
'OID' => '1.2.643.100.4',
'title' => 'ИНН организации',
'title_variants' => ['ИННЮЛ', 'ИНН ЮЛ', 'INN LE'],
],
[
'RDN' => 'E',
'OID' => '1.2.840.113549.1.9.1',
'title' => 'Email',
'title_variants' => ['email', 'emailAddress', 'pkcs9email'],
],
[
'RDN' => 'L',
'OID' => '2.5.4.7',
'title' => 'Город',
'title_variants' => ['localityName'],
],
];
}

View File

@ -0,0 +1,8 @@
<?php
namespace Webmasterskaya\CryptoPro\Dictionary;
interface OIDAwareInterface
{
public static function getByOID(string $oid);
}

View File

@ -0,0 +1,34 @@
<?php
namespace Webmasterskaya\CryptoPro\Dictionary;
trait OIDAwareTrait
{
public static function getByOID(string $oid)
{
$oid = mb_strtolower(trim($oid));
$map = self::getOIDMap();
return isset($map[$oid]) ? self::getResult($map[$oid]) : null;
}
protected static function getOIDMap()
{
static $OIDMap;
if (!isset($OIDMap))
{
foreach (self::MAP as $row)
{
if (isset($row['OID']))
{
$variant = mb_strtolower($row['OID']);
$OIDMap[$variant] = $row;
}
}
}
return $OIDMap;
}
}

View File

@ -0,0 +1,8 @@
<?php
namespace Webmasterskaya\CryptoPro\Dictionary;
interface RDNAwareInterface
{
public static function getByRDN(string $RDN);
}

View File

@ -0,0 +1,34 @@
<?php
namespace Webmasterskaya\CryptoPro\Dictionary;
trait RDNAwareTrait
{
public static function getByRDN(string $RDN)
{
$RDN = mb_strtolower(trim($RDN));
$map = self::getRDNMap();
return isset($map[$RDN]) ? self::getResult($map[$RDN]) : null;
}
protected static function getRDNMap()
{
static $RDNMap;
if (!isset($RDNMap))
{
foreach (self::MAP as $row)
{
if (isset($row['RDN']))
{
$variant = mb_strtolower($row['RDN']);
$RDNMap[$variant] = $row;
}
}
}
return $RDNMap;
}
}

View File

@ -0,0 +1,116 @@
<?php
namespace Webmasterskaya\CryptoPro\Dictionary;
class SubjectTagsDictionary extends AbstractDictionary implements RDNAwareInterface, TitleAwareInterface, OIDAwareInterface
{
use RDNAwareTrait;
use TitleAwareTrait;
use OIDAwareTrait;
protected const MAP
= [
[
'RDN' => 'UN',
'OID' => '1.2.840.113549.1.9.2',
'title' => 'Неструктурированное имя',
'title_variants' => ['unstructuredName'],
],
[
'RDN' => 'CN',
'OID' => '2.5.4.3',
'title' => 'Владелец',
'title_variants' => ['commonName'],
],
[
'RDN' => 'SN',
'OID' => '2.5.4.4',
'title' => 'Фамилия',
'title_variants' => ['surname'],
],
[
'RDN' => 'G',
'OID' => '2.5.4.42',
'title' => 'Имя Отчество',
'title_variants' => ['givenName', 'gn'],
],
[
'RDN' => 'C',
'OID' => '2.5.4.6',
'title' => 'Страна',
'title_variants' => ['countryName'],
],
[
'RDN' => 'S',
'OID' => '2.5.4.8',
'title' => 'Регион',
'title_variants' => ['ST', 'stateOrProvinceName'],
],
[
'RDN' => 'STREET',
'OID' => '2.5.4.9',
'title' => 'Адрес',
'title_variants' => ['streetAddress'],
],
[
'RDN' => 'O',
'OID' => '2.5.4.10',
'title' => 'Компания',
'title_variants' => ['organizationName'],
],
[
'RDN' => 'OU',
'OID' => '2.5.4.11',
'title' => 'Отдел/подразделение',
'title_variants' => ['organizationalUnitName'],
],
[
'RDN' => 'T',
'OID' => '2.5.4.12',
'title' => 'Должность',
'title_variants' => ['TITLE'],
],
[
'RDN' => 'OGRN',
'OID' => '1.2.643.100.1',
'title' => 'ОГРН',
'title_variants' => ['ОГРН'],
],
[
'RDN' => 'OGRNIP',
'OID' => '1.2.643.100.5',
'title' => 'ОГРНИП',
'title_variants' => ['ОГРНИП'],
],
[
'RDN' => 'SNILS',
'OID' => '1.2.643.100.3',
'title' => 'СНИЛС',
'title_variants' => ['СНИЛС'],
],
[
'RDN' => 'INN',
'OID' => '1.2.643.3.131.1.1',
'title' => 'ИНН',
'title_variants' => ['ИННФЛ', 'ИНН ФЛ'],
],
[
'RDN' => 'INNLE',
'OID' => '1.2.643.100.4',
'title' => 'ИНН организации',
'title_variants' => ['ИННЮЛ', 'ИНН ЮЛ', 'INN LE'],
],
[
'RDN' => 'E',
'OID' => '1.2.840.113549.1.9.1',
'title' => 'Email',
'title_variants' => ['email', 'emailAddress', 'pkcs9email'],
],
[
'RDN' => 'L',
'OID' => '2.5.4.7',
'title' => 'Город',
'title_variants' => ['localityName'],
],
];
}

View File

@ -0,0 +1,8 @@
<?php
namespace Webmasterskaya\CryptoPro\Dictionary;
interface TitleAwareInterface
{
public static function getByTitle(string $title);
}

View File

@ -0,0 +1,57 @@
<?php
namespace Webmasterskaya\CryptoPro\Dictionary;
trait TitleAwareTrait
{
public static function getByTitle(string $title)
{
$title = mb_strtolower(trim($title));
$map = self::getTitleMap();
return isset($map[$title]) ? self::getResult($map[$title]) : null;
}
protected static function getTitleMap()
{
static $titleMap;
if (!isset($titleMap))
{
foreach (self::MAP as $row)
{
if (isset($row['title_variants']))
{
if (is_string($row['title_variants']))
{
$variant = mb_strtolower($row['title_variants']);
$titleMap[$variant] = $row;
}
else
{
foreach ($row['title_variants'] as $variant)
{
$variant = mb_strtolower($variant);
$titleMap[$variant] = $row;
}
}
}
if (isset($row['title']))
{
$variant = mb_strtolower($row['title']);
$titleMap[$variant] = $row;
}
if (isset($row['RDN']))
{
$variant = mb_strtolower($row['RDN']);
$titleMap[$variant] = $row;
}
}
}
return $titleMap;
}
}