FindByCoordinatesRequest::onDateTime()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 10
1
<?php
2
3
/**
4
 * This file is part of RussianPost SDK package.
5
 *
6
 * © Appwilio (http://appwilio.com), JhaoDa (https://github.com/jhaoda)
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Appwilio\RussianPostSDK\Dispatching\Endpoints\PostOffices\Requests;
15
16
use Appwilio\RussianPostSDK\Core\Arrayable;
17
use Appwilio\RussianPostSDK\Dispatching\Entities\Coordinates;
18
19
final class FindByCoordinatesRequest implements Arrayable
20
{
21
    // TODO: вынести в enum
22
    public const ALL               = 'ALL';
23
    public const ROUND_THE_CLOCK   = 'ROUND_THE_CLOCK';
24
    public const WORK_ON_WEEKENDS  = 'WORK_ON_WEEKENDS';
25
    public const CURRENTLY_WORKING = 'CURRENTLY_WORKING';
26
27
    private const AVAILABLE_FILTERS = [
28
        self::ROUND_THE_CLOCK,
29
        self::WORK_ON_WEEKENDS,
30
        self::CURRENTLY_WORKING,
31
    ];
32
33
    /** @var array */
34
    private $data;
35
36
    public static function fromCoordinates(Coordinates $coordinates)
37
    {
38
        return new self($coordinates, null, null);
39
    }
40
41
    public static function fromYandexData(string $yandexAddress, string $yandexGeo)
42
    {
43
        return new self(null, $yandexAddress, $yandexGeo);
44
    }
45
46
    public function __construct(...$args)
47
    {
48
        [$coordinates, $yAddress, $yGeo] = $args;
49
50
        if ($coordinates instanceof Coordinates && \count($args) === 1) {
51
            $this->data = $coordinates->toArray();
52
        } elseif ($coordinates === null && \is_string($yAddress) && \is_string($yGeo)) {
53
            $this->data = [
54
                'geo-object'     => $yGeo,
55
                'yandex-address' => $yAddress,
56
            ];
57
        } else {
58
            throw new \InvalidArgumentException(
59
                'Используйте для создания запроса методы "fromCoordinates" или "fromYandexData".'
60
            );
61
        }
62
63
        $this->data['filter'] = self::ALL;
64
    }
65
66
    public function filterByWorkingTime(string $value)
67
    {
68
        if (\in_array($value, self::AVAILABLE_FILTERS)) {
69
            $this->data['filter'] = self::ALL;
70
71
            return $this;
72
        }
73
74
        throw new \InvalidArgumentException();
75
    }
76
77
    public function filterByType(bool $value = true)
78
    {
79
        $this->data['filter-by-office-type'] = $value;
80
81
        return $this;
82
    }
83
84
    public function onDateTime(\DateTimeInterface $dateTime)
85
    {
86
        $this->data['current-date-time'] = $dateTime->format('Y-m-dTH:m:s');
87
88
        return $this;
89
    }
90
91
    public function radius(int $radius)
92
    {
93
        $this->data['radius'] = $radius;
94
95
        return $this;
96
    }
97
98
    public function hidePrivate(bool $hidePrivate = true)
99
    {
100
        $this->data['hide-private'] = $hidePrivate;
101
102
        return $this;
103
    }
104
105
    public function take(int $value = 3)
106
    {
107
        $this->data['top'] = $value;
108
109
        return $this;
110
    }
111
112
    public function toArray(): array
113
    {
114
        return $this->data;
115
    }
116
}
117