Passed
Branch master (a62c84)
by Salah
02:38
created

Model   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 81
rs 10
c 0
b 0
f 0
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setRules() 0 5 1
A getModel() 0 5 1
A getFields() 0 3 1
A getData() 0 9 3
A getRules() 0 3 1
A InValidCallback() 0 3 1
1
<?php
2
3
/*
4
 * Copyright (c) 2017 Salah Alkhwlani <[email protected]>
5
 *
6
 * For the full copyright and license information, please view
7
 * the LICENSE file that was distributed with this source code.
8
 */
9
10
namespace Yemenifree\PickServices\Base;
11
12
use InvalidArgumentException;
13
use Yemenifree\PickServices\Helpers\Str;
14
use Yemenifree\Validation\Traits\HasValidator;
15
16
abstract class Model
17
{
18
    use HasValidator;
19
20
    /** @var array */
21
    protected $rules = [];
22
    /** @var array */
23
    protected $fields = [];
24
25
    /**
26
     * Valid current model & return data array.
27
     *
28
     * @return array
29
     */
30
    public function getModel()
31
    {
32
        $this->valid($this->getData(), $this->getRules());
33
34
        return $this->getData();
35
    }
36
37
    /**
38
     * Get data of model.
39
     *
40
     * @return array
41
     */
42
    public function getData(): array
43
    {
44
        return collect(\get_object_vars($this))->filter(function ($value, $name) {
45
            return \in_array($name, $this->getFields()) && !empty($value);
46
        })->map(function ($value, $name) {
47
            $methodName = Str::camel('get_' . $name);
48
49
            return \method_exists($this, $methodName) ? $this->$methodName() : $value;
50
        })->toArray();
51
    }
52
53
    /**
54
     * Get model fields.
55
     *
56
     * @return array
57
     */
58
    public function getFields(): array
59
    {
60
        return $this->fields;
61
    }
62
63
    /**
64
     * Get list of rules array.
65
     *
66
     * @return array
67
     */
68
    private function getRules(): array
69
    {
70
        return $this->rules;
71
    }
72
73
    /**
74
     * In valid function.
75
     *
76
     * @param array $errors
77
     *
78
     * @throws InvalidArgumentException
79
     */
80
    public function InValidCallback(array $errors)
81
    {
82
        throw new InvalidArgumentException($errors[0]);
83
    }
84
85
    /**
86
     * Set Rules of current model.
87
     *
88
     * @param array $rules
89
     *
90
     * @return self
91
     */
92
    public function setRules(array $rules): self
93
    {
94
        $this->rules = $rules;
95
96
        return $this;
97
    }
98
}
99