First commit

This commit is contained in:
Maximilian Ruta 2019-06-25 14:38:56 +02:00
commit 48d33c3011
35 changed files with 1195 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/vendor/
/composer.lock
/tests/green-directory

1
.phpunit.result.cache Normal file
View File

@ -0,0 +1 @@
C:37:"PHPUnit\Runner\DefaultTestResultCache":831:{a:2:{s:7:"defects";a:5:{s:69:"Netzbegruenung\GreenDirectory\Models\Tests\PartyTest::testDeserialize";i:5;s:74:"Netzbegruenung\GreenDirectory\Models\Tests\SerializerTest::testDeserialize";i:4;s:67:"Netzbegruenung\GreenDirectory\Tests\SerializerTest::testDeserialize";i:5;s:55:"Netzbegruenung\GreenDirectory\Tests\ReaderTest::testAll";i:5;s:59:"Netzbegruenung\GreenDirectory\Tests\RepositoryTest::testAll";i:4;}s:5:"times";a:5:{s:69:"Netzbegruenung\GreenDirectory\Models\Tests\PartyTest::testDeserialize";d:0.009;s:74:"Netzbegruenung\GreenDirectory\Models\Tests\SerializerTest::testDeserialize";d:0.01;s:67:"Netzbegruenung\GreenDirectory\Tests\SerializerTest::testDeserialize";d:0.042;s:55:"Netzbegruenung\GreenDirectory\Tests\ReaderTest::testAll";d:0.919;s:59:"Netzbegruenung\GreenDirectory\Tests\RepositoryTest::testAll";d:0.898;}}}

10
.travis.yml Normal file
View File

@ -0,0 +1,10 @@
language: php
php:
- '7.0'
- '7.1'
- '7.2'
- nightly
install:
- travis_retry composer update --no-interaction --no-suggest
- cd tests; git clone git@github.com:netzbegruenung/green-directory.git

19
LICENSE Normal file
View File

@ -0,0 +1,19 @@
Copyright (c) 2019 NETZBEGRÜNUNG Verein für grüne Netzkultur e.V.
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

5
bootstrap.php Normal file
View File

@ -0,0 +1,5 @@
<?php
define('NETZBEGRUENUNG_GREEN_DIRECTORY_MODELS_SERIALIZER_PREFIX', 'Netzbegruenung\GreenDirectory\Model');
define('NETZBEGRUENUNG_GREEN_DIRECTORY_MODELS_SERIALIZER_METADATA', __DIR__ . DIRECTORY_SEPARATOR . 'serializer');

23
composer.json Normal file
View File

@ -0,0 +1,23 @@
{
"name": "netzbegruenung/green-directory-client",
"description": "PHP client representations of netzbegruenung/green-directory",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Maximilian Ruta",
"email": "maximilian.ruta@netzbegruenung.de"
}
],
"require": {
"ext-yaml": "*",
"jms/serializer": "^3.0",
"psr/cache": "^1.0"
},
"autoload": {
"psr-4": {
"Netzbegruenung\\GreenDirectory\\": "src/"
},
"files": ["bootstrap.php"]
}
}

17
phpunit.xml.dist Normal file
View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Test Suite">
<directory suffix=".php">./tests</directory>
</testsuite>
</testsuites>
</phpunit>

4
serializer/Email.yml Normal file
View File

@ -0,0 +1,4 @@
Netzbegruenung\GreenDirectory\Model\Email:
properties:
address:
type: string

View File

@ -0,0 +1,6 @@
Netzbegruenung\GreenDirectory\Model\ExternalRef:
properties:
type:
type: string
key:
type: string

10
serializer/Party.yml Normal file
View File

@ -0,0 +1,10 @@
Netzbegruenung\GreenDirectory\Model\Party:
properties:
country:
type: string
level:
type: string
urls:
type: array<Netzbegruenung\GreenDirectory\Model\Url>
emails:
type: array<Netzbegruenung\GreenDirectory\Model\Email>

