Skip to content

Commit

Permalink
Merge pull request #2 from publicwhip/rb-0.4
Browse files Browse the repository at this point in the history
Removed hydrator as, with afterthought, it wasnt needed. Added a wiki…
  • Loading branch information
rbairwell authored Mar 15, 2019
2 parents fbf50c1 + 817ee6e commit acb4002
Show file tree
Hide file tree
Showing 39 changed files with 1,212 additions and 309 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
# run tests
- run:
name: Run Unit tests
command: ./vendor/bin/phpunit -c ./qaTools/phpunit.xml
command: ./vendor/bin/phpunit -c ./phpunit.xml
- run:
name: Run Grumphp
command: ./vendor/bin/grumphp run
1 change: 0 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
tests/ export-ignore
docker/ export-ignore
qa/ export-ignore
qaTools/ export-ignore
docs/ export-ignore
database/ export-ignore

Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@
"jakub-onderka/php-parallel-lint": "^1.0"
},
"scripts": {
"style": "phpcs --standard=./qaTools/phpcs.xml",
"style": "phpcs --standard=./phpcs.xml",
"test": "phpunit",
"format-code": "php-cs-fixer fix --allow-risky=yes",
"phpstan": "phpstan analyse -l 6 -c qaTools/phpstan.neon src"
"phpstan": "phpstan analyse -l 7 -c ./phpstan.neon src",
"phpcbf": "phpcbf --standard=./phpcs.xml ./src"
},
"license": "MIT",
"authors": [
Expand Down
10 changes: 5 additions & 5 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
2 changes: 1 addition & 1 deletion docs/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ All pull requests must adhere to the
No `grumPHP` tests should fail: you can check this at any time by using `\vendor\bin\grumphp run`
(or on Windows: `vendor\bin\grumphp.bat run`);

Yoda conditions should be used. Along with the PHP Code Sniffer configuration in `qaTools`, there is also a
Yoda conditions should be used. Along with the PHP Code Sniffer configuration in `php_cs.xml`, there is also a
PHPStorm coding style and inspections configuration file.

## Unit Testing
Expand Down
16 changes: 10 additions & 6 deletions docs/Milestones.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
[Jump to Completed Milestones](#completed).

## Planned Milestones (in planning order)

* 0.4 - Get accurate information about Divisions showing: Estimate 1-2 days

Division motion text is stored in a custom 'wiki' format which needs to be parsed.

* 0.4.5 - Create basic division editing system (with no authentication) : Estimate 3-4 days

Expand Down Expand Up @@ -51,13 +47,21 @@

* 1.5 - Add new import/export system. Estimate 1 week.
* 2.0 (?) - Remove v1 of the website.

* 2.1 - Move away from the [parliamentary parser](http://parser.theyworkforyou.com/)
So we can retrieve division information faster (as per [CommonsVotes](https://commonsvotes.digiminster.com/))

## <a name="completed">Completed Milestones</a> (in recently completed order)

* 0.3.1 - Small fixes.
* 0.4 - Get accurate information about Divisions showing: Estimate 1-2 days. DONE - 2019-03-15

Division motion text is stored in a custom 'wiki' format which needs to be parsed.

* 0.3.1 - Small fixes. DONE - 2019-03-14

Added continuous integration settings, missing database tables (for policies), added planned
milestones, fix indentation in composer.json
milestones, fix indentation in composer.json.

* 0.3 - Prepare for upload to Github. Estimate 1 day. DONE - 2019-03-12

The system should present a basic styled POC (proof-of-concept) website with
Expand Down
4 changes: 2 additions & 2 deletions docs/QuickStart.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ If you use PHPStorm as well, you may find going to:
- Mac: PHPStorm->Preferences->Editor->Inspections->(Cog)->Import Profile
- Windows: File->Settings->Editor->Inspections->(Cog)->Import Profile

and importing `qaTools/phpStormInspections.xml` and
and importing `phpStormInspections.xml` and

- Mac: PHPStorm->Preferences->Editor->Code Style->(Cog)->Import Scheme->IntelliJ IDEA code style XML
- Windows: File->Settings->Editor->Inspections->(Cog)->Import Scheme->IntelliJ IDEA code style XML

and then importing `qaTools/phpStormCodeStyle.xml` to help.
and then importing `phpStormCodeStyle.xml` to help.

Other IDEs will be able to make use of the `.editorconfig` file in the root folder to help.

10 changes: 5 additions & 5 deletions grumphp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@ parameters:
tasks:
composer: ~
composer_require_checker:
config_file: ./qaTools/composerRequireCheckWhitelist.json
config_file: ./composerRequireCheckWhitelist.json
file_size:
max_size: 10M
ignore_patterns: ['database','docker','cache','logs']
phpcs:
standard: ./qaTools/phpcs.xml
standard: ./phpcs.xml
ignore_patterns: ['web','database','docker','cache','logs','.phpstorm.meta.php']
phplint: ~
phpmd:
whitelist_patterns:
- /^src\/(.*)/
- /^tests\/(.*)/
ruleset:
- ./qaTools/phpmd.xml
- ./phpmd.xml
phpstan:
configuration: ./qaTools/phpstan.neon
configuration: ./phpstan.neon
level: 7
ignore_patterns: ['web','database','docker','cache','logs','.phpstorm.meta.php','NotFoundHandler.php']
phpversion:
project: '7.2'
phpunit:
config_file: ./qaTools/phpunit.xml
config_file: ./phpunit.xml
securitychecker: ~
phpcpd:
directory: '.'
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions qaTools/phpStormInspections.xml → phpStormInspections.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
<list>
<RulesetDescriptor>
<option name="name" value="PublicWhip PHPMD rule set" />
<option name="path" value="$PROJECT_DIR$/qaTools/phpmd.xml" />
<option name="path" value="$PROJECT_DIR$/phpmd.xml" />
</RulesetDescriptor>
</list>
</option>
Expand All @@ -107,7 +107,7 @@
</inspection_tool>
<inspection_tool class="PhpCSValidationInspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<option name="CODING_STANDARD" value="Custom" />
<option name="CUSTOM_RULESET_PATH" value="$PROJECT_DIR$/qaTools/phpcs.xml" />
<option name="CUSTOM_RULESET_PATH" value="$PROJECT_DIR$/phpcs.xml" />
<option name="SHOW_SNIFF_NAMES" value="true" />
<option name="EXTENSIONS" value="php,js,css,inc" />
</inspection_tool>
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions qaTools/phpunit.xml → phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<phpunit bootstrap="../tests/bootstrap.php" colors="true" forceCoversAnnotation="true">
<phpunit bootstrap="./tests/bootstrap.php" colors="true" forceCoversAnnotation="true">
<testsuites>
<testsuite name="Tests">
<directory suffix=".php">../tests</directory>
<directory suffix=".php">./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">../src</directory>
<directory suffix=".php">./src</directory>
<exclude>
<directory suffix="Interface.php">../src</directory>
<directory suffix="Interface.php">./src</directory>
</exclude>
</whitelist>
</filter>
Expand Down
1 change: 0 additions & 1 deletion qaTools/README.md

This file was deleted.

8 changes: 6 additions & 2 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Psr\Container\ContainerInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface;
use PublicWhip\Providers\DatabaseProvider;
use PublicWhip\Providers\DatabaseProviderInterface;
Expand All @@ -21,6 +22,8 @@
use PublicWhip\Providers\HydratorProviderInterface;
use PublicWhip\Providers\MailerProvider;
use PublicWhip\Providers\MailerProviderInterface;
use PublicWhip\Providers\WikiParserProvider;
use PublicWhip\Providers\WikiParserProviderInterface;
use PublicWhip\Services\DivisionService;
use PublicWhip\Services\DivisionServiceInterface;
use PublicWhip\Web\ErrorHandlers\ErrorHandler;
Expand Down Expand Up @@ -71,7 +74,8 @@ public function getWebConfig(): array
RouterInterface::class => get('router'),
// setup request interface to point to Slim's already existing request
RequestInterface::class => get('request'),
// setup response interface to point to SLim's already existing response
ServerRequestInterface::class => get('request'),
// setup response interface to point to Slim's already existing response
ResponseInterface::class => get('response'),
// setup a CSRF guard
Guard::class => create(Guard::class),
Expand Down Expand Up @@ -119,7 +123,7 @@ public function getGeneralConfig(): array
->method('addToDebugger', get(DebuggerProviderInterface::class)),
DebuggerProviderInterface::class => create(DebuggerProvider::class)
->constructor(get('settings.debug')),
HydratorProviderInterface::class => autowire(HydratorProvider::class),
WikiParserProviderInterface::class => autowire(WikiParserProvider::class),
/**
* Services.
*/
Expand Down
148 changes: 148 additions & 0 deletions src/Entities/AbstractEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php
declare(strict_types=1);

namespace PublicWhip\Entities;

use DateTimeImmutable;
use PublicWhip\Exceptions\Entities\EntityFieldWrongTypeException;
use PublicWhip\Exceptions\Entities\EntityMissingRequiredFieldException;
use PublicWhip\Exceptions\Entities\EntityPassedUnrecognisedFieldException;

/**
* Class AbstractEntity.
*
* Base class for all the entities.
*
* @package PublicWhip\Entities
*/
abstract class AbstractEntity
{

/**
* Returns an associated array of properties the entity requires.
*
* @return array
*/
abstract protected function requiredPropertiesMapping(): array;

/**
* Returns an optional associated array of properties this entity can deal with.
*
* @return array
*/
protected function optionalPropertiesMapping(): array
{
return [];
}

/**
* Build an entity from an array.
*
* @param array $data
* @return static
*/
final public static function buildFromArray(array $data): AbstractEntity
{
$new = new static();
$hydrate = function ($object, $property, $newValue) {
\Closure::bind(function () use ($property, $newValue) {
$this->$property = $newValue;
}, $object, $object)->__invoke();
};
$fieldsMapped = [];
foreach ($new->requiredPropertiesMapping() as $name => $type) {
if (!isset($data[$name])) {
throw new EntityMissingRequiredFieldException(
sprintf(
'Missing required field %s when building %s',
$name,
static::class
)
);
}
if (!self::checkType($name, $type, $data[$name])) {
throw new EntityFieldWrongTypeException(
sprintf(
'Expected required entity field %s to be %s, but was %s when creating %s',
$name,
$type,
is_object($data[$name]) ? get_class($data[$name]) : gettype($data[$name]),
static::class
)
);
}
$hydrate($new, $name, $data[$name]);
$fieldsMapped[] = $name;
}
// now add the optional fields.
foreach ($new->optionalPropertiesMapping() as $name => $type) {
if (isset($data[$name])) {
if (!self::checkType($name, $type, $data[$name])) {
throw new EntityFieldWrongTypeException(
sprintf(
'Expected entity field %s to be %s, but was %s when creating %s',
$name,
$type,
is_object($data[$name]) ? get_class($data[$name]) : gettype($data[$name]),
static::class
)
);
}
$hydrate($new, $name, $data[$name]);
} else {
$hydrate($new, $name, null);
}
$fieldsMapped[] = $name;
}
$difference = array_diff(array_keys($data), $fieldsMapped);
if (count($difference) > 0) {
throw new EntityPassedUnrecognisedFieldException(
sprintf(
'The entity %s was passed the following field(s) which it does not know how to handle: %s',
static::class,
implode(', ', $difference)
)
);
}
return $new;
}

/**
* Checks that the field is of the expected type.
*
* @param string $fieldName Name of the field (for error reporting).
* @param string $expectedType String of the expected type.
* @param mixed $value The value to check.
* @return bool
*/
private static function checkType(string $fieldName, string $expectedType, $value): bool
{
$correctType = true;
switch ($expectedType) {
case 'int':
if (!is_int($value)) {
$correctType = false;
}
break;
case 'string':
if (!is_string($value)) {
$correctType = false;
}
break;
case DateTimeImmutable::class:
if (!$value instanceof DateTimeImmutable) {
$correctType = false;
}
break;
default:
throw new EntityFieldWrongTypeException(
sprintf(
'Bad definition found for field %s in %s',
$fieldName,
static::class
)
);
}
return $correctType;
}
}
Loading

0 comments on commit acb4002

Please sign in to comment.