Completed
Push — master ( ea590f...4351c0 )
by Maciej
05:54
created

RepositoryAbstract::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
/**
3
 * Created by Maciej Paprocki for Bureau-VA.
4
 * Date: 17/02/2016
5
 * Project Name: MaciekPaprocki\WordpressGuzzle
6
 * Time: 10:46.
7
 */
8
namespace BureauVa\WordpressGuzzle\Query;
9
10
use BureauVa\WordpressGuzzle\Helper\StringHelper as S;
11
12
/**
13
 * Class AbstractRepository.
14
 */
15
abstract class RepositoryAbstract
16
{
17
    public static $BASE_PATH = 'posts';
18
19
    /**
20
     * @return string
21
     */
22
    public function __toString()
23
    {
24
        $ar = (array) $this;
25
26
        return $this->prepareCall($ar);
27
    }
28
29
    /**
30
     * @param $ar
31
     *
32
     * @return string
33
     */
34
    private function prepareCall($ar)
35
    {
36
        if (count($ar) === 1 && isset($ar['post__in']) && is_numeric($ar['post__in'])) {
37
            return self::$BASE_PATH.'/'.$ar['post__in'];
38
        }
39
40
        return self::$BASE_PATH.'?'.http_build_query([
41
            'filter' => $ar,
42
        ]);
43
    }
44
45
    /**
46
     * @param $method
47
     * @param $params
48
     *
49
     * @return $this
50
     */
51
    public function __call($method, $params)
52
    {
53
        if (substr($method, 0, 5) == 'where') {
54
            $varName = S::camelCaseToSnakeCase(mb_substr($method, 5));
55
56
            $this->$varName = $params[0];
57
        }
58
59
        return $this;
60
    }
61
62
    /**
63
     * @param $id
64
     *
65
     * @return $this
66
     */
67
    public function whereId($id)
68
    {
69
        $this->post__in = (int) $id;
0 ignored issues
show
Bug introduced by
The property post__in does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
70
71
        return $this;
72
    }
73
74
    /**
75
     * Set's.
76
     *
77
     * @param $key
78
     * @param $value
79
     */
80
    public function setNotFiltered($key, $value)
81
    {
82
        $this->$key = $value;
83
84
        return $this;
85
    }
86
87
    public function whereIds($ids)
88
    {
89
        $this->post__in = (array) $ids;
90
91
        return $this;
92
    }
93
}
94