24
serializer/Person.yml Normal file
View File

@ -0,0 +1,24 @@
Netzbegruenung\GreenDirectory\Model\Person:
properties:
name:
type: string
givenName:
type: string
familyName:
type: string
level:
type: string
country:
type: string
state:
type: string
district:
type: string
city:
type: string
region:
type: string
urls:
type: array<Netzbegruenung\GreenDirectory\Model\Url>
emails:
type: array<Netzbegruenung\GreenDirectory\Model\Email>

View File

@ -0,0 +1,22 @@
Netzbegruenung\GreenDirectory\Model\RegionalChapter:
properties:
name:
type: string
level:
type: string
country:
type: string
state:
type: string
district:
type: string
city:
type: string
region:
type: string
urls:
type: array<Netzbegruenung\GreenDirectory\Model\Url>
emails:
type: array<Netzbegruenung\GreenDirectory\Model\Email>
externalRefs:
type: array<Netzbegruenung\GreenDirectory\Model\ExternalRef>

6
serializer/Url.yml Normal file
View File

@ -0,0 +1,6 @@
Netzbegruenung\GreenDirectory\Model\Url:
properties:
type:
type: string
url:
type: string

View File

@ -0,0 +1,18 @@
Netzbegruenung\GreenDirectory\Model\YouthOrganization:
properties:
level:
type: string
country:
type: string
state:
type: string
district:
type: string
city:
type: string
region:
type: string
urls:
type: array<Netzbegruenung\GreenDirectory\Model\Url>
emails:
type: array<Netzbegruenung\GreenDirectory\Model\Email>

12
src/IndexInterface.php Normal file
View File

@ -0,0 +1,12 @@
<?php
namespace Netzbegruenung\GreenDirectory;
use Netzbegruenung\GreenDirectory\Model\ItemInterface;
interface IndexInterface
{
public function add(ItemInterface $item) : void;
public function find(string $reference) : iterable;
}

24
src/Model/Email.php Normal file
View File

@ -0,0 +1,24 @@
<?php
namespace Netzbegruenung\GreenDirectory\Model;
class Email
{
/**
* @var string
*/
protected $address;
public function __construct(string $address)
{
$this->address = $address;
}
/**
* @return string
*/
public function getAddress(): string
{
return $this->address;
}
}

33
src/Model/ExternalRef.php Normal file
View File

@ -0,0 +1,33 @@
<?php
namespace Netzbegruenung\GreenDirectory\Model;
class ExternalRef
{
/**
* @var string
* @see \Netzbegruenung\GreenDirectory\Model\ExternalRef\Type
*/
protected $type;
/**
* @var string
*/
protected $key;
/**
* @return string
*/
public function getType(): string
{
return $this->type;
}
/**
* @return string
*/
public function getKey(): string
{
return $this->key;
}
}

View File

@ -0,0 +1,8 @@
<?php
namespace Netzbegruenung\GreenDirectory\Model\ExternalRef;
final class Type
{
const SHERPA = 'SHERPA';
}

View File

@ -0,0 +1,11 @@
<?php
namespace Netzbegruenung\GreenDirectory\Model;
interface ExternalRefAwareInterface
{
/**
* @return ExternalRef[]
*/
public function getExternalRefs() : array;
}

View File

@ -0,0 +1,8 @@
<?php
namespace Netzbegruenung\GreenDirectory\Model;
interface ItemInterface
{
public function getType() : string;
}

18
src/Model/Level.php Normal file
View File

@ -0,0 +1,18 @@
<?php
namespace Netzbegruenung\GreenDirectory\Model;
final class Level
{
const DE_BUNDESVERBAND = 'DE:BUNDESVERBAND';
const DE_LANDESVERBAND = 'DE:LANDESVERBAND';
const DE_REGIONALVERBAND = 'DE:REGIONALVERBAND';
const DE_KREISVERBAND = 'DE:KREISVERBAND';
const DE_BEZIRKSVERBAND = 'DE:BEZIRKSVERBAND';
const DE_ORTSVERBAND = 'DE:ORTSVERBAND';
}

