Completed
Push — master ( 2236d8...f66191 )
by Taosikai
10:51
created

AbstractManager::createMany()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the slince/shopify-api-php
5
 *
6
 * (c) Slince <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Slince\Shopify\Common\Manager;
13
14
use Slince\Shopify\Client;
15
use Slince\Shopify\Common\Model\ModelInterface;
16
17
abstract class AbstractManager implements ManagerInterface
18
{
19
    /**
20
     * @var Client
21
     */
22
    protected $client;
23
24
    public function __construct(Client $client)
25
    {
26
        $this->client = $client;
27
    }
28
29
    /**
30
     * Gets the model class.
31
     *
32
     * @return string
33
     */
34
    abstract public function getModelClass();
35
36
    /**
37
     * Gets the resource name.
38
     *
39
     * @return string
40
     */
41
    abstract public function getResourceName();
42
43
    /**
44
     * Create the model from an array.
45
     *
46
     * @param array $data
47
     *
48
     * @return ModelInterface
49
     */
50
    public function fromArray(array $data)
51
    {
52
        return $this->client->getHydrator()->hydrate($this->getModelClass(), $data);
53
    }
54
55
    /**
56
     * Create many models from an array.
57
     *
58
     * @param array $data
59
     *
60
     * @return ModelInterface[]
61
     */
62
    public function createMany(array $data)
63
    {
64
        $models = [];
65
        foreach ($data as $item) {
66
            $models[] = $this->fromArray($item);
67
        }
68
        return $models;
69
    }
70
71
    /**
72
     * Converts the model to array.
73
     *
74
     * @param ModelInterface $model
75
     * @return array
76
     */
77
    public function toArray($model)
78
    {
79
        return $this->client->getHydrator()->extract($model);
80
    }
81
}