Skip to content

Commit 7734f5b

Browse files
committed
Reformat + fix code style
1 parent 2dc0684 commit 7734f5b

File tree

7 files changed

+78
-42
lines changed

7 files changed

+78
-42
lines changed

src/Engine.php

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77

88
use Mathematicator\Router\Router;
9-
use Mathematicator\SearchController\BaseController;
9+
use Mathematicator\SearchController\IController;
1010
use Nette\DI\Container;
1111
use Tracy\Debugger;
1212

13-
class Engine
13+
final class Engine
1414
{
1515

1616
/**
@@ -36,41 +36,43 @@ public function __construct(Router $router, Container $container)
3636
/**
3737
* @param string $query
3838
* @return EngineResult
39+
* @throws InvalidDataException
3940
*/
4041
public function compute(string $query): EngineResult
4142
{
42-
if (preg_match('/^(?<left>.+?)\s*vs\.?\s*(?<right>.+?)$/', $query, $versus)) {
43-
$result = new EngineMultiResult($query, null);
44-
45-
$result->addResult($this->compute($versus['left']), 'left');
46-
$result->addResult($this->compute($versus['right']), 'right');
47-
48-
return $result;
43+
if (preg_match('/^(?<left>.+?)\s+vs\.?\s+(?<right>.+?)$/', $query, $versus)) {
44+
return (new EngineMultiResult($query, null))
45+
->addResult($this->compute($versus['left']), 'left')
46+
->addResult($this->compute($versus['right']), 'right');
4947
}
5048

5149
$callback = $this->router->routeQuery($query);
52-
$callbackResult = $this->callCallback($query, $callback);
53-
54-
$result = new EngineSingleResult(
55-
$query,
56-
preg_replace('/^.+\\\\([^\\\\]+)$/', '$1', $callback),
57-
$callbackResult === null ? null : $callbackResult->getInterpret(),
58-
$callbackResult === null ? null : $callbackResult->getBoxes(),
59-
$callbackResult === null ? [] : $callbackResult->getSources()
60-
);
61-
$result->setTime((int) round(Debugger::timer('search_request') * 1000));
62-
63-
return $result;
50+
$matchedRoute = (string) preg_replace('/^.+\\\\([^\\\\]+)$/', '$1', $callback);
51+
52+
if ($result = $this->processCallback($query, $callback)) {
53+
$return = new EngineSingleResult(
54+
$query,
55+
$matchedRoute,
56+
$result->getInterpret(),
57+
$result->getBoxes(),
58+
$result->getSources()
59+
);
60+
} else {
61+
$return = new EngineSingleResult($query, $matchedRoute);
62+
}
63+
64+
return $return->setTime((int) round(Debugger::timer('search_request') * 1000));
6465
}
6566

6667
/**
6768
* @param string $query
6869
* @param string $callback
69-
* @return BaseController|null
70+
* @return IController|null
71+
* @throws InvalidDataException
7072
*/
71-
private function callCallback(string $query, string $callback): ?BaseController
73+
private function processCallback(string $query, string $callback): ?IController
7274
{
73-
/** @var BaseController|null $return */
75+
/** @var IController|null $return */
7476
$return = $this->serviceFactory->getByType($callback);
7577

7678
if ($return !== null) {
@@ -83,7 +85,7 @@ private function callCallback(string $query, string $callback): ?BaseController
8385
}
8486
}
8587

86-
return $return;
88+
return $return ?? null;
8789
}
8890

8991
}

src/Entity/EngineMultiResult.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ public function getResult(string $name = null): EngineResult
3838
/**
3939
* @param EngineResult $result
4040
* @param string|null $name
41+
* @return EngineMultiResult
4142
*/
42-
public function addResult(EngineResult $result, ?string $name = null): void
43+
public function addResult(EngineResult $result, ?string $name = null): self
4344
{
4445
if ($name !== null) {
4546
$this->results[$name] = $result;
@@ -48,6 +49,8 @@ public function addResult(EngineResult $result, ?string $name = null): void
4849
}
4950

5051
$this->setTime($this->getTime() + $result->getTime());
52+
53+
return $this;
5154
}
5255

5356
}

src/Entity/EngineResult.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,13 @@ public function getTime(): int
7272