74
src/Model/Party.php Normal file
View File

@ -0,0 +1,74 @@
<?php
namespace Netzbegruenung\GreenDirectory\Model;
class Party implements ItemInterface
{
/**
* ISO 3166-1 Alpha-2 country code
*
* @var string
*/
protected $country;
/**
* @var string
* @see \Netzbegruenung\GreenDirectory\Model\Level
*/
protected $level;
/**
* @var Url[]
*/
protected $urls = [];
/**
* @var Email[]
*/
protected $emails = [];
public function __construct(string $country, string $level, array $urls, array $emails)
{
$this->country = $country;
$this->level = $level;
$this->urls = $urls;
$this->emails = $emails;
}
public function getType() : string
{
return 'PARTY';
}
/**
* @return string
*/
public function getCountry(): string
{
return $this->country;
}
/**
* @return string
*/
public function getLevel(): string
{
return $this->level;
}
/**
* @return Url[]
*/
public function getUrls(): array
{
return $this->urls;
}
/**
* @return Email[]
*/
public function getEmails(): array
{
return $this->emails;
}
}

181
src/Model/Person.php Normal file
View File

@ -0,0 +1,181 @@
<?php
namespace Netzbegruenung\GreenDirectory\Model;
class Person implements ItemInterface
{
/**
* @var string
*/
protected $name;
/**
* @var string|null
*/
protected $givenName;
/**
* @var string|null
*/
protected $familyName;
/**
* @var string|null
* @see \Netzbegruenung\GreenDirectory\Model\Level
*/
protected $level;
/**
* @var string|null
*/
protected $country;
/**
* @var string|null
*/
protected $state;
/**
* @var string|null
*/
protected $district;
/**
* @var string|null
*/
protected $city;
/**
* @var string|null
*/
protected $region;
/**
* @var Url[]
*/
protected $urls = [];
/**
* @var Email[]
*/
protected $emails = [];
public function __construct(
string $name,
?string $givenName,
?string $familyName,
?string $level,
?string $country,
?string $state,
?string $district,
?string $city,
?string $region,
array $urls = [],
array $emails = []
) {
$this->name = $name;
$this->givenName = $givenName;
$this->familyName = $familyName;
$this->level = $level;
$this->country = $country;
$this->state = $state;
$this->district = $district;
$this->city = $city;
$this->region = $region;
$this->urls = $urls;
$this->emails = $emails;
}
public function getType() : string
{
return 'PERSON';
}
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* @return string|null
*/
public function getGivenName(): ?string
{
return $this->givenName;
}
/**
* @return string|null
*/
public function getFamilyName(): ?string
{
return $this->familyName;
}
/**
* @return string|null
*/
public function getLevel(): ?string
{
return $this->level;
}
/**
* @return string|null
*/
public function getCountry(): ?string
{
return $this->country;
}
/**
* @return string|null
*/
public function getState(): ?string
{
return $this->state;
}
/**
* @return string|null
*/
public function getDistrict(): ?string
{
return $this->district;
}
/**
* @return string|null
*/
public function getCity(): ?string
{
return $this->city;
}
/**
* @return string|null
*/
public function getRegion(): ?string
{
return $this->region;
}
/**
* @return Url[]
*/
public function getUrls(): array
{
return $this->urls;
}
/**
* @return Email[]
*/
public function getEmails(): array
{
return $this->emails;
}
}

View File

