Passed
Push — master ( 6fae9a...ddbc6b )
by Seth
01:51
created

AbstractObject::validateParams()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 12
ccs 6
cts 6
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/** Object */
3
4
namespace Battis\SharedLogs;
5
6
use Battis\SharedLogs\Exceptions\ObjectException;
7
use JsonSerializable;
8
9
/**
10
 * A generic object bound to a database table
11
12
 * Implements `JsonSerializable` for easy output as a JSON response via the API.
13
 *
14
 * @author Seth Battis <[email protected]>
15
 */
16
abstract class AbstractObject implements JsonSerializable
17
{
18
    /**
19
     * Construct an object from the corresponding row of a bound database table
20
     *
21
     * @param array $databaseRecord Associative array of fields
22
     *
23
     * @uses Object::validateParams()
24
     *
25
     * @throws ObjectException If an empty database record is provided
26
     */
27 20
    public function __construct($databaseRecord)
28
    {
29 20
        foreach ($this->validateParams($databaseRecord) as $key => $value) {
30 19
            $this->$key = $value;
31
        }
32 19
    }
33
34
    /**
35
     * Validate (and filter) a row of database information before applying it to the object
36
     *
37
     * @param array $databaseRow Associative array
38
     * @return array Filtered and validated
39
     * @throws ObjectException If no parameters are provided
40
     */
41 20
    protected function validateParams($databaseRow)
42
    {
43
        /* TODO Convert MySQL timestamp to SOAP timestamp for portability */
44 20
        if (is_array($databaseRow)) {
45 19
            return $databaseRow;
46
        } else {
47 3
            throw new ObjectException(
48 3
                'No parameters provided to from which construct object',
49 3
                ObjectException::MISSING_DATABASE_RECORD
50
            );
51
        }
52
    }
53
54
    /**
55
     * Prepare the object for JSON serialization
56
     *
57
     * @see JsonSerializable::jsonSerialize()
58
     *
59
     * @return array
60
     */
61 8
    public function jsonSerialize()
62
    {
63 8
        return get_object_vars($this);
64
    }
65
}
66