Coder Social home page Coder Social logo

codeplusdev / strobj Goto Github PK

View Code? Open in Web Editor NEW

This project forked from uuur86/strobj

0.0 0.0 0.0 66 KB

StrObj is a php micro-library providing a fluent code interface for object manipulation using string parameters.

License: GNU General Public License v2.0

PHP 100.00%

strobj's Introduction

PHP String Objects

This repository allows you to access objects via string. You can also check if their values are valid.

composer require uuur86/strobj

TESTS

php vendor/bin/phpunit tests/TestScenarios

BASIC USAGE

use StrObj\StringObjects;

require('vendor/autoload.php');

$persons = (object)array(
  "persons" => array(
    (object)array(
      "name" => "John Doe",
      "age" => 12
    ),
    (object)array(
      "name" => "Molly Doe",
      "age" => 14
    ),
    (object)array(
      "name" => "Lorem Ipsum",
      "age" => 21
    )
  )
);

$test = StringObjects::instance($persons);

// prints all persons
var_dump($test->get('persons'));

// prints first person's name
var_dump($test->get('persons/0/name'));

// prints all person's name
var_dump($test->get('persons/*/name'));

DATA Validation

use StrObj\StringObjects;

require('vendor/autoload.php');

$persons = json_decode('{
    persons: [
        {
            name: "John Doe",
            age: "twelve"
        },
        {
            name: "Molly Doe",
            age: 14
        },
        {
            name: "Lorem Doe",
            age: 34
        },
        {
            name: "Ipsum Doe",
            age: 21
        }
    ]
}');

$test = StringObjects::instance($persons);

// defines regex types which is going to use in control method
$test->addRegexType('age', '#^[0-9]{1,3}$#siu');

$test->validator('persons/*/age', 'age', true);
$test->validator('persons/*/name', '', true, '#^[a-z0-9 ]+$#siu');

// prints "persons/*/name values are not acceptable!"
if ($test->isValid('persons/*/name')) {
  var_dump($test->get('persons/*/name'));
} else {
  echo "persons/*/name values are not acceptable!";
}

// prints "persons/0/age value is not acceptable!"
if ($test->isValid('persons/0/age')) {
  var_dump($test->get('persons/0/age'));
} else {
  echo "persons/0/age value is not acceptable!";
}

// prints "14"
if ($test->isValid('persons/1/age')) {
  var_dump($test->get('persons/1/age'));
} else {
  echo "persons/1/age value is not acceptable!";
}

// prints "persons/*/age values are not acceptable!"
if ($test->isValid('persons/*/age')) {
  var_dump($test->get('persons/*/age'));
} else {
  echo "persons/*/age values are not acceptable!";
}

strobj's People

Contributors

uuur86 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.