@ -0,0 +1,166 @@
<?php
namespace Netzbegruenung\GreenDirectory\Model;
class RegionalChapter implements ItemInterface, ExternalRefAwareInterface
{
/**
* @var string|null
*/
protected $name;
/**
* @var string|null
* @see \Netzbegruenung\GreenDirectory\Model\Level
*/
protected $level;
/**
* @var string
*/
protected $country;
/**
* @var string|null
*/
protected $state;
/**
* @var string|null
*/
protected $district;
/**
* @var string|null
*/
protected $city;
/**
* @var string|null
*/
protected $region;
/**
* @var Url[]
*/
protected $urls = [];
/**
* @var Email[]
*/
protected $emails = [];
/**
* @var ExternalRef[]
*/
protected $externalRefs = [];
public function __construct(
string $name,
?string $level,
string $country,
?string $state,
?string $district,
?string $city,
?string $region,
array $urls = [],
array $emails = [],
array $externalRefs = []
) {
$this->name = $name;
$this->level = $level;
$this->country = $country;
$this->state = $state;
$this->district = $district;
$this->city = $city;
$this->region = $region;
$this->urls = $urls;
$this->emails = $emails;
$this->externalRefs = $externalRefs;
}
public function getType() : string
{
return 'REGIONAL_CHAPTER';
}
/**
* @return string
*/
public function getName(): ?string
{
return $this->name;
}
/**
* @return string|null
*/
public function getLevel(): ?string
{
return $this->level;
}
/**
* @return string
*/
public function getCountry(): string
{
return $this->country;
}
/**
* @return string|null
*/
public function getState(): ?string
{
return $this->state;
}
/**
* @return string|null
*/
public function getDistrict(): ?string
{
return $this->district;
}
/**
* @return string|null
*/
public function getCity(): ?string
{
return $this->city;
}
/**
* @return string|null
*/
public function getRegion(): ?string
{
return $this->region;
}
/**
* @return Url[]
*/
public function getUrls(): array
{
return $this->urls;
}
/**
* @return Email[]
*/
public function getEmails(): array
{
return $this->emails;
}
/**
* @return ExternalRef[]
*/
public function getExternalRefs(): array
{
return $this->externalRefs;
}
}

38
src/Model/Url.php Normal file
View File

@ -0,0 +1,38 @@
<?php
namespace Netzbegruenung\GreenDirectory\Model;
class Url
{
/**
* @var string
*/
protected $type;
/**
* @var string
*/
protected $url;
public function __construct(string $type, string $url)
{
$this->type = $type;
$this->url = $url;
}
/**
* @return string
*/
public function getType(): string
{
return $this->type;
}
/**
* @return string
*/
public function getUrl(): string
{
return $this->url;
}
}

16
src/Model/Url/Type.php Normal file
View File

@ -0,0 +1,16 @@
<?php
namespace Netzbegruenung\GreenDirectory\Model\Url;
final class Type
{
const WEBSITE = 'WEBSITE';
const FACEBOOK = 'FACEBOOK';
const TWITTER = 'TWITTER';
const INSTAGRAM = 'INSTAGRAM';
const WIKIDATA = 'WIKIDATA';
}

View File

@ -0,0 +1,135 @@
<?php
namespace Netzbegruenung\GreenDirectory\Model;
class YouthOrganization implements ItemInterface
{
/**
* @var string|null
*/
protected $level;
/**
* @var string|null
*/
protected $country;
/**
* @var string|null
*/
protected $state;
/**
* @var string|null
*/
protected $district;
/**
* @var string|null
*/
protected $city;
/**
* @var string|null
*/
protected $region;
/**
* @var Url[]
*/
protected $urls = [];
/**
* @var Email[]
*/
protected $emails = [];
public function __construct(
?string $level,
?string $country,
?string $state,
?string $district,
?string $city,
?string $region,
array $urls = [],
array $emails = []
) {
$this->level = $level;
$this->country = $country;
$this->state = $state;
$this->district = $district;
$this->city = $city;
$this->region = $region;
$this->urls = $urls;
$this->emails = $emails;
}
public function getType() : string
{
return 'YOUTH_ORGANIZATION';
}
/**
* @return string|null
*/
public function getLevel(): ?string
{
return $this->level;
}
/**
* @return string|null
*/
public function getCountry(): ?string
{
return $this->country;
}
/**
* @return string|null
*/
public function getState(): ?string
{
return $this->state;
}
/**
* @return string|null
*/
public function getDistrict(): ?string
{
return $this->district;
}
/**
* @return string|null
*/
public function getCity(): ?string
{
return $this->city;
}
/**
* @return string|null
*/
public function getRegion(): ?string
{
return $this->region;
}
/**
* @return Url[]
*/
public function getUrls(): array
{
return $this->urls;
}
/**
* @return Email[]
*/
public function getEmails(): array
{
return $this->emails;
}
}

