| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * This software may be modified and distributed under the terms | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * of the MIT license.  See the LICENSE file for details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | namespace FAPI\Localise\Api; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use FAPI\Localise\Model\Asset\Asset as AssetModel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Psr\Http\Message\ResponseInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use FAPI\Localise\Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @author Tobias Nyholm <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | class Asset extends HttpApi | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * Create an asset. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * {@link https://localise.biz/api/docs/assets/createasset}. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @param string $projectKey | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @param string $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @return AssetModel|ResponseInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @throws Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     public function create(string $projectKey, string $id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $param = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             'name' => $id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             'id' => $id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             'type' => 'text', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             'default' => 'untranslated', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $response = $this->httpPost(sprintf('/api/assets?key=%s', $projectKey), $param); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         if (!$this->hydrator) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             return $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         if ($response->getStatusCode() !== 201) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             $this->handleErrors($response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         if ($response->getStatusCode() === 409) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             throw Exception\Domain\AssetConflictException::create($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         return $this->hydrator->hydrate($response, AssetModel::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * Patch an asset. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * {@link https://localise.biz/api/docs/assets/patchasset}. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @param string $projectKey | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @param string $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @param string $type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @param string $context | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @param string $notes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @return AssetModel|ResponseInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @throws Exception | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |     public function patch(string $projectKey, string $id, $type = null, $name = null, $context = null, $notes = null) | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |         $param = [ | 
            
                                                        
            
                                    
            
            
                | 75 |  |  |             'id' => $id, | 
            
                                                        
            
                                    
            
            
                | 76 |  |  |         ]; | 
            
                                                        
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |         if ($type) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |             $param['type'] = $type; | 
            
                                                        
            
                                    
            
            
                | 80 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 81 |  |  |         if ($name) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |             $param['name'] = $name; | 
            
                                                        
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |         if ($context) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |             $param['context'] = $context; | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |         if ($notes) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |             $param['notes'] = $notes; | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |         $response = $this->httpPatch(sprintf('/api/assets/%s.json?key=%s', $id, $projectKey), $param); | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |         if (!$this->hydrator) { | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |             return $response; | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |         if ($response->getStatusCode() === 409) { | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |             throw Exception\Domain\AssetConflictException::create($id); | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |         if ($response->getStatusCode() >= 400) { | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |             $this->handleErrors($response); | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |         return $this->hydrator->hydrate($response, AssetModel::class); | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 106 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 107 |  |  |  | 
            
                        
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: