### Install JBZoo Data via Composer
Source: https://github.com/jbzoo/data/blob/master/README.md
This command installs the JBZoo Data package using Composer, a dependency manager for PHP. Ensure you have Composer installed globally to use this command.
```sh
composer require jbzoo/data
```
--------------------------------
### Install and Update Dependencies with Composer
Source: https://github.com/jbzoo/data/blob/master/CLAUDE.md
This command installs or updates all project dependencies managed by Composer. It ensures the project has the necessary libraries to run and be developed.
```bash
make update
```
--------------------------------
### Object-like Access (JBZoo/Data)
Source: https://github.com/jbzoo/data/blob/master/README.md
Shows the object-oriented approach to accessing data elements using property access syntax (`->`) and the `get()` method provided by JBZoo/Data.
```php
$d->key;
$d->get('key');
$d->find('key');
$d->offsetGet('key');
```
--------------------------------
### Type Conversion with Filters
Source: https://github.com/jbzoo/data/blob/master/CLAUDE.md
Shows how to use the `get()` method with a filter to perform type conversion. The third argument specifies the desired type or a validation filter.
```php
$data = data(['count' => '5']);
$count = $data->get('count', 0, 'int'); // $count will be integer 5
```
--------------------------------
### Access Data Using Method Syntax with Default Value
Source: https://github.com/jbzoo/data/blob/master/CLAUDE.md
Illustrates retrieving data using the `get()` method, which allows specifying a default value if the key is not found. This prevents errors when accessing potentially undefined keys.
```php
$data = data(['key' => 'value']);
echo $data->get('key', 'default'); // Output: value
echo $data->get('nonexistent', 'default'); // Output: default
```
--------------------------------
### Get Value with Default (JBZoo/Data vs Pure PHP)
Source: https://github.com/jbzoo/data/blob/master/README.md
Compares how to safely retrieve a value from a data structure and provide a default if the key is not found. JBZoo/Data uses a dedicated method, while pure PHP uses the null coalescing operator.
```php
// JBZoo/Data
$d->get('key', 42);
// Pure PHP
$ar['key'] ?? 42;
```
--------------------------------
### Using Custom Callbacks for Filtering
Source: https://github.com/jbzoo/data/blob/master/CLAUDE.md
Demonstrates how to use custom callback functions as filters with the `get()` method. This allows for complex, application-specific data transformations.
```php
$data = data(['value' => 'some_value']);
$processed = $data->get('value', null, function($v) { return strtoupper($v); }); // $processed will be 'SOME_VALUE'
```
--------------------------------
### Filter and Transform Data Values
Source: https://github.com/jbzoo/data/blob/master/README.md
Explains how to apply filters to data values when retrieving them using `get()` or `find()`. It supports predefined filters from JBZoo/Utils and custom callback functions for complex transformations.
```php
$config->get('key', 42, 'int'); // Smart converting to integer
$config->find('key', 42, 'float'); // To float
$config->find('no', 'yes', 'bool'); // Smart converting popular word to boolean value
$config->get('key', 42, 'strip, trim'); // Chain of filters
// Your custom handler
$config->get('key', 42, function($value) {
return (float)str_replace(',', '.', $value);
});
```
--------------------------------
### Chained Filters for Data Cleaning
Source: https://github.com/jbzoo/data/blob/master/CLAUDE.md
Illustrates applying multiple filters in a chain to clean and transform data retrieved using the `get()` method. Filters are separated by commas.
```php
$data = data([' text ']);
$cleanedText = $data->get(0, '', 'strip,trim'); // $cleanedText will be 'text'
```
--------------------------------
### Serialize to JSON
Source: https://github.com/jbzoo/data/blob/master/README.md
Provides an example of a JSON object structure, commonly used for data interchange. It includes various data types like strings, numbers, and nested arrays.
```json
{
"empty": "",
"zero": "0",
"string": " ",
"tag": "Google.com",
"array1": {
"0": "1",
"1": "2"
},
"section": {
"array2": {
"0": "1",
"12": "2",
"3": "3"
}
},
"section.nested": {
"array3": {
"00": "0",
"01": "1"
}
}
}
```
--------------------------------
### Get Schema of JSON Data
Source: https://github.com/jbzoo/data/blob/master/README.md
Retrieves the schema definition from a JSON data object. This is useful for understanding the structure and types of data within the JSON.
```php
$json = json('{ "some": "thing", "number": 42 }');
dump($json->getSchema();
// [
// "some" => "string",
// "number" => "int"
// ]
```
--------------------------------
### Run All Tests and Code Style Checks
Source: https://github.com/jbzoo/data/blob/master/CLAUDE.md
This command performs a comprehensive check of the project by running all unit tests and code style checks. It provides a consolidated report on code quality and functionality.
```bash
make test-all
```
--------------------------------
### Include JBZoo Codestyle Makefile
Source: https://github.com/jbzoo/data/blob/master/CLAUDE.md
This Makefile snippet includes the standardized JBZoo codestyle initialization. It allows the project to leverage common development tools and checks defined in the JBZoo/codestyle package.
```makefile
ifneq (, $(wildcard ./vendor/jbzoo/codestyle/src/init.Makefile))
include ./vendor/jbzoo/codestyle/src/init.Makefile
endif
```
--------------------------------
### Load Data from Various Sources
Source: https://github.com/jbzoo/data/blob/master/README.md
Demonstrates how to load data into a JBZoo Data object from different file formats including INI, YAML, JSON, and PHP arrays. It also shows how to create a data object from a standard PHP associative array.
```php
use function JBZoo\Data\data;
use function JBZoo\Data\ini;
use function JBZoo\Data\json;
use function JBZoo\Data\phpArray;
use function JBZoo\Data\yml;
$config = data([/* Assoc Array */]); // Any PHP-array or simple object, serialized data
$config = ini('./configs/some.ini'); // Load configs from ini file (or string, or simple array)
$config = yml('./configs/some.yml'); // Yml (or string, or simple array). Parsed with Symfony/Yaml Component.
$config = json('./configs/some.json'); // JSON File (or string, or simple array)
$config = phpArray('./configs/some.php'); // PHP-file that must return array
```
--------------------------------
### Run Benchmark Tests
Source: https://github.com/jbzoo/data/blob/master/CLAUDE.md
Executes performance benchmark tests for the library. This helps in understanding the performance characteristics of different methods and data structures.
```bash
make test-performance
```
--------------------------------
### Create Data Structure (JBZoo/Data)
Source: https://github.com/jbzoo/data/blob/master/README.md
Demonstrates the creation of a data structure using the JBZoo/Data library. This approach is more flexible than pure PHP arrays, supporting multiple formats like objects and JSON.
```php
$d = data($someData);
```
--------------------------------
### Export to Pretty Print PHP
Source: https://github.com/jbzoo/data/blob/master/README.md
Demonstrates converting a configuration object to a string representation in PHP using different methods. This is useful for debugging or generating string outputs.
```php
echo $config;
$result = '' . $config;
$result = (string)$config;
$result = $config->__toString();
```
--------------------------------
### Run PHPUnit Tests
Source: https://github.com/jbzoo/data/blob/master/CLAUDE.md
Executes the project's unit tests using PHPUnit. This command is crucial for verifying the correctness of the code and ensuring no regressions have been introduced.
```bash
make test
```
--------------------------------
### Accessing Undefined Keys (JBZoo/Data)
Source: https://github.com/jbzoo/data/blob/master/README.md
Illustrates how JBZoo/Data handles access to undefined keys without generating notices, offering a safer way to interact with data compared to raw PHP arrays.
```php
$d->get('undefined');
$d->find('undefined');
$d->undefined === null;
$d['undefined'] === null;
$d['undef']['undef'] === null;
```
--------------------------------
### Run All Linters and Code Style Checks
Source: https://github.com/jbzoo/data/blob/master/CLAUDE.md
Executes all configured linters and code style checkers for the project. This ensures adherence to coding standards like PSR-12.
```bash
make codestyle
```
--------------------------------
### Access Data Using Array Syntax
Source: https://github.com/jbzoo/data/blob/master/CLAUDE.md
Demonstrates accessing data within a `Data` object using standard array key notation. This method provides a familiar way to retrieve values.
```php
$data = data(['key' => 'value']);
echo $data['key']; // Output: value
```
--------------------------------
### Generate All Analysis Reports
Source: https://github.com/jbzoo/data/blob/master/CLAUDE.md
This command generates a comprehensive set of analysis reports for the project, including metrics, dependencies, and code statistics.
```bash
make report-all
```
--------------------------------
### Export Data to Various Formats (JBZoo/Data)
Source: https://github.com/jbzoo/data/blob/master/README.md
Illustrates how JBZoo/Data can export data structures into multiple formats including Serialized, JSON, Yml, Ini, and PHP Code, offering more versatility than pure PHP's limited options.
```php
// Export to Serialized
echo data([/* ... */]); // Equivalent to serialize() in this context
// Export to JSON (readable)
echo json([/* ... */]);
// Export to Yml (readable)
echo yml([/* ... */]);
// Export to Ini (readable)
echo ini([/* ... */]);
// Export to PHP Code (readable)
echo phpArray([/* ... */]);
```
--------------------------------
### Update and Test All Commands
Source: https://github.com/jbzoo/data/blob/master/README.md
Shell commands to update project dependencies and run all automated tests. These commands are typically used for maintaining code quality and ensuring the project remains stable.
```sh
make update
make test-all
```
--------------------------------
### Create INI Object Instance
Source: https://github.com/jbzoo/data/blob/master/CLAUDE.md
A factory function to create an instance of the `Ini` class for parsing INI configuration files. It extends `AbstractData` and handles the INI format.
```php
use function Jbzoo\Data\ini;
$ini = ini("[section]\nkey = value");
```
--------------------------------
### Set Value (JBZoo/Data vs Pure PHP)
Source: https://github.com/jbzoo/data/blob/master/README.md
Shows the straightforward assignment of values to keys in data structures, applicable to both JBZoo/Data and pure PHP arrays.
```php
// JBZoo/Data
$d['value'] = 42;
// Pure PHP
$ar['value'] = 42;
```
--------------------------------
### Dump Optimized Autoloader for Composer
Source: https://github.com/jbzoo/data/blob/master/CLAUDE.md
This command generates an optimized autoloader file for Composer. It improves performance by reducing the number of file lookups required to load classes.
```bash
make autoload
```
--------------------------------
### Access Data Using Object Syntax
Source: https://github.com/jbzoo/data/blob/master/CLAUDE.md
Shows how to access data within a `Data` object using object property notation. This offers an alternative to array access for retrieving values.
```php
$data = data(['key' => 'value']);
echo $data->key; // Output: value
```
--------------------------------
### PHP Benchmark: CreateObject
Source: https://github.com/jbzoo/data/blob/master/README.md
Benchmarks the performance of creating objects using different data structures and serialization methods. Compares native PHP arrays, ArrayObject, JSON, INI, and custom Data structures. Results show minimal differences, with native structures generally performing slightly faster.
```php
array = CreateArrayHelper::generateHugeArray($this->size);
}
/**
* @return array
*/
public function provideSubject(): array
{
return [
'Native' => [$this->array],
'ArrayObject' => [new \ArrayObject($this->array)],
'JSON' => [json_encode($this->array)],
'INI' => [parse_ini_string(implode("\n", array_map(function($k, $v) { return "{$k} = " . var_export($v, true);
}, array_keys($this->array), array_values($this->array))))],
'Data' => [new Data($this->array)],
];
}
/**
* @Subject("CreateObject")
* @Iterations(5)
* @AssertGroups({"Native", "ArrayObject", "JSON", "INI", "Data"})
*/
public function benchCreateObject(array $object): void
{
// No operation needed, just object creation.
}
}
```
--------------------------------
### Create JSON Object Instance
Source: https://github.com/jbzoo/data/blob/master/CLAUDE.md
A factory function to create an instance of the `JSON` class, which is designed to handle JSON strings and files. It extends `AbstractData` and implements JSON-specific decoding and encoding.
```php
use function Jbzoo\Data\json;
$json = json('{"key": "value"}');
```
--------------------------------
### Perform Static Analysis with Psalm
Source: https://github.com/jbzoo/data/blob/master/CLAUDE.md
Executes Psalm, another static analysis tool, to find type errors and other potential bugs in the PHP code. It complements PHPStan by offering a different analysis approach.
```bash
make test-psalm
```
--------------------------------
### Serialize to YAML
Source: https://github.com/jbzoo/data/blob/master/README.md
Demonstrates the data structure in YAML format, known for its human-readable syntax. It uses indentation to represent nesting.
```yml
empty: ''
zero: '0'
string: ' '
tag: 'Google.com'
array1:
- '1'
- '2'
section:
array2: { 0: '1', 12: '2', 3: '3' }
section.nested:
array3: ['0', '1']
```
--------------------------------
### Utility Methods for Data Manipulation
Source: https://github.com/jbzoo/data/blob/master/README.md
Introduces utility methods available in the JBZoo Data library, such as searching for values across nested structures and creating a flattened representation of the data.
```php
$config->search($needle); // Find a value also in nested arrays/objects
$config->flattenRecursive(); // Return flattened array copy. Keys are NOT preserved.
```
--------------------------------
### Serialize to INI
Source: https://github.com/jbzoo/data/blob/master/README.md
Illustrates the data structure in INI format, commonly used for configuration files. It uses key-value pairs and sections.
```ini
empty = ""
zero = "0"
string = " "
tag = "Google.com"
array1[0] = "1"
array1[1] = "2"
[section]
array2[0] = "1"
array2[12] = "2"
array2[3] = "3"
[section.nested]
array3[00] = "0"
array3[01] = "1"
```
--------------------------------
### Create YAML Object Instance
Source: https://github.com/jbzoo/data/blob/master/CLAUDE.md
A factory function to create an instance of the `Yml` class for handling YAML data. This class requires the `symfony/yaml` component and extends `AbstractData`.
```php
use function Jbzoo\Data\yml;
$yaml = yml("key: value\n");
```
--------------------------------
### Check Key Existence (JBZoo/Data vs Pure PHP)
Source: https://github.com/jbzoo/data/blob/master/README.md
Demonstrates different methods for checking if a key exists within a data structure. JBZoo/Data offers a `has()` method for clarity, while pure PHP relies on `isset()` or `array_key_exists()`.
```php
// JBZoo/Data
isset($d['key']);
isset($d->key);
$d->has('key');
// Pure PHP
isset($ar['key']);
array_key_exists('key', $ar);
```
--------------------------------
### Generate PHP Depend Analysis Report
Source: https://github.com/jbzoo/data/blob/master/CLAUDE.md
Creates a report based on PHP Depend analysis. This report focuses on coupling, cohesion, and other object-oriented design principles.
```bash
make report-pdepend
```
--------------------------------
### Create Data Object Instance
Source: https://github.com/jbzoo/data/blob/master/CLAUDE.md
A factory function to create an instance of the generic `Data` class. This class extends `ArrayObject` and is suitable for general-purpose data manipulation.
```php
use function Jbzoo\Data\data;
$data = data(['key' => 'value']);
```
--------------------------------
### Set Nested Value (JBZoo/Data)
Source: https://github.com/jbzoo/data/blob/master/README.md
Demonstrates JBZoo/Data's capability to set values within nested data structures using dot notation, simplifying the process compared to manual array creation in pure PHP.
```php
$d->set('q.w.e.r.t.y') = 42;
// Equivalent in pure PHP would require manual creation of intermediate arrays:
// $ar['q']['w']['e']['r']['t']['y'] = 42;
```
--------------------------------
### Accessing Nested Keys (JBZoo/Data)
Source: https://github.com/jbzoo/data/blob/master/README.md
Highlights JBZoo/Data's convenient syntax for accessing deeply nested keys using dot notation or array/object access chains, preventing potential errors common in pure PHP.
```php
$d->find('inner.inner.prop', $default);
$d->inner['inner']['prop'];
$d['inner']['inner']['prop'];
```
--------------------------------
### Create PHP Array Object Instance
Source: https://github.com/jbzoo/data/blob/master/CLAUDE.md
A factory function to create an instance of the `PhpArray` class, intended for PHP files that return arrays. It extends `AbstractData` and loads array data from PHP files.
```php
use function Jbzoo\Data\phpArray;
$phpArray = phpArray(" 'value'];");
```
--------------------------------
### Generate PHP Metrics Report
Source: https://github.com/jbzoo/data/blob/master/CLAUDE.md
Generates a report detailing various code metrics using PHP Metrics. This report provides insights into the complexity, maintainability, and size of the codebase.
```bash
make report-phpmetrics
```
--------------------------------
### Write Data to JBZoo Data Object
Source: https://github.com/jbzoo/data/blob/master/README.md
Shows how to set or update values within a JBZoo Data object. Supports direct key assignment, array-like access, and object-like property assignment.
```php
// Write
$config->set('key', 42);
$config['key'] = 42;
$config->key = 42;
```
--------------------------------
### Access Nested Data Using Find Method
Source: https://github.com/jbzoo/data/blob/master/CLAUDE.md
Demonstrates using the `find()` method for accessing deeply nested data using dot notation. It also supports a default value for non-existent paths.
```php
$data = data(['level1' => ['level2' => 'value']]);
echo $data->find('level1.level2', 'default'); // Output: value
echo $data->find('level1.nonexistent', 'default'); // Output: default
```
--------------------------------
### Perform Static Analysis with Phan
Source: https://github.com/jbzoo/data/blob/master/CLAUDE.md
Executes Phan, a static analysis tool, to find bugs in PHP code. It analyzes the code to detect issues that might not be caught by type checkers alone.
```bash
make test-phan
```
--------------------------------
### Read Data from JBZoo Data Object
Source: https://github.com/jbzoo/data/blob/master/README.md
Illustrates various methods for reading data from a JBZoo Data object, including accessing values by key, using default values, and retrieving nested values safely without encountering PHP errors.
```php
// Read
$config->get('key', 42); // Returns value if it exists oR returns default value
$config['key']; // As regular array
$config->key; // As regular object
// Read nested values without PHP errors
$config->find('deep.config.key', 42); // Gets `$config['very']['deep']['config']['key']` OR returns default value
```
--------------------------------
### Perform Static Analysis with PHPStan
Source: https://github.com/jbzoo/data/blob/master/CLAUDE.md
Runs PHPStan, a static analysis tool, to detect bugs and type errors in the codebase without executing the code. It helps in identifying potential issues early in the development cycle.
```bash
make test-phpstan
```
--------------------------------
### Serialize to Custom Data Format
Source: https://github.com/jbzoo/data/blob/master/README.md
Presents a serialized string representing the data in a custom format, likely specific to the project. This format appears to be a compact representation of array structures.
```text
a:7:{s:5:"empty";s:0:"";s:4:"zero";s:1:"0";s:6:"string";s:1:" ";s:3:"tag";s:42:"Google.com";s:6:"array1";a:2:{i:0;s:1:"1";i:1;s:1:"2";}s:7:"section";a:1:{s:6:"array2";a:3:{i:0;s:1:"1";i:12;s:1:"2";i:3;s:1:"3";}}s:14:"section.nested";a:1:{s:6:"array3";a:2:{s:2:"00";s:1:"0";s:2:"01";s:1:"1";}}}
```
--------------------------------
### Generate Lines of Code Statistics
Source: https://github.com/jbzoo/data/blob/master/CLAUDE.md
Generates statistics on the lines of code in the project using PHPLOC. This includes metrics like total lines, code lines, and comment lines.
```bash
make report-phploc
```
--------------------------------
### Detect Code Smells with PHP Mess Detector
Source: https://github.com/jbzoo/data/blob/master/CLAUDE.md
Runs PHP Mess Detector (PHPMD) to identify potential problems in the code, such as unused code, complex methods, and suboptimal code.
```bash
make test-phpmd
```
--------------------------------
### Check Code Style with PHP CodeSniffer
Source: https://github.com/jbzoo/data/blob/master/CLAUDE.md
Runs PHP CodeSniffer (PHPCS) to check the code against PSR-12 standards and other compatibility rules. It ensures consistent code formatting and style.
```bash
make test-phpcs
```
--------------------------------
### Check for Data Existence
Source: https://github.com/jbzoo/data/blob/master/README.md
Provides methods to check if a specific key or value exists within the JBZoo Data object. Supports methods like `has()` and standard PHP `isset()` syntax.
```php
// Isset
$config->has('key');
isset($config['key']);
isset($config->key);
```
--------------------------------
### PHP Benchmark: GetValue
Source: https://github.com/jbzoo/data/blob/master/README.md
Benchmarks the performance of retrieving a defined value from various data structures. Compares native PHP arrays, ArrayObject, and custom Data objects. Native arrays and ArrayObjects show comparable performance, while Data objects are slower for direct value retrieval.
```php
array = CreateArrayHelper::generateHugeArray($this->size);
}
/**
* @return array
*/
public function provideSubject(): array
{
return [
'Native' => [$this->array],
'ArrayObject' => [new \ArrayObject($this->array)],
'Data' => [new Data($this->array)],
];
}
/**
* @Subject("GetValue")
* @Iterations(5)
* @AssertGroups({"Native", "ArrayObject", "Data"})
*/
public function benchGetValue(array $object): void
{
// Accessing a defined key.
$value = $object['first_key'];
}
}
```
--------------------------------
### Automatically Fix Code Style Issues
Source: https://github.com/jbzoo/data/blob/master/CLAUDE.md
This command uses PHP-CS-Fixer to automatically fix code style issues in the project. It helps in maintaining code consistency and adhering to PSR-12 standards.
```bash
make test-phpcsfixer-fix
```
--------------------------------
### Check Code Style with PHP-CS-Fixer
Source: https://github.com/jbzoo/data/blob/master/CLAUDE.md
Executes PHP-CS-Fixer to check for code style violations. This tool can also be used to automatically fix many of the detected style issues.
```bash
make test-phpcsfixer
```
--------------------------------
### PHP Benchmark: GetUndefinedValue
Source: https://github.com/jbzoo/data/blob/master/README.md
Benchmarks the performance of accessing an undefined value within various data structures. Compares native PHP arrays, ArrayObject, and custom Data objects. Results indicate that accessing undefined keys in native arrays is significantly faster than in Data objects.
```php
array = CreateArrayHelper::generateHugeArray($this->size);
}
/**
* @return array
*/
public function provideSubject(): array
{
return [
'Native' => [$this->array],
'ArrayObject' => [new \ArrayObject($this->array)],
'Data' => [new Data($this->array)],
];
}
/**
* @Subject("GetUndefinedValue")
* @Iterations(5)
* @AssertGroups({"Native", "ArrayObject", "Data"})
*/
public function benchGetUndefinedValue(array $object): void
{
// Accessing an undefined key.
$value = $object['undefined_key'];
}
}
```
--------------------------------
### Serialize to PHP Array
Source: https://github.com/jbzoo/data/blob/master/README.md
Shows how to represent the data structure as a PHP array. This is a fundamental data type in PHP for storing collections of data.
```php
'',
'zero' => '0',
'string' => ' ',
'tag' => 'Google.com',
'array1' => array(
0 => '1',
1 => '2',
),
'section' => array(
'array2' => array(
0 => '1',
12 => '2',
3 => '3',
),
),
'section.nested' => array(
'array3' => array(
'00' => '0',
'01' => '1',
),
),
);
```
--------------------------------
### Remove Data from JBZoo Data Object
Source: https://github.com/jbzoo/data/blob/master/README.md
Demonstrates how to remove keys and their associated values from a JBZoo Data object. Supports both a dedicated `remove()` method and standard PHP `unset()` syntax.
```php
// Unset
$config->remove('key');
unset($config['key']);
unset($config->key);
```
--------------------------------
### PHP Benchmark: GetValueInner
Source: https://github.com/jbzoo/data/blob/master/README.md
Benchmarks the performance of retrieving a deeply nested value within various data structures. Compares native PHP arrays, ArrayObject, and custom Data objects. Nested access in Data objects is significantly slower than in native arrays or ArrayObjects.
```php
array = CreateArrayHelper::generateHugeArray($this->size);
}
/**
* @return array
*/
public function provideSubject(): array
{
return [
'Native' => [$this->array],
'ArrayObject' => [new \ArrayObject($this->array)],
'Data' => [new Data($this->array)],
];
}
/**
* @Subject("GetValueInner")
* @Iterations(5)
* @AssertGroups({"Native", "ArrayObject", "Data"})
*/
public function benchGetValueInner(array $object): void
{
// Accessing a nested key.
$value = $object['first_key']['second_key']['third_key'];
}
}
```
--------------------------------
### PHP Strict Types Declaration
Source: https://github.com/jbzoo/data/blob/master/CLAUDE.md
Enforces strict type checking in PHP files. This declaration must be the very first statement in a PHP file, before any other code.
```php
declare(strict_types=1);
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.