56
src/Reader.php Normal file
View File

@ -0,0 +1,56 @@
<?php
namespace Netzbegruenung\GreenDirectory;
class Reader
{
/**
* @var string
*/
protected $directory;
/**
* @var Serializer
*/
protected $serializer;
public function __construct(
string $directory,
Serializer $serializer = null,
array $indizes = []
) {
$this->directory = $directory;
if ($serializer === null) {
$serializer = SerializerFactory::create();
}
$this->serializer = $serializer;
$this->indizes = $indizes;
}
private function findFiles() : iterable
{
$iterator = new \RecursiveDirectoryIterator($this->directory);
/** @var \SplFileInfo $item */
foreach (new \RecursiveIteratorIterator($iterator) as $item) {
if (in_array($item->getExtension(), ['yaml', 'yml'])) {
yield $item->getPathname();
}
}
yield from [];
}
public function all() : iterable
{
foreach ($this->findFiles() as $file) {
foreach ($this->serializer->deserializeFile($file) as $item) {
yield $item;
}
}
yield from [];
}
}

56
src/Repository.php Normal file
View File

@ -0,0 +1,56 @@
<?php
namespace Netzbegruenung\GreenDirectory;
use Netzbegruenung\GreenDirectory\Model\ExternalRef\Type;
use Netzbegruenung\GreenDirectory\Model\ExternalRefAwareInterface;
use Netzbegruenung\GreenDirectory\Model\ItemInterface;
class Repository
{
protected $reader;
protected $index;
public function __construct(Reader $reader)
{
$this->reader = $reader;
}
protected static function getSherpaId(ItemInterface $item) : ?string
{
if ($item instanceof ExternalRefAwareInterface) {
foreach ($item->getExternalRefs() as $ref) {
if ($ref->getType() == Type::SHERPA) {
return $ref->getKey();
}
}
}
return null;
}
protected function index()
{
if ($this->index === null) {
$this->index = [];
foreach ($this->reader->all() as $item) {
$ref = self::getSherpaId($item);
if ($ref !== null) {
$this->index[$ref] = $item;
}
}
}
}
public function findBySherpaId(string $id) : ?ItemInterface
{
$this->index();
if (isset($this->index[$id])) {
return $this->index[$id];
}
return null;
}
}

80
src/Serializer.php Normal file
View File

@ -0,0 +1,80 @@
<?php
namespace Netzbegruenung\GreenDirectory;
use JMS\Serializer\SerializerInterface;
use Netzbegruenung\GreenDirectory\Model\ItemInterface;
use Netzbegruenung\GreenDirectory\Model\Party;
use Netzbegruenung\GreenDirectory\Model\Person;
use Netzbegruenung\GreenDirectory\Model\RegionalChapter;
use Netzbegruenung\GreenDirectory\Model\YouthOrganization;
final class Serializer
{
protected $serializer;
public function __construct(SerializerInterface $serializer)
{
$this->serializer = $serializer;
}
/**
* @param string $file
* @return ItemInterface[]
* @throws UnknownTypeException
*/
public function deserializeFile(string $file) : iterable
{
return $this->deserializeArray(yaml_parse_file($file, -1));
}
/**
* @param string $data
* @return ItemInterface[]
* @throws UnknownTypeException
*/
public function deserialize(string $data) : iterable
{
return $this->deserializeArray(yaml_parse($data, -1));
}
/**
* @param array[] $documents
* @return ItemInterface[]
* @throws UnknownTypeException
*/
public function deserializeArray(array $documents) : iterable
{
foreach ($documents as $document) {
$class = null;
switch ($document['type']) {
case 'PARTY':
$class = Party::class;
break;
case 'PERSON':
$class = Person::class;
break;
case 'REGIONAL_CHAPTER':
$class = RegionalChapter::class;
break;
case 'YOUTH_ORGANIZATION':
$class = YouthOrganization::class;
break;
}
if ($class === null) {
continue;
}
unset($document['type']);
yield $this->serializer->deserialize(
json_encode($document),
$class,
'json'
);
}
yield from [];
}
}