7373
/**
7474
* @param int $time
75+
* @return EngineResult
7576
*/
76-
public function setTime(int $time): void
77+
public function setTime(int $time): self
7778
{
7879
$this->time = $time;
80+
81+
return $this;
7982
}
8083

8184
}

src/Entity/EngineSingleResult.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class EngineSingleResult extends EngineResult
3232
* @param Box[] $boxes
3333
* @param Source[] $sources
3434
*/
35-
public function __construct(string $query, string $matchedRoute, ?Box $interpret, array $boxes, array $sources = [])
35+
public function __construct(string $query, string $matchedRoute, ?Box $interpret = null, array $boxes = [], array $sources = [])
3636
{
3737
parent::__construct($query, $matchedRoute);
3838
$this->interpret = $interpret;

src/Entity/Source.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Source
1919
private $title;
2020

2121
/**
22-
* @var \string[]
22+
* @var string[]
2323
*/
2424
private $authors = [];
2525

@@ -86,7 +86,7 @@ public function setAuthor(string $author): void
8686
}
8787

8888
/**
89-
* @param \string[] $authors
89+
* @param string[] $authors
9090
*/
9191
public function setAuthors(array $authors): void
9292
{

src/Formatter/FixSpaces.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class FixSpaces
1111
/**
1212
* @var string[]
1313
*/
14-
private $rules = [
14+
private static $rules = [
1515
'(\s|;|^)(\w)\s' => '$1$2&nbsp;',
1616
'(\d)\s(let|rok.*?|g|kg|m|mm|h|hod|hodi.+?|m|min|minu.+?|s|sekun.+?|sec|second|milio.+?|miliar.+?|kč|Kč|°)([^\w])' => '$1&nbsp;$2$3',
1717
'(\d)\s*(\%)' => '$1&nbsp;$2',
@@ -22,27 +22,19 @@ class FixSpaces
2222
'([§\*†©])\s' => '$1&nbsp;',
2323
];
2424

25-
/**
26-
* @param array $rules
27-
*/
28-
public function __construct(array $rules = [])
29-
{
30-
$this->rules = $rules === [] ? $this->rules : array_merge($this->rules, $rules);
31-
}
32-
3325
/**
3426
* @param string $content
3527
* @return mixed|string
3628
*/
3729
public function fix(string $content)
3830
{
39-
$content = preg_replace('/(\&nbsp\;|\s)+/', ' ', $content);
31+
$content = (string) preg_replace('/(\&nbsp\;|\s)+/', ' ', $content);
4032
$iterator = 0;
4133

4234
while (true) {
4335
$origin = $content;
44-
foreach ($this->rules as $pattern => $replacement) {
45-
$content = preg_replace('/' . $pattern . '/', $replacement, $content);
36+
foreach (self::$rules as $pattern => $replacement) {
37+
$content = (string) preg_replace('/' . $pattern . '/', $replacement, $content);
4638
}
4739

4840
$iterator++;

src/Helpers/Czech.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,41 @@ public static function inflection(int $number, array $parameters): string
4949
return $result;
5050
}
5151

52+
/**
53+
* Return "6. září 2018"
54+
*
55+
* @param string|int|\DateTime $date
56+
* @param bool $singular (true => "5. květen 2018", false => "5. května 2018")
57+
* @return string
58+
*/
59+
public static function getDate($date = null, bool $singular = false): string
60+
{
61+
if ($date === null) {
62+
$time = \time();
63+
} elseif ($date instanceof \DateTime) {
64+
$time = $date->getTimestamp();
65+
} else {
66+
$time = is_numeric($date) ? $date : @strtotime($date);
67+
}
68+
69+
$months = [
70+
'ledna', 'února', 'března', 'dubna', 'května',
71+
'června', 'července', 'srpna', 'září', 'října',
72+
'listopadu', 'prosince',
73+
];
74+
75+
$singularMonths = [
76+
'leden', 'únor', 'březen', 'duben', 'květen',
77+
'červen', 'červenec', 'srpen', 'září', 'říjen',
78+
'listopad', 'prosinec',
79+
];
80+
81+
[$day, $month, $year] = explode('-', date('j-n-Y', (int) $time));
82+
83+
return $day . '. ' . ($singular === true
84+
? $singularMonths[(int) $month - 1]
85+
: $months[(int) $month - 1]
86+
) . ' ' . $year;
87+
}
5288

5389
}

0 commit comments

Comments
 (0)