Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Larachileanlaw Laravel Package

unforgivencl/larachileanlaw

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation

    composer require unforgivencl/larachileanlaw:dev-master
    

    Add the service provider to config/app.php under providers:

    Unforgivencl\LaraChileanLaw\LaraChileanLawServiceProvider::class,
    

    Optionally, add the facade alias:

    'LaraChileanLaw' => Unforgivencl\LaraChileanLaw\Facades\LaraChileanLaw::class,
    
  2. First Use Case Fetch the latest 5 published laws:

    use LaraChileanLaw;
    
    $laws = LaraChileanLaw::law()->paginate(5)->getLatestPublished()->fetch();
    

Implementation Patterns

Core Workflows

  1. Querying Laws Chain methods for granular control:

    // Get laws paginated with content filter
    $laws = LaraChileanLaw::law()
        ->paginate(10)
        ->content('aborto')
        ->getByContent()
        ->fetch();
    
    // Get a specific law by BCN (e.g., "1")
    $law = LaraChileanLaw::law()
        ->number('1')
        ->getLatestSpecific()
        ->fetch();
    
  2. Integration with Laravel Ecosystem

    • API Responses: Convert XML responses to JSON for consistency:
      $response = LaraChileanLaw::law()->fetch(); // Returns JSON
      
    • Caching: Cache frequent queries (e.g., latest laws) using Laravel's cache:
      $cachedLaws = Cache::remember('latest_laws', now()->addHours(1), function () {
          return LaraChileanLaw::law()->paginate(5)->getLatestPublished()->fetch();
      });
      
  3. Error Handling Wrap calls in try-catch for API failures:

    try {
        $law = LaraChileanLaw::law()->number('999')->fetch();
    } catch (\Exception $e) {
        Log::error("Chilean Law API Error: " . $e->getMessage());
        return response()->json(['error' => 'Failed to fetch law'], 500);
    }
    

Gotchas and Tips

Pitfalls

  1. Inconsistent XML Structure

    • The underlying leychile.cl API returns inconsistent XML. Validate responses:
      if (!isset($response['data'])) {
          throw new \RuntimeException("Unexpected API response format");
      }
      
  2. Rate Limiting

    • No official rate limits are documented. Monitor API calls to avoid throttling.
  3. Facade vs. Direct Usage

    • Facade (LaraChileanLaw::law()) is convenient but less testable. For tests, inject the service directly:
      $this->app->bind(\Unforgivencl\LaraChileanLaw\Contracts\ChileanLaw::class, function () {
          return new \Unforgivencl\LaraChileanLaw\ChileanLawClient();
      });
      

Debugging Tips

  • Log Raw Responses: Inspect XML/JSON for debugging:

    $rawResponse = LaraChileanLaw::law()->number('1')->fetch();
    Log::debug("Raw API Response:", ['response' => $rawResponse]);
    
  • Check for Deprecations: The package is in early development. Verify compatibility with leychile.cl updates.

Extension Points

  1. Custom Endpoints Extend the base client to support undocumented endpoints:

    // In a service class
    public function getDraftLaws() {
        return $this->client->request('GET', '/api/drafts');
    }
    
  2. Response Transformers Override JSON conversion logic in app/Providers/AppServiceProvider:

    public function boot() {
        LaraChileanLaw::extend(function ($client) {
            $client->setTransformer(function ($xml) {
                return json_decode(json_encode(simplexml_load_string($xml)), true);
            });
        });
    }
    
  3. Testing Mock the API client in tests:

    $mock = Mockery::mock(\Unforgivencl\LaraChileanLaw\Contracts\ChileanLaw::class);
    $mock->shouldReceive('fetch')->andReturn(['data' => []]);
    $this->app->instance(\Unforgivencl\LaraChileanLaw\Contracts\ChileanLaw::class, $mock);
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
craftcms/url-validator
directorytree/privacy-filter-classifier
directorytree/privacy-filter
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata
splash/openapi
splash/scopes
splash/toolkit
testo/output-teamcity
testo/bridge-symfony