17
src/SerializerFactory.php Normal file
View File

@ -0,0 +1,17 @@
<?php
namespace Netzbegruenung\GreenDirectory;
class SerializerFactory
{
public static function create() : Serializer
{
return new Serializer(\JMS\Serializer\SerializerBuilder::create()
->setMetadataDirs([
NETZBEGRUENUNG_GREEN_DIRECTORY_MODELS_SERIALIZER_PREFIX =>
NETZBEGRUENUNG_GREEN_DIRECTORY_MODELS_SERIALIZER_METADATA
])
->build());
}
}

View File

@ -0,0 +1,8 @@
<?php
namespace Netzbegruenung\GreenDirectory;
class UnknownTypeException extends \RuntimeException
{
}

25
tests/ReaderTest.php Normal file
View File

@ -0,0 +1,25 @@
<?php
namespace Netzbegruenung\GreenDirectory\Tests;
use Netzbegruenung\GreenDirectory\Reader;
use PHPUnit\Framework\TestCase;
class ReaderTest extends TestCase
{
/**
* @var Reader
*/
protected $reader;
public function setUp(): void
{
$this->reader = new Reader(__DIR__ . '/green-directory/data');
}
public function testAll()
{
$items = iterator_to_array($this->reader->all());
$this->assertGreaterThan(1, count($items));
}
}

29
tests/RepositoryTest.php Normal file
View File

@ -0,0 +1,29 @@
<?php
namespace Netzbegruenung\GreenDirectory\Tests;
use Netzbegruenung\GreenDirectory\Model\RegionalChapter;
use Netzbegruenung\GreenDirectory\Reader;
use Netzbegruenung\GreenDirectory\Repository;
use PHPUnit\Framework\TestCase;
class RepositoryTest extends TestCase
{
/**
* @var Repository
*/
protected $repository;
public function setUp(): void
{
$this->repository = new Repository(new Reader(__DIR__ . '/green-directory/data'));
}
public function testAll()
{
/** @var RegionalChapter $ov */
$ov = $this->repository->findBySherpaId('11002609');
$this->assertInstanceOf(RegionalChapter::class, $ov);
$this->assertEquals('Köln-Kalk', $ov->getCity());
}
}

32
tests/SerializerTest.php Normal file
View File

@ -0,0 +1,32 @@
<?php
namespace Netzbegruenung\GreenDirectory\Tests;
use Netzbegruenung\GreenDirectory\Model\ItemInterface;
use Netzbegruenung\GreenDirectory\Serializer;
use Netzbegruenung\GreenDirectory\SerializerFactory;
use PHPUnit\Framework\TestCase;
class SerializerTest extends TestCase
{
/**
* @var Serializer
*/
protected $serializer;
public function setUp(): void
{
$this->serializer = SerializerFactory::create();
}
public function testDeserialize()
{
$items = iterator_to_array($this->serializer->deserializeFile(
__DIR__ . '/green-directory/data/countries/de/nw/data.yaml'
));
foreach ($items as $item) {
$this->assertInstanceOf(ItemInterface::class, $item);
}
}
}