• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

stripe / stripe-php / 6471862601

10 Oct 2023 04:02PM UTC coverage: 69.665% (-0.5%) from 70.141%
6471862601

push

github

web-flow
Merge pull request #1570 from localheinz/feature/coveralls

Enhancement: Use `coverallsapp/github-action` to report code coverage

2393 of 3435 relevant lines covered (69.67%)

3.5 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/lib/ApiResource.php
1
<?php
2

3
namespace Stripe;
4

5
/**
6
 * Class ApiResource.
7
 *
8
 * */
9
abstract class ApiResource extends StripeObject
10
{
11
    use ApiOperations\Request;
12

13
    /**
14
     * @return \Stripe\Util\Set A list of fields that can be their own type of
15
     * API resource (say a nested card under an account for example), and if
16
     * that resource is set, it should be transmitted to the API on a create or
17
     * update. Doing so is not the default behavior because API resources
18
     * should normally be persisted on their own RESTful endpoints.
19
     */
20
    public static function getSavedNestedResources()
×
21
    {
22
        static $savedNestedResources = null;
×
23
        if (null === $savedNestedResources) {
×
24
            $savedNestedResources = new Util\Set();
×
25
        }
26

27
        return $savedNestedResources;
×
28
    }
29

30
    /**
31
     * @var bool A flag that can be set a behavior that will cause this
32
     * resource to be encoded and sent up along with an update of its parent
33
     * resource. This is usually not desirable because resources are updated
34
     * individually on their own endpoints, but there are certain cases,
35
     * replacing a customer's source for example, where this is allowed.
36
     */
37
    public $saveWithParent = false;
38

39
    public function __set($k, $v)
×
40
    {
41
        parent::__set($k, $v);
×
42
        $v = $this->{$k};
×
43
        if ((static::getSavedNestedResources()->includes($k))
×
44
            && ($v instanceof ApiResource)) {
×
45
            $v->saveWithParent = true;
×
46
        }
47
    }
48

49
    /**
50
     * @throws Exception\ApiErrorException
51
     *
52
     * @return ApiResource the refreshed resource
53
     */
54
    public function refresh()
×
55
    {
56
        $requestor = new ApiRequestor($this->_opts->apiKey, static::baseUrl());
×
57
        $url = $this->instanceUrl();
×
58

59
        list($response, $this->_opts->apiKey) = $requestor->request(
×
60
            'get',
×
61
            $url,
×
62
            $this->_retrieveOptions,
×
63
            $this->_opts->headers
×
64
        );
×
65
        $this->setLastResponse($response);
×
66
        $this->refreshFrom($response->json, $this->_opts);
×
67

68
        return $this;
×
69
    }
70

71
    /**
72
     * @return string the base URL for the given class
73
     */
74
    public static function baseUrl()
×
75
    {
76
        return Stripe::$apiBase;
×
77
    }
78

79
    /**
80
     * @return string the endpoint URL for the given class
81
     */
82
    public static function classUrl()
×
83
    {
84
        // Replace dots with slashes for namespaced resources, e.g. if the object's name is
85
        // "foo.bar", then its URL will be "/v1/foo/bars".
86

87
        /** @phpstan-ignore-next-line */
88
        $base = \str_replace('.', '/', static::OBJECT_NAME);
×
89

90
        return "/v1/{$base}s";
×
91
    }
92

93
    /**
94
     * @param null|string $id the ID of the resource
95
     *
96
     * @throws Exception\UnexpectedValueException if $id is null
97
     *
98
     * @return string the instance endpoint URL for the given class
99
     */
100
    public static function resourceUrl($id)
×
101
    {
102
        if (null === $id) {
×
103
            $class = static::class;
×
104
            $message = 'Could not determine which URL to request: '
×
105
               . "{$class} instance has invalid ID: {$id}";
×
106

107
            throw new Exception\UnexpectedValueException($message);
×
108
        }
109
        $id = Util\Util::utf8($id);
×
110
        $base = static::classUrl();
×
111
        $extn = \urlencode($id);
×
112

113
        return "{$base}/{$extn}";
×
114
    }
115

116
    /**
117
     * @return string the full API URL for this API resource
118
     */
119
    public function instanceUrl()
×
120
    {
121
        return static::resourceUrl($this['id']);
×
122
    }
